#P3261. [JLOI2015] 城池攻占

[JLOI2015] 城池攻占

Description

Xiao Mingming recently got a new board game. In the game, you need to use mm knights to conquer nn cities.

The nn cities are numbered from 11 to nn. Except for city 11, each city ii is governed by another city fif_i with fi<if_i<i. In other words, all cities form a rooted tree.

The mm knights are numbered from 11 to mm, where knight ii has initial combat power sis_i, and his first target city is cic_i.

Each city has a defense value hih_i. If a knight's combat power is greater than or equal to the city's defense value, then the knight can capture that city; otherwise, the capture fails and the knight will fall at that city. After capturing a city, the knight's combat power will change, and then he continues to attack the city that governs this city (its parent), until he captures city 11, or dies.

Except for city 11, each city ii provides a combat-power change parameter (ai,vi)(a_i, v_i). If ai=0a_i=0, after capturing city ii, the knight's combat power increases by viv_i; if ai=1a_i=1, after capturing city ii, the combat power is multiplied by viv_i.

Note that each knight is simulated independently. That is, when a knight attacks a city, regardless of the outcome, it does not affect the result of other knights attacking this city.

Now the problem is: for each city, output how many knights die here; and for each knight, output how many cities he captures.

Input Format

The first line contains two positive integers n,mn,m, the numbers of cities and knights.

The second line contains nn integers, where the ii-th number is hih_i, the defense value of city ii.

Lines 33 to n+1n+1: for each city ii from 22 to nn, line i+1i+1 contains three integers fi,ai,vif_i,a_i,v_i, denoting the governing (parent) city of city ii and the two combat-power change parameters.

Lines n+2n+2 to n+m+1n+m+1: for each knight ii from 11 to mm, line n+1+in+1+i contains two integers si,cis_i,c_i, the initial combat power and the first city to attack.

Output Format

Output n+mn+m lines, each containing a nonnegative integer. The first nn lines give, for cities 11 to nn, the number of knights who die there; the last mm lines give, for knights 11 to mm, the number of cities they capture.

5 5
50 20 10 10 30
1 1 2
2 0 5
2 0 -10
1 0 10
20 2
10 3
40 4
20 4
35 5
2
2
0
0
0
1
1
3
1
1

Hint

For 100%100\% of the testdata, 1n,m3×1051\le n,m\le 3\times 10^5, 1018hi,vi,si1018-10^{18}\le h_i,v_i,s_i\le 10^{18}, 1fi<i1\le f_i<i, 1cin1\le c_i\le n, ai{0,1}a_i\in\{0,1\}. It is guaranteed that when ai=1a_i=1, vi>0v_i>0, and that at any time the absolute value of a knight's combat power does not exceed 101810^{18}.

Translated by ChatGPT 5