Geography of Rivers
시간 제한2초메모리 제한1024 MB
두 강이 합쳐질 때 물이 더 많은 쪽의 이름을 유지하는 이진 병합 트리에서, 각 수원의 물량이 늘어나는 갱신을 처리한 뒤 매번 바다로 흘러가는 최종 강의 이름을 구한다.
문제
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, river sources are given. For each source, you have a quantity of liters of water 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 operations. In each of them, a rain occurred that caused liters more of water to be produced in the source (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 (): the number of river sources.
The second line contains integers (): the number of liters of water that originate in source .
The following lines describe how the rivers join together. In the -th of them, two integers , () indicate that the rivers and join together to form the river (whose volume of water will be the sum of the volumes of and , and whose name will be the name of the one with the largest volume of water). It is guaranteed that the values and are valid, that is, and neither of them has been previously joined in the input.
The next line contains an integer (), the number of operations.
Then lines with the operations follow: the -th line contains two integers and ( and ), meaning that the source now sources liters more of water.
출력
Print, on the first line, the name of the river that initially flows into the sea. Then print lines: after each operation, the name of the river that flows into the sea.