Given n gangsters each aiming at a distinct target, count survivors after each of q updates to the shooting time of one gangster.
Hard8GraphDynamic programmingSortingImplementationNo attempts yetTime limit2sMemory limit512 MBSteven Byteberg is a movie director who specializes in action movies. He is now working on a new movie about the Byteonian Mafia wars. Byteberg wonders what the climax of the movie, a spectacular exchange of gunfire, should look like.
n gangsters take part in this scene. For simplicity they are numbered from 1 to n. When the tension reaches its peak, every gangster draws his weapon and takes aim at another gangster. Nobody is aimed at by more than one gangster. The gangsters are poor but well trained: each of them can shoot only once, and that shot is always accurate and always deadly.
At some point one of the thugs cannot stand the tension any longer, and the shooting starts.
The director has fixed the order in which the gangsters pull their triggers. Gangster i shoots at gangster pi at exactly the moment ti, unless gangster i has already been killed by then. A gangster is killed at exactly the moment someone shoots at him.
The director wants to know how many gangsters are alive at the end of the scene. Byteberg is not yet sure about the order in which the gangsters shoot. From time to time he orders one of the values ti to be changed. After every such change he wants to know how many gangsters survive under the new order, taking into account all changes made so far.
The first line contains one integer n (2≤n≤200,000), the number of gangsters in the scene. The second line contains n integers p1,p2,…,pn (1≤pi≤n, pi=i, and pi=pj for i=j), where gangster i aims at gangster pi.
The third line contains n integers u1,u2,…,un (1≤ui≤109) describing the initial shooting order: the initial value of ti is ui.
The fourth line contains one integer q (0≤q≤200,000), the number of changes to t1,…,tn that Byteberg plans. Each of the next q lines describes one change. The i-th of these lines contains two integers ki and vi (1≤ki≤n, 1≤vi≤109), meaning that the i-th change sets tki to vi. The numbers u1,u2,…,un,v1,v2,…,vq are pairwise distinct.
Print exactly q+1 lines. The first line contains the number of gangsters who survive the shooting under the initial order. The i-th of the following q lines contains the number of gangsters who survive when the shooting order is given by t1,…,tn after the first i changes have been applied.