CF45B.School

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

There are nn 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 ii by g(i)g(i) . Note that the friendships are not mutual, i.e. g(g(i))g(g(i)) is not necessarily equal to ii .

On day ii the person numbered as aia_{i} learns the news with the rating of bib_{i} ( bi>=1b_{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 bi1b_{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 bi2b_{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 xx learns the news with a non-zero rating yy , he calls his friend g(i)g(i) and his friend learns the news with the rating of y1y-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 bib_{i} will lead to as much as bib_{i} calls.

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

The values of bib_{i} are known initially, whereas aia_{i} is determined from the following formula:

where mod stands for the operation of taking the excess from the cleavage, res0res_{0} is considered equal to zero and viv_{i} — some given integers.

输入格式

The first line contains two space-separated integers nn and mm ( 2<=n,m<=1052<=n,m<=10^{5} ) — the number of students and the number of days. The second line contains nn space-separated integers g(i)g(i) ( 1<=g(i)<=n,g(i)i1<=g(i)<=n,g(i)≠i ) — the number of a friend of the ii -th student. The third line contains mm space-separated integers viv_{i} ( 1<=vi<=1071<=v_{i}<=10^{7} ). The fourth line contains mm space-separated integers bib_{i} ( 1<=bi<=1071<=b_{i}<=10^{7} ).

输出格式

Print mm lines containing one number each. The ii -th line should contain resires_{i} — for what number of students the first news they've learned over the mm days in question, was the news number ii . 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 res0res_{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
    
首页