CF45B.School

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There are n students studying in the 6th grade, in group "B" of a berland secondary school. Every one of them has exactly one friend whom he calls when he has some news. Let us denote the friend of the person number i by g(i). Note that the friendships are not mutual, i.e. g(g(i)) is not necessarily equal to i.

On day i the person numbered as a__i learns the news with the rating of b__i (b__i ≥ 1). He phones the friend immediately and tells it. While he is doing it, the news becomes old and its rating falls a little and becomes equal to b__i - 1. The friend does the same thing — he also calls his friend and also tells the news. The friend of the friend gets the news already rated as b__i - 2. It all continues until the rating of the news reaches zero as nobody wants to tell the news with zero rating.

More formally, everybody acts like this: if a person x learns the news with a non-zero rating y, he calls his friend g(i) and his friend learns the news with the rating of y - 1 and, if it is possible, continues the process.

Let us note that during a day one and the same person may call his friend and tell him one and the same news with different ratings. Thus, the news with the rating of b__i will lead to as much as b__i calls.

Your task is to count the values of res__i — how many students learned their first news on day i.

The values of b__i are known initially, whereas a__i is determined from the following formula:

where mod stands for the operation of taking the excess from the cleavage, _res_0 is considered equal to zero and v__i — some given integers.

nn 名学生在某伯兰中学六年级 B 组学习。每位学生恰好有一位朋友,当他得知某则新闻时,便会立即给这位朋友打电话告知。我们用 g(i)g(i) 表示编号为 ii 的学生的朋友。注意:这种友谊关系并非双向的,即 g(g(i))g(g(i)) 不一定等于 ii

在第 ii 天,编号为 aia_i 的学生得知一则评分为 bib_ibi1b_i \geq 1)的新闻。他立刻给自己的朋友打电话并告知该新闻;在此过程中,新闻“变旧”,其评分略微下降,变为 bi1b_i - 1。这位朋友也做同样的事——他同样打电话告诉自己的朋友。朋友的朋友收到该新闻时,其评分已变为 bi2b_i - 2。如此继续传递,直到新闻评分为零为止(因为没有人愿意传播评分为零的新闻)。

更严格地定义如下:若某人 xx 以非零评分 yy 得知一则新闻,则他立即致电其朋友 g(x)g(x),使该朋友以评分 y1y - 1 得知该新闻;若 y1>0y - 1 > 0,则该朋友将继续这一过程。

需注意:在同一天内,同一人可能多次给其朋友打电话,并以不同评分传递同一则新闻。因此,评分为 bib_i 的新闻将引发总共 bib_i 次电话。

你的任务是计算 resires_i 的值:即在第 ii 天,有多少名学生首次得知新闻。

所有 bib_i 的值初始已知,而 aia_i 由以下公式确定:

其中 mod\bmod 表示取模运算(即除法余数),规定 res0=0res_0 = 0,而 viv_i 是给定的整数。

输入格式

The first line contains two space-separated integers n and m (2 ≤ n, m ≤ 105) — the number of students and the number of days. The second line contains n space-separated integers g(i) (1 ≤ g(i) ≤ n, g(i) ≠ i) — the number of a friend of the i-th student. The third line contains m space-separated integers v__i (1 ≤ v__i ≤ 107). The fourth line contains m space-separated integers b__i (1 ≤ b__i ≤ 107).

第一行包含两个以空格分隔的整数 nnmm2n,m1052 ≤ n, m ≤ 10^5)——分别表示学生人数和天数。
第二行包含 nn 个以空格分隔的整数 g(i)g(i)1g(i)n1 ≤ g(i) ≤ n,且 g(i)ig(i) ≠ i)——表示第 ii 个学生的朋友编号。
第三行包含 mm 个以空格分隔的整数 viv_i1vi1071 ≤ v_i ≤ 10^7)。
第四行包含 mm 个以空格分隔的整数 bib_i1bi1071 ≤ b_i ≤ 10^7)。

输出格式

Print m lines containing one number each. The i-th line should contain res__i — for what number of students the first news they've learned over the m days in question, was the news number i. The number of the news is the number of the day on which it can be learned. The days are numbered starting from one in the order in which they are given in the input file. Don't output _res_0.

输出 m 行,每行一个数字。第 i 行应包含 res__i —— 即:有多少名学生在所给的 m 天中,首次得知的新闻编号为 i。新闻的编号即为其可被获知的那一天的编号(天数从 1 开始编号,按输入文件中给出的顺序)。不要输出 _res_0。

输入输出样例

  • 输入#1

    3 4
    2 3 1
    1 2 3 4
    1 2 3 4

    输出#1

    1
    1
    1
    0
  • 输入#2

    8 6
    7 6 4 2 3 5 5 7
    10 4 3 8 9 1
    1 1 1 2 2 2

    输出#2

    1
    1
    1
    2
    1
    1

输入解题思路,AI测评打分。不知道怎么写?

首页