1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
| #include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <vector>
typedef long long ll;
using namespace std;
namespace fast_io{
//...
}using namespace fast_io;
const int MAXN = 310000;
namespace MH{
int l[MAXN],r[MAXN],d[MAXN];
ll v[MAXN],addn[MAXN],muln[MAXN];
inline void add(int x,ll val){
if(!x) return;
v[x]+=val,addn[x]+=val;
}
inline void mul(int x,ll val){
if(!x) return;
v[x]*=val,addn[x]*=val,muln[x] *= val;
}
inline void push_down(int x){
if(!x) return;
if(muln[x]!=1){
mul(l[x],muln[x]),mul(r[x],muln[x]);
muln[x] = 1;
}
if(addn[x]){
add(l[x],addn[x]),add(r[x],addn[x]);
addn[x] = 0;
}
}
inline int merge(int x,int y){
if(x == y) return x;
if(!x || !y) return x+y;
if(v[x] > v[y]) swap(x,y);
push_down(x);
r[x] = merge(r[x],y);
if(d[l[x]] < d[r[x]]) swap(l[x],r[x]);
d[x] = d[r[x]] + 1;
return x;
}
inline ll top(int x){
return v[x];
}
inline int del(int x){
push_down(x);
int t = merge(l[x],r[x]);
l[x] = r[x] = d[x] = 0;
return t;
}
inline void init(int n,ll *num){
for(int i = 1;i<=n;i++)
v[i] = num[i],l[i] = r[i] = d[i] = addn[i] = 0,muln[i] = 1;
}
}
int n,m,root;
ll h[MAXN];
int f[MAXN],a[MAXN];ll v[MAXN];
ll s[MAXN];int c[MAXN],dep[MAXN];
int ans1[MAXN],ans2[MAXN];
vector<int> edge[MAXN];
vector<int> st[MAXN];
void init(){
read(n),read(m);
for(int i = 1;i<=n;i++)
read(h[i]);
for(int i = 2;i<=n;i++){
read(f[i]),read(a[i]),read(v[i]);
edge[f[i]].push_back(i);
}
for(int i = 1;i<=m;i++){
read(s[i]),read(c[i]);
st[c[i]].push_back(i);
}
root = m+1;
edge[root].push_back(1);
h[root] = (long long)(1e18);
MH::init(m,s);
}
int dfs(int nown){
int ans = 0,tmp = 0;
for(int i = 0;i < edge[nown].size();i++){
int v = edge[nown][i];
dep[v] = dep[nown] + 1;
tmp = dfs(v);
ans = MH::merge(ans,tmp);
}
for(int i = 0;i<st[nown].size();i++)
ans = MH::merge(st[nown][i],ans);
while(MH::top(ans) < h[nown] && ans!=0){
ans1[nown]++;
tmp = ans;
ans2[tmp] = dep[c[tmp]] - dep[nown];
ans = MH::del(ans);
}
if(a[nown] == 0)
MH::add(ans,v[nown]);
else if(a[nown] == 1)
MH::mul(ans,v[nown]);
return ans;
}
void output(){
for(int i = 1;i<=n;i++)
print(ans1[i]),print('\n');
for(int i = 1;i<=m;i++)
print(ans2[i]),print('\n');
}
signed main(){
init();
dfs(root);
output();
flush();
return 0;
}
|