Fun on Tree

시간 제한7초메모리 제한2048 MB

요약
서브트리에 값을 더하고 루트가 바뀌는 질의마다 새 루트까지의 거리에서 황 함량을 뺀 값이 최대인 노드를 찾고, 동점이면 번호가 가장 작은 노드를 출력한다.
난이도

어려움10점 중 9점

유형
트리, 세그먼트 트리, DFS, 누적 합
정답자
아직 제출이 없습니다

문제

Beitou, renowned for its breathtaking beauty and abundant hot springs, attracts countless tourists seeking relaxation and rejuvenation. The region boasts an extraordinary concentration of hot springs and spas, making it a global hotspot for such indulgence. Once a quaint locale where locals sought solace in its natural hot springs, the Beitou Valley has blossomed into a sprawling expanse, now housing over thirty luxurious resorts.

Traditionally, hot springs evoke imagery of volcanic activity and the unmistakable scent of sulfur. However, for some, the latter proves rather unpleasant. You find yourself among those sensitive to the smell of sulfur, which brings us to your current predicament.

Presently visiting Beitou, you've obtained a map of this enchanting place, revealing a conceptual representation as a rooted tree. It's worth noting that Beitou's mystical aura allows for unconventional measurements, even enabling negative distances between nodes. Additionally, each node on the map comes with an associated sulfur content value.

An unexpected revelation leaves you questioning the accuracy of the sulfur content assigned to each location. In order to rectify this, your mission is to align the sulfur content with the newly acquired information. Moreover, you've uncovered intel about the location of the largest volcano in the vicinity. Prioritizing your safety and well-being, your ultimate goal is to pinpoint the spot farthest from this prominent volcano while having the lowest sulfur content. Your chosen evaluation metric for each node's viability is given by the formula d_i−a_id\_i - a\_i, wherein d_id\_i represents the distance between node ii and the largest volcano, and a_ia\_i is the corresponding sulfur content value.

We can describe each piece of new information you acquire with three integers, x_ix\_i, y_iy\_i, and v_iv\_i. It means that all the subtree of y_iy\_i now has v_iv\_i more sulfur (if v_iv\_i is negative, then the sulfur content value decreases). Besides, you also learn that the largest volcano is actually located at x_ix\_i.

Note that the modifications of sulfur value are persistent: when you get another piece of new information, the sulfur modifications from the previous ones still apply.

입력

The first line contains two integers nn and qq (2≤n≤2⋅1052 \leq n \leq 2 \cdot 10^5 and 0≤q≤2⋅1050 \leq q \leq 2 \cdot 10^5): the size of the tree and the number of queries.

The second line contains nn integers a_1,a_2,…,a_na\_1,a\_2,\ldots,a\_n (∣a_i∣≤109|a\_i| \leq 10^9): the sulfur content value of each nodes.

Next, n−1n-1 lines are given. The ii-th of these lines contains two integers p_i+1p\_{i+1} and w_i+1w\_{i+1} (1≤p_i+1≤i1 \leq p\_{i+1} \leq i and ∣w_i+1∣≤109|w\_{i+1}| \leq 10^9): the parent node of vertex i+1i+1 and the distance between p_i+1p\_{i+1} and i+1i+1.

Finally, qq lines are given. Each line contains three integers x_ix\_i, y_iy\_i, and v_iv\_i (1≤x_i,y_i≤n1 \leq x\_i, y\_i \leq n and ∣v_i∣≤109|v\_i| \leq 10^9): the new piece of information you acquired.

출력

For each new piece of information, print a line with two integers s_is\_i and d_id\_i: the index of the node having the largest viability and the viability itself.

If there are multiple answers, please output the one with the minimal node index.

예제3

  1. 예제 1

    입력
    6 6
    1 1 4 5 1 4
    1 5
    2 0
    3 2
    4 1
    5 6
    3 2 -100000
    1 2 100000
    1 1 0
    2 2 66
    3 1 5
    4 4 -3
    
    예상 출력
    6 100005
    6 10
    6 10
    1 4
    1 -1
    1 1
    
  2. 예제 2

    입력
    5 6
    -10 0 2 -4 8
    1 7
    1 1
    2 2
    2 -2
    1 1 100
    2 1 -100
    1 1 0
    4 3 10
    2 5 3
    5 2 2
    
    예상 출력
    4 -87
    1 17
    4 13
    1 19
    1 17
    1 15
    
  3. 예제 3

    입력
    6 3
    0 0 0 0 0 0
    1 10
    1 10
    1 -100
    4 10
    4 11
    1 1 0
    4 1 0
    1 4 1000
    
    예상 출력
    2 10
    6 11
    2 10