408980: GYM103401 I Broken routers

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

I. Broken routerstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

There are $$$n$$$ routers in a network each of which has some packets stored in it and a routing table. Routers look up ip address of the packet in its routing table and send the packet to corresponding router neighbor. If the packet's ip address is not in the routing table then the packet will be send to its default router neighbor.

Unfortunately, all of the $$$n$$$ routers have broken down and their routing table become empty. So they can only send packets to their default router neighbor.

There are $$$a_i$$$ packets stored in $$$ith$$$ router at time slot 0. Each of the $$$n$$$ routers send all the packets stored in it to default router neighbor and remove these packets from itself in every time slot. Routers also store the packets it recieved in each time slot.

As a administrator of the network, you want to know how many packets are there in router $$$x$$$ at time $$$t$$$.

Input

The first line contains an integer $$$n(1 \leq n \leq 10^5)$$$, meaning the number of routers.

The second line contains $$$n$$$ intergers $$$a_1,...,a_n(1 \leq a_i \leq 10^9)$$$.

The third line contains $$$n$$$ intergers $$$neb_1,...,neb_n(1 \leq neb_i \leq n)$$$ where $$$neb_i$$$ represents the default router neighbor of $$$ith$$$ router.

The next line contains an integer $$$Q(1 \leq Q \leq 10^5)$$$, meaning the number of queries.

The next $$$Q$$$ lines each contains two integer $$$x,t(1 \leq x \leq n, 0 \leq t \leq 10^9)$$$, having the same meaning in description.

Output

There are $$$Q$$$ lines in the output, the $$$ith$$$ line contains one interger representing the answer of $$$ith$$$ query.

ExampleInput
7
1 2 3 4 5 6 7
2 3 1 1 4 2 2
5
1 2
4 1
5 0
3 3
6 1
Output
7
5
5
7
0

加入题单

上一题 下一题 算法标签: