#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef pair<ll, ll> pii;
typedef pair<ld, ld> pld;
ll n;
vector<ll> querry;
ll val[100009];
vector<ll> g[100009];
ll prt[100009];
ll subsz[100009];
ll depth[100009];
void dfs1(int v){
subsz[v] = 1;
for(auto u : g[v]){
prt[u] = v;
depth[u] = depth[v]+1;
dfs1(u);
subsz[v] += subsz[u];
}
}
int eul[100009];
int chain[100009];
int cureul = 0;
void dfs2(int v){
//cout << v << " -- " << cureul << '\n';
eul[v] = cureul++;
if(eul[prt[v]] == eul[v]-1)
chain[v] = chain[prt[v]];
else
chain[v] = v;
int biggest = 0, id = 0;
for(auto u : g[v])
if(subsz[u] > biggest){
biggest = subsz[u];
id = u;
}
if(id != 0)
dfs2(id);
for(auto u : g[v])
if(u != id)
dfs2(u);
}
deque<pll> vec[100009];
ll bit[100009];
void updbit(ll idx, ll val){
++idx;
while(idx <= n+2){
bit[idx] += val;
idx += idx&(-idx);
}
}
ll getbit(ll idx){
++idx;
ll ret = 0;
while(idx > 0){
ret += bit[idx];
idx -= idx&(-idx);
}
return ret;
}
ll gettree(ll idx){
vector<pll> all;
while(idx != 0){
deque<pll> &v = vec[chain[idx]];
ll cnt = depth[idx]-depth[chain[idx]]+1;
vector<pll> cur;
for(auto u : v){
ll sub = min(cnt, u.ff);
cnt -= sub;
cur.pb({sub, u.ss});
if(cnt == 0)
break;
}
reverse(cur.begin(), cur.end());
for(auto u : cur)
all.pb(u);
idx = prt[chain[idx]];
}
ll ret = 0;
for(auto u : all){
ret += u.ff*getbit(u.ss-1);
updbit(u.ss, u.ff);
}
for(auto u : all)
updbit(u.ss, -u.ff);
return ret;
}
void updtree(ll idx, ll val){
while(idx != 0){
deque<pll> &v = vec[chain[idx]];
ll cnt = depth[idx]-depth[chain[idx]]+1;
while(cnt > 0 && !v.empty()){
ll sub = min(cnt, v.front().ff);
cnt -= sub;
v.front().ff -= sub;
if(v.front().ff == 0)
v.pop_front();
}
v.push_front({depth[idx]-depth[chain[idx]]+1, val});
idx = prt[chain[idx]];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> n;
vector<ll> compress;
for(ll i = 1; i <= n; ++i){
cin >> val[i];
compress.pb(val[i]);
}
sort(compress.begin(), compress.end());
compress.resize(unique(compress.begin(), compress.end())-compress.begin());
for(ll i = 1; i <= n; ++i)
val[i] = lower_bound(compress.begin(), compress.end(), val[i])-compress.begin();
for(ll i = 1; i < n; ++i){
ll x, y;
cin >> x >> y;
g[x].pb(y);
querry.pb(y);
}
dfs1(1);
dfs2(1);
//updtree(1, val[1]);
for(auto u : querry){
cout << gettree(prt[u]) << '\n';
updtree(u, val[u]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
70008 KB |
Output is correct |
2 |
Correct |
52 ms |
70008 KB |
Output is correct |
3 |
Correct |
58 ms |
70136 KB |
Output is correct |
4 |
Correct |
60 ms |
70140 KB |
Output is correct |
5 |
Correct |
58 ms |
70264 KB |
Output is correct |
6 |
Correct |
58 ms |
70272 KB |
Output is correct |
7 |
Correct |
59 ms |
70264 KB |
Output is correct |
8 |
Correct |
54 ms |
70136 KB |
Output is correct |
9 |
Correct |
55 ms |
70136 KB |
Output is correct |
10 |
Correct |
54 ms |
70140 KB |
Output is correct |
11 |
Correct |
57 ms |
70264 KB |
Output is correct |
12 |
Correct |
59 ms |
70264 KB |
Output is correct |
13 |
Correct |
59 ms |
70264 KB |
Output is correct |
14 |
Correct |
59 ms |
70264 KB |
Output is correct |
15 |
Correct |
60 ms |
70264 KB |
Output is correct |
16 |
Correct |
58 ms |
70264 KB |
Output is correct |
17 |
Correct |
58 ms |
70136 KB |
Output is correct |
18 |
Correct |
60 ms |
70264 KB |
Output is correct |
19 |
Correct |
59 ms |
70264 KB |
Output is correct |
20 |
Correct |
58 ms |
70264 KB |
Output is correct |
21 |
Correct |
58 ms |
70264 KB |
Output is correct |
22 |
Correct |
59 ms |
70264 KB |
Output is correct |
23 |
Correct |
59 ms |
70264 KB |
Output is correct |
24 |
Correct |
61 ms |
70136 KB |
Output is correct |
25 |
Correct |
58 ms |
70264 KB |
Output is correct |
26 |
Correct |
61 ms |
70264 KB |
Output is correct |
27 |
Correct |
59 ms |
70264 KB |
Output is correct |
28 |
Correct |
58 ms |
70268 KB |
Output is correct |
29 |
Correct |
58 ms |
70264 KB |
Output is correct |
30 |
Correct |
60 ms |
70264 KB |
Output is correct |
31 |
Correct |
59 ms |
70264 KB |
Output is correct |
32 |
Correct |
67 ms |
70264 KB |
Output is correct |
33 |
Correct |
59 ms |
70264 KB |
Output is correct |
34 |
Correct |
58 ms |
70248 KB |
Output is correct |
35 |
Correct |
59 ms |
70248 KB |
Output is correct |
36 |
Correct |
58 ms |
70264 KB |
Output is correct |
37 |
Correct |
62 ms |
70248 KB |
Output is correct |
38 |
Correct |
58 ms |
70264 KB |
Output is correct |
39 |
Correct |
59 ms |
70264 KB |
Output is correct |
40 |
Correct |
59 ms |
70248 KB |
Output is correct |
41 |
Correct |
61 ms |
70312 KB |
Output is correct |
42 |
Correct |
57 ms |
70248 KB |
Output is correct |
43 |
Correct |
58 ms |
70268 KB |
Output is correct |
44 |
Correct |
59 ms |
70136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
70008 KB |
Output is correct |
2 |
Correct |
52 ms |
70008 KB |
Output is correct |
3 |
Correct |
58 ms |
70136 KB |
Output is correct |
4 |
Correct |
60 ms |
70140 KB |
Output is correct |
5 |
Correct |
58 ms |
70264 KB |
Output is correct |
6 |
Correct |
58 ms |
70272 KB |
Output is correct |
7 |
Correct |
59 ms |
70264 KB |
Output is correct |
8 |
Correct |
54 ms |
70136 KB |
Output is correct |
9 |
Correct |
55 ms |
70136 KB |
Output is correct |
10 |
Correct |
54 ms |
70140 KB |
Output is correct |
11 |
Correct |
57 ms |
70264 KB |
Output is correct |
12 |
Correct |
59 ms |
70264 KB |
Output is correct |
13 |
Correct |
59 ms |
70264 KB |
Output is correct |
14 |
Correct |
59 ms |
70264 KB |
Output is correct |
15 |
Correct |
60 ms |
70264 KB |
Output is correct |
16 |
Correct |
58 ms |
70264 KB |
Output is correct |
17 |
Correct |
58 ms |
70136 KB |
Output is correct |
18 |
Correct |
60 ms |
70264 KB |
Output is correct |
19 |
Correct |
59 ms |
70264 KB |
Output is correct |
20 |
Correct |
58 ms |
70264 KB |
Output is correct |
21 |
Correct |
58 ms |
70264 KB |
Output is correct |
22 |
Correct |
59 ms |
70264 KB |
Output is correct |
23 |
Correct |
59 ms |
70264 KB |
Output is correct |
24 |
Correct |
61 ms |
70136 KB |
Output is correct |
25 |
Correct |
58 ms |
70264 KB |
Output is correct |
26 |
Correct |
61 ms |
70264 KB |
Output is correct |
27 |
Correct |
59 ms |
70264 KB |
Output is correct |
28 |
Correct |
58 ms |
70268 KB |
Output is correct |
29 |
Correct |
58 ms |
70264 KB |
Output is correct |
30 |
Correct |
60 ms |
70264 KB |
Output is correct |
31 |
Correct |
59 ms |
70264 KB |
Output is correct |
32 |
Correct |
67 ms |
70264 KB |
Output is correct |
33 |
Correct |
59 ms |
70264 KB |
Output is correct |
34 |
Correct |
58 ms |
70248 KB |
Output is correct |
35 |
Correct |
59 ms |
70248 KB |
Output is correct |
36 |
Correct |
58 ms |
70264 KB |
Output is correct |
37 |
Correct |
62 ms |
70248 KB |
Output is correct |
38 |
Correct |
58 ms |
70264 KB |
Output is correct |
39 |
Correct |
59 ms |
70264 KB |
Output is correct |
40 |
Correct |
59 ms |
70248 KB |
Output is correct |
41 |
Correct |
61 ms |
70312 KB |
Output is correct |
42 |
Correct |
57 ms |
70248 KB |
Output is correct |
43 |
Correct |
58 ms |
70268 KB |
Output is correct |
44 |
Correct |
59 ms |
70136 KB |
Output is correct |
45 |
Correct |
62 ms |
70392 KB |
Output is correct |
46 |
Correct |
68 ms |
71416 KB |
Output is correct |
47 |
Correct |
66 ms |
71544 KB |
Output is correct |
48 |
Correct |
66 ms |
71416 KB |
Output is correct |
49 |
Correct |
61 ms |
70648 KB |
Output is correct |
50 |
Correct |
59 ms |
70648 KB |
Output is correct |
51 |
Correct |
62 ms |
70796 KB |
Output is correct |
52 |
Correct |
65 ms |
71716 KB |
Output is correct |
53 |
Correct |
61 ms |
71648 KB |
Output is correct |
54 |
Correct |
64 ms |
71672 KB |
Output is correct |
55 |
Correct |
65 ms |
71676 KB |
Output is correct |
56 |
Correct |
64 ms |
71544 KB |
Output is correct |
57 |
Correct |
70 ms |
71420 KB |
Output is correct |
58 |
Correct |
69 ms |
71416 KB |
Output is correct |
59 |
Correct |
68 ms |
71416 KB |
Output is correct |
60 |
Correct |
70 ms |
71544 KB |
Output is correct |
61 |
Correct |
62 ms |
71544 KB |
Output is correct |
62 |
Correct |
64 ms |
71612 KB |
Output is correct |
63 |
Correct |
64 ms |
71544 KB |
Output is correct |
64 |
Correct |
65 ms |
71416 KB |
Output is correct |
65 |
Correct |
64 ms |
71416 KB |
Output is correct |
66 |
Correct |
65 ms |
71544 KB |
Output is correct |
67 |
Correct |
68 ms |
71416 KB |
Output is correct |
68 |
Correct |
57 ms |
70644 KB |
Output is correct |
69 |
Correct |
63 ms |
71672 KB |
Output is correct |
70 |
Correct |
64 ms |
71620 KB |
Output is correct |
71 |
Correct |
63 ms |
71544 KB |
Output is correct |
72 |
Correct |
68 ms |
71544 KB |
Output is correct |
73 |
Correct |
67 ms |
71416 KB |
Output is correct |
74 |
Correct |
62 ms |
71672 KB |
Output is correct |
75 |
Correct |
64 ms |
71548 KB |
Output is correct |
76 |
Correct |
65 ms |
71544 KB |
Output is correct |
77 |
Correct |
64 ms |
71476 KB |
Output is correct |
78 |
Correct |
63 ms |
71420 KB |
Output is correct |
79 |
Correct |
63 ms |
71416 KB |
Output is correct |
80 |
Correct |
64 ms |
71416 KB |
Output is correct |
81 |
Correct |
63 ms |
71420 KB |
Output is correct |
82 |
Correct |
65 ms |
71544 KB |
Output is correct |
83 |
Correct |
64 ms |
71416 KB |
Output is correct |
84 |
Correct |
65 ms |
71536 KB |
Output is correct |
85 |
Correct |
64 ms |
71416 KB |
Output is correct |
86 |
Correct |
63 ms |
71416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
70008 KB |
Output is correct |
2 |
Correct |
52 ms |
70008 KB |
Output is correct |
3 |
Correct |
58 ms |
70136 KB |
Output is correct |
4 |
Correct |
60 ms |
70140 KB |
Output is correct |
5 |
Correct |
58 ms |
70264 KB |
Output is correct |
6 |
Correct |
58 ms |
70272 KB |
Output is correct |
7 |
Correct |
59 ms |
70264 KB |
Output is correct |
8 |
Correct |
54 ms |
70136 KB |
Output is correct |
9 |
Correct |
55 ms |
70136 KB |
Output is correct |
10 |
Correct |
54 ms |
70140 KB |
Output is correct |
11 |
Correct |
57 ms |
70264 KB |
Output is correct |
12 |
Correct |
59 ms |
70264 KB |
Output is correct |
13 |
Correct |
59 ms |
70264 KB |
Output is correct |
14 |
Correct |
59 ms |
70264 KB |
Output is correct |
15 |
Correct |
60 ms |
70264 KB |
Output is correct |
16 |
Correct |
58 ms |
70264 KB |
Output is correct |
17 |
Correct |
58 ms |
70136 KB |
Output is correct |
18 |
Correct |
60 ms |
70264 KB |
Output is correct |
19 |
Correct |
59 ms |
70264 KB |
Output is correct |
20 |
Correct |
58 ms |
70264 KB |
Output is correct |
21 |
Correct |
58 ms |
70264 KB |
Output is correct |
22 |
Correct |
59 ms |
70264 KB |
Output is correct |
23 |
Correct |
59 ms |
70264 KB |
Output is correct |
24 |
Correct |
61 ms |
70136 KB |
Output is correct |
25 |
Correct |
58 ms |
70264 KB |
Output is correct |
26 |
Correct |
61 ms |
70264 KB |
Output is correct |
27 |
Correct |
59 ms |
70264 KB |
Output is correct |
28 |
Correct |
58 ms |
70268 KB |
Output is correct |
29 |
Correct |
58 ms |
70264 KB |
Output is correct |
30 |
Correct |
60 ms |
70264 KB |
Output is correct |
31 |
Correct |
59 ms |
70264 KB |
Output is correct |
32 |
Correct |
67 ms |
70264 KB |
Output is correct |
33 |
Correct |
59 ms |
70264 KB |
Output is correct |
34 |
Correct |
58 ms |
70248 KB |
Output is correct |
35 |
Correct |
59 ms |
70248 KB |
Output is correct |
36 |
Correct |
58 ms |
70264 KB |
Output is correct |
37 |
Correct |
62 ms |
70248 KB |
Output is correct |
38 |
Correct |
58 ms |
70264 KB |
Output is correct |
39 |
Correct |
59 ms |
70264 KB |
Output is correct |
40 |
Correct |
59 ms |
70248 KB |
Output is correct |
41 |
Correct |
61 ms |
70312 KB |
Output is correct |
42 |
Correct |
57 ms |
70248 KB |
Output is correct |
43 |
Correct |
58 ms |
70268 KB |
Output is correct |
44 |
Correct |
59 ms |
70136 KB |
Output is correct |
45 |
Correct |
62 ms |
70392 KB |
Output is correct |
46 |
Correct |
68 ms |
71416 KB |
Output is correct |
47 |
Correct |
66 ms |
71544 KB |
Output is correct |
48 |
Correct |
66 ms |
71416 KB |
Output is correct |
49 |
Correct |
61 ms |
70648 KB |
Output is correct |
50 |
Correct |
59 ms |
70648 KB |
Output is correct |
51 |
Correct |
62 ms |
70796 KB |
Output is correct |
52 |
Correct |
65 ms |
71716 KB |
Output is correct |
53 |
Correct |
61 ms |
71648 KB |
Output is correct |
54 |
Correct |
64 ms |
71672 KB |
Output is correct |
55 |
Correct |
65 ms |
71676 KB |
Output is correct |
56 |
Correct |
64 ms |
71544 KB |
Output is correct |
57 |
Correct |
70 ms |
71420 KB |
Output is correct |
58 |
Correct |
69 ms |
71416 KB |
Output is correct |
59 |
Correct |
68 ms |
71416 KB |
Output is correct |
60 |
Correct |
70 ms |
71544 KB |
Output is correct |
61 |
Correct |
62 ms |
71544 KB |
Output is correct |
62 |
Correct |
64 ms |
71612 KB |
Output is correct |
63 |
Correct |
64 ms |
71544 KB |
Output is correct |
64 |
Correct |
65 ms |
71416 KB |
Output is correct |
65 |
Correct |
64 ms |
71416 KB |
Output is correct |
66 |
Correct |
65 ms |
71544 KB |
Output is correct |
67 |
Correct |
68 ms |
71416 KB |
Output is correct |
68 |
Correct |
57 ms |
70644 KB |
Output is correct |
69 |
Correct |
63 ms |
71672 KB |
Output is correct |
70 |
Correct |
64 ms |
71620 KB |
Output is correct |
71 |
Correct |
63 ms |
71544 KB |
Output is correct |
72 |
Correct |
68 ms |
71544 KB |
Output is correct |
73 |
Correct |
67 ms |
71416 KB |
Output is correct |
74 |
Correct |
62 ms |
71672 KB |
Output is correct |
75 |
Correct |
64 ms |
71548 KB |
Output is correct |
76 |
Correct |
65 ms |
71544 KB |
Output is correct |
77 |
Correct |
64 ms |
71476 KB |
Output is correct |
78 |
Correct |
63 ms |
71420 KB |
Output is correct |
79 |
Correct |
63 ms |
71416 KB |
Output is correct |
80 |
Correct |
64 ms |
71416 KB |
Output is correct |
81 |
Correct |
63 ms |
71420 KB |
Output is correct |
82 |
Correct |
65 ms |
71544 KB |
Output is correct |
83 |
Correct |
64 ms |
71416 KB |
Output is correct |
84 |
Correct |
65 ms |
71536 KB |
Output is correct |
85 |
Correct |
64 ms |
71416 KB |
Output is correct |
86 |
Correct |
63 ms |
71416 KB |
Output is correct |
87 |
Correct |
82 ms |
73720 KB |
Output is correct |
88 |
Correct |
140 ms |
81012 KB |
Output is correct |
89 |
Correct |
453 ms |
106476 KB |
Output is correct |
90 |
Correct |
409 ms |
106472 KB |
Output is correct |
91 |
Correct |
411 ms |
106496 KB |
Output is correct |
92 |
Correct |
182 ms |
86508 KB |
Output is correct |
93 |
Correct |
184 ms |
86508 KB |
Output is correct |
94 |
Correct |
185 ms |
86380 KB |
Output is correct |
95 |
Correct |
223 ms |
111084 KB |
Output is correct |
96 |
Correct |
237 ms |
111468 KB |
Output is correct |
97 |
Correct |
235 ms |
111468 KB |
Output is correct |
98 |
Correct |
235 ms |
111468 KB |
Output is correct |
99 |
Correct |
237 ms |
108648 KB |
Output is correct |
100 |
Correct |
486 ms |
98412 KB |
Output is correct |
101 |
Correct |
525 ms |
98668 KB |
Output is correct |
102 |
Correct |
545 ms |
98668 KB |
Output is correct |
103 |
Correct |
547 ms |
98540 KB |
Output is correct |
104 |
Correct |
254 ms |
108520 KB |
Output is correct |
105 |
Correct |
263 ms |
108520 KB |
Output is correct |
106 |
Correct |
267 ms |
108524 KB |
Output is correct |
107 |
Correct |
371 ms |
104940 KB |
Output is correct |
108 |
Correct |
381 ms |
105196 KB |
Output is correct |
109 |
Correct |
403 ms |
105576 KB |
Output is correct |
110 |
Correct |
165 ms |
85096 KB |
Output is correct |
111 |
Correct |
229 ms |
111080 KB |
Output is correct |
112 |
Correct |
225 ms |
110184 KB |
Output is correct |
113 |
Correct |
217 ms |
107116 KB |
Output is correct |
114 |
Correct |
483 ms |
98388 KB |
Output is correct |
115 |
Correct |
499 ms |
97320 KB |
Output is correct |
116 |
Correct |
230 ms |
107244 KB |
Output is correct |
117 |
Correct |
244 ms |
106988 KB |
Output is correct |
118 |
Correct |
243 ms |
106604 KB |
Output is correct |
119 |
Correct |
237 ms |
106420 KB |
Output is correct |
120 |
Correct |
219 ms |
105836 KB |
Output is correct |
121 |
Correct |
220 ms |
105708 KB |
Output is correct |
122 |
Correct |
233 ms |
105448 KB |
Output is correct |
123 |
Correct |
262 ms |
106988 KB |
Output is correct |
124 |
Correct |
270 ms |
106604 KB |
Output is correct |
125 |
Correct |
264 ms |
106472 KB |
Output is correct |
126 |
Correct |
248 ms |
106092 KB |
Output is correct |
127 |
Correct |
247 ms |
105664 KB |
Output is correct |
128 |
Correct |
244 ms |
105324 KB |
Output is correct |