#P17163. [CEOI 2026] Birdwatchers

[CEOI 2026] Birdwatchers

Problem Description

The Birdwatchers' Society of San Serriffe has a curiously bloated and ever-changing internal structure. The Society consists of nn chapters, and each member of the Society belongs to exactly one chapter. The chapters are numbered from 11 to nn and the ii-th chapter has mim_i members. Thus the Society has a total of M=m1+m2++mnM=m_1+m_2+\cdots+m_n members.

Each chapter is led by one of its members, who in this role is called the officer of the chapter. The officers are numbered the same as the chapters, so that (for each i=1,,ni=1,\ldots,n) officer ii is the one in charge of chapter ii.

Moreover, the officers are organized hierarchically through a system of mentorship: each officer except one has a mentor, who is the officer of some other chapter. The only officer without a mentor is the President of the Society. If officer aa is the mentor of officer bb, we also say that officer bb is a disciple of officer aa. No officer is, directly or indirectly, a mentor of themselves; thus, by following the sequence from an officer to their mentor, their mentor's mentor, and so on, we eventually always reach the President.

We define the influence of an officer as the sum of the number of members in his chapter and of the influences of all his disciples (if he has any). It will be easily seen that the officer with the highest influence is the President, whose influence always equals MM. An officer is called a senior officer if his influence is M/2\ge M/2.

The Bylaws of the Society specify that the senior officer with the lowest influence (amongst all the senior officers) shall act as the Treasurer of the Society.

From time to time, an officer (other than the President) may change his allegiance, so that he is thenceforth the disciple of a different mentor than before (provided that his new mentor is not one of his disciples, or his disciples' disciples, etc.). Because of this, it can happen that the influence of some officers changes and the role of Treasurer falls to a different officer than before.

Task

Write a program that reads the description of the initial state of the Society and a sequence of changes of allegiance. Your program must print who the Treasurer is in the initial state of the Society as well as after each change of allegiance.

Input Format

The first line contains two integers, nn and qq, separated by a space; nn is the number of chapters, qq is the number of changes of allegiance.

The next nn lines describe the initial state of the Society. The ii-th of these lines contains two integers, sis_i and mim_i, separated by a space; sis_i is the mentor of officer ii (i.e. of the officer in charge of chapter ii), while mim_i is the number of members of chapter ii. The value si=0s_i=0 indicates that officer ii is the President of the Society and thus has no mentor.

The remaining qq lines describe the changes of allegiance. The jj-th of these lines contains two integers, x^j\hat{x}_j and z^j\hat{z}_j, separated by a space. The meaning of these integers is as follows. Let us denote by tjt_j (for j=0,,qj=0,\ldots,q) the Treasurer after the first jj changes of allegiance (thus t0t_0 is the initial Treasurer before the first change of allegiance). Then the jj-th change of allegiance consists of officer zjz_j becoming the new mentor of officer xjx_j, where xj=1+((tj1+x^j)modn)x_j=1+((t_{j-1}+\hat{x}_j)\bmod n) and zj=1+((tj1+z^j)modn)z_j=1+((t_{j-1}+\hat{z}_j)\bmod n). The purpose of this representation of the values xjx_j and zjz_j is to force your program to process the changes of allegiance in the order in which they appear.

The changes of allegiance in the input data will always be valid, i.e. zjz_j will not be equal to xjx_j, nor will zjz_j be a disciple of xjx_j, a disciple of a disciple of xjx_j, etc. It is, however, possible that zjz_j was already the mentor of xjx_j just before the jj-th change (so that nothing actually changes at that point).

Note that if your program calculates the wrong result tjt_j at some point, it will decode the subsequent inputs x^j+1\hat{x}_{j+1}, z^j+1\hat{z}_{j+1}, etc., incorrectly as well, and might crash with an RTE (runtime error) verdict instead of a WA (wrong answer) verdict because the incorrectly decoded inputs might be invalid (e.g. it might erroneously obtain a zj+1z_{j+1} which is a disciple of xj+1x_{j+1}).

Output Format

Print the numbers t0,t1,,tqt_0,t_1,\ldots,t_q, each on its own line, where tjt_j is the Treasurer after the first jj changes of allegiance. Naturally, each tjt_j must be an integer from the range 1tjn1\le t_j\le n.

7 2
0 1
1 3
1 3
2 3
2 1
5 2
5 1
3 7
2 7
2
2
3

Hint

Comment

Initially, officer 22 is the Treasurer (hence t0=2t_0=2). In the first change of allegiance, we read x^1=3\hat{x}_1=3 and z^1=7\hat{z}_1=7 and calculate x1=1+((2+3)mod7)=6x_1=1+((2+3)\bmod 7)=6 and z1=1+((2+7)mod7)=3z_1=1+((2+7)\bmod 7)=3; thus, officer 33 becomes the new mentor of officer 66; officer 22 remains Treasurer (hence t1=2t_1=2). In the second change of allegiance, we read x^2=2\hat{x}_2=2 and z^2=7\hat{z}_2=7 and calculate x2=1+((2+2)mod7)=5x_2=1+((2+2)\bmod 7)=5 and z2=1+((2+7)mod7)=3z_2=1+((2+7)\bmod 7)=3; thus, officer 33 becomes the new mentor of officer 55 and also becomes the new Treasurer (hence t2=3t_2=3).

Constraints

  • 1n10000001\le n\le 1\,000\,000
  • 1q300001\le q\le 30\,000
  • 1mi1\le m_i for each i=1,,ni=1,\ldots,n
  • m1+m2++mn109m_1+m_2+\cdots+m_n\le 10^9
  • 1x^jn1\le\hat{x}_j\le n and 1z^jn1\le\hat{z}_j\le n for each j=1,,qj=1,\ldots,q.

Subtasks

  • Subtask 11 (1515 points): n100n\le 100
  • Subtask 22 (1010 points): n1000n\le 1000
  • Subtask 33 (5050 points): n300000n\le 300\,000
  • Subtask 44 (2525 points): No additional constraints.