Geography of Rivers

시간 제한2초메모리 제한1024 MB

요약
두 강이 합쳐질 때 물이 더 많은 쪽의 이름을 유지하는 이진 병합 트리에서, 각 수원의 물량이 늘어나는 갱신을 처리한 뒤 매번 바다로 흘러가는 최종 강의 이름을 구한다.
난이도

어려움10점 중 8점

유형
트리, 동적 계획법, 정렬, 구현
정답자
아직 제출이 없습니다

문제

When studying the geography of the world’s rivers, you may ask yourself: when two rivers join together, who chooses the name of the river that results from this junction? In fact, the answer is simple: when two rivers join together, the name of the river that had the largest volume of water becomes the name. Given that all rivers eventually join together and flow into the sea, an interesting problem is to calculate, given the name of each source, the name of the final river that flows into the sea.

Formally, NN river sources are given. For each source, you have a quantity of liters of water l_il\_i that originates from it. Furthermore, pairs of rivers meet (like a binary tree), until they all join and flow into the sea. When two rivers meet, the quantity of liters of water is added together, and the name of the river becomes the name of the river that had more water, or, in case of a tie, the one with the lowest index. The initial name of each source is its index.

What you want to know is the name of the river that eventually flows into the sea. However, it’s rainy season! You need to process QQ operations. In each of them, a rain occurred that caused q_iq\_i liters more of water to be produced in the source n_in\_i (and this will be maintained for future operations). After each operation, calculate the name of the river that flows into the sea.

입력

The first line contains an integer NN (1≤N≤1051 ≤ N ≤ 10^5): the number of river sources.

The second line contains NN integers l_il\_i (1≤l_i≤1091 ≤ l\_i ≤ 10^9): the number of liters of water that originate in source ii.

The following N−1N - 1 lines describe how the rivers join together. In the ii-th of them, two integers a_ia\_i, b_ib\_i (1≤a_i,b_i<N+i1 ≤ a\_i , b\_i < N +i) indicate that the rivers a_ia\_i and b_ib\_i join together to form the river N+iN +i (whose volume of water will be the sum of the volumes of a_ia\_i and b_ib\_i, and whose name will be the name of the one with the largest volume of water). It is guaranteed that the values a_ia\_i and b_ib\_i are valid, that is, a_i≠b_ia\_i \ne b\_i and neither of them has been previously joined in the input.

The next line contains an integer QQ (1≤Q≤1051 ≤ Q ≤ 10^5), the number of operations.

Then QQ lines with the operations follow: the ii-th line contains two integers n_in\_i and q_iq\_i (1≤n_i≤N1 ≤ n\_i ≤ N and 1≤q_i≤1091 ≤ q\_i ≤ 10^9), meaning that the source n_in\_i now sources q_iq\_i liters more of water.

출력

Print, on the first line, the name of the river that initially flows into the sea. Then print QQ lines: after each operation, the name of the river that flows into the sea.

예제1

  1. 예제 1

    입력
    3
    1 4 4
    1 2
    4 3
    2
    3 2
    1 2
    
    예상 출력
    2
    3
    2