#include <bits/stdc++.h>
using namespace std;
int N, M;
int arr[100005];
int a[100005], b[100005];
vector<int> graph[100005];
int par[100005], top[100005], sz[100005], dep[100005], id[100005], hvy[100005], rng[100005], tim;
int bit[100005];
vector<int> cmp;
set<pair<int, int>> st;
void dfs(int n){
sz[n] = 1;
for(int e : graph[n]){
par[e] = n;
dep[e] = dep[n] + 1;
dfs(e);
sz[n] = sz[e] + 1;
hvy[n] = sz[hvy[n]] < sz[e] ? e : hvy[n];
}
}
void hld(int n, int t){
top[n] = t;
id[n] = ++tim;
rng[id[t]] = tim;
if(hvy[n]){
hld(hvy[n], t);
}
for(int e : graph[n]){
if(hvy[n] != e){
hld(e, e);
}
}
}
void upd(int n, int v){
while(n <= M){
bit[n] = (v ? bit[n] + v : 0);
n += n&-n;
}
}
int sum(int n){
int ret = 0;
while(n){
ret += bit[n];
n -= n&-n;
}
return ret;
}
long long query(int n){
vector<int> clr;
int value = arr[n];
long long ret = 0;
bool fst = 1;
while(n){
//cout << n << " " << par[top[n]] << endl;
vector<pair<int, int>> v;
vector<pair<int, int>> rem;
int lft = id[top[n]], rht = id[n];
if(fst){
rht = id[par[n]];
}
int lst = lft-1;
for(auto it = st.lower_bound({lft, 0}); it != st.end() && lft <= rht; it++){
//cout << st.size() << endl;
auto p = *it;
if(p.first <= rht){
rem.push_back(p);
}
int amt = min(p.first, rht) - lst;
v.push_back({p.second, amt});
clr.push_back(p.second);
if(p.first >= rht){
break;
}
lst = min(p.first, rht);
}
reverse(v.begin(), v.end());
for(auto p : v){
ret += sum(p.first-1)*p.second;
upd(p.first, p.second);
}
for(auto p : rem){
st.erase(p);
}
if(fst){
/*
if(top[n] == n){
st.insert({id[par[n]], value});
}
*/
st.insert({id[n], value});
}
else{
st.insert({rht, value});
}
n = par[top[n]];
fst = 0;
}
for(int n : clr){
upd(n, 0);
}
return ret;
}
int main(){
cin.sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N;
for(int i = 1; i<=N; i++){
cin >> arr[i];
cmp.push_back(arr[i]);
}
sort(cmp.begin(), cmp.end());
cmp.erase(unique(cmp.begin(), cmp.end()), cmp.end());
for(int i = 1; i<=N; i++){
arr[i] = lower_bound(cmp.begin(), cmp.end(), arr[i]) - cmp.begin() + 1;
}
M = cmp.size();
for(int i = 1; i<N; i++){
cin >> a[i] >> b[i];
graph[a[i]].push_back(b[i]);
}
dfs(1);
hld(1, 1);
st.insert({1, arr[1]});
for(int i = 1; i<N; i++){
cout << query(b[i]) << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2816 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Correct |
7 ms |
2816 KB |
Output is correct |
6 |
Correct |
7 ms |
2816 KB |
Output is correct |
7 |
Correct |
7 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2816 KB |
Output is correct |
9 |
Correct |
6 ms |
2816 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
7 ms |
2816 KB |
Output is correct |
12 |
Correct |
7 ms |
2816 KB |
Output is correct |
13 |
Correct |
6 ms |
2816 KB |
Output is correct |
14 |
Correct |
6 ms |
2816 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
7 ms |
2816 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2816 KB |
Output is correct |
20 |
Correct |
6 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
7 ms |
2816 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
6 ms |
2816 KB |
Output is correct |
27 |
Correct |
6 ms |
2816 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2816 KB |
Output is correct |
30 |
Correct |
7 ms |
2816 KB |
Output is correct |
31 |
Correct |
7 ms |
2816 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
2816 KB |
Output is correct |
34 |
Correct |
6 ms |
2816 KB |
Output is correct |
35 |
Correct |
6 ms |
2816 KB |
Output is correct |
36 |
Correct |
6 ms |
2816 KB |
Output is correct |
37 |
Correct |
7 ms |
2816 KB |
Output is correct |
38 |
Correct |
6 ms |
2816 KB |
Output is correct |
39 |
Correct |
6 ms |
2816 KB |
Output is correct |
40 |
Correct |
6 ms |
2816 KB |
Output is correct |
41 |
Correct |
6 ms |
2816 KB |
Output is correct |
42 |
Correct |
6 ms |
2816 KB |
Output is correct |
43 |
Correct |
6 ms |
2816 KB |
Output is correct |
44 |
Correct |
6 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2816 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Correct |
7 ms |
2816 KB |
Output is correct |
6 |
Correct |
7 ms |
2816 KB |
Output is correct |
7 |
Correct |
7 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2816 KB |
Output is correct |
9 |
Correct |
6 ms |
2816 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
7 ms |
2816 KB |
Output is correct |
12 |
Correct |
7 ms |
2816 KB |
Output is correct |
13 |
Correct |
6 ms |
2816 KB |
Output is correct |
14 |
Correct |
6 ms |
2816 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
7 ms |
2816 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2816 KB |
Output is correct |
20 |
Correct |
6 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
7 ms |
2816 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
6 ms |
2816 KB |
Output is correct |
27 |
Correct |
6 ms |
2816 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2816 KB |
Output is correct |
30 |
Correct |
7 ms |
2816 KB |
Output is correct |
31 |
Correct |
7 ms |
2816 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
2816 KB |
Output is correct |
34 |
Correct |
6 ms |
2816 KB |
Output is correct |
35 |
Correct |
6 ms |
2816 KB |
Output is correct |
36 |
Correct |
6 ms |
2816 KB |
Output is correct |
37 |
Correct |
7 ms |
2816 KB |
Output is correct |
38 |
Correct |
6 ms |
2816 KB |
Output is correct |
39 |
Correct |
6 ms |
2816 KB |
Output is correct |
40 |
Correct |
6 ms |
2816 KB |
Output is correct |
41 |
Correct |
6 ms |
2816 KB |
Output is correct |
42 |
Correct |
6 ms |
2816 KB |
Output is correct |
43 |
Correct |
6 ms |
2816 KB |
Output is correct |
44 |
Correct |
6 ms |
2816 KB |
Output is correct |
45 |
Correct |
9 ms |
2944 KB |
Output is correct |
46 |
Correct |
18 ms |
3200 KB |
Output is correct |
47 |
Correct |
19 ms |
3200 KB |
Output is correct |
48 |
Correct |
18 ms |
3200 KB |
Output is correct |
49 |
Correct |
9 ms |
3328 KB |
Output is correct |
50 |
Correct |
9 ms |
3328 KB |
Output is correct |
51 |
Correct |
9 ms |
3328 KB |
Output is correct |
52 |
Correct |
12 ms |
3456 KB |
Output is correct |
53 |
Correct |
10 ms |
3456 KB |
Output is correct |
54 |
Correct |
10 ms |
3456 KB |
Output is correct |
55 |
Correct |
13 ms |
3456 KB |
Output is correct |
56 |
Correct |
10 ms |
3328 KB |
Output is correct |
57 |
Correct |
20 ms |
3196 KB |
Output is correct |
58 |
Correct |
21 ms |
3200 KB |
Output is correct |
59 |
Correct |
21 ms |
3328 KB |
Output is correct |
60 |
Correct |
21 ms |
3328 KB |
Output is correct |
61 |
Correct |
11 ms |
3328 KB |
Output is correct |
62 |
Correct |
11 ms |
3328 KB |
Output is correct |
63 |
Correct |
11 ms |
3328 KB |
Output is correct |
64 |
Correct |
18 ms |
3200 KB |
Output is correct |
65 |
Correct |
18 ms |
3200 KB |
Output is correct |
66 |
Correct |
20 ms |
3148 KB |
Output is correct |
67 |
Correct |
18 ms |
3200 KB |
Output is correct |
68 |
Correct |
9 ms |
3328 KB |
Output is correct |
69 |
Correct |
10 ms |
3456 KB |
Output is correct |
70 |
Correct |
10 ms |
3456 KB |
Output is correct |
71 |
Correct |
10 ms |
3200 KB |
Output is correct |
72 |
Correct |
20 ms |
3200 KB |
Output is correct |
73 |
Correct |
20 ms |
3200 KB |
Output is correct |
74 |
Correct |
10 ms |
3328 KB |
Output is correct |
75 |
Correct |
11 ms |
3200 KB |
Output is correct |
76 |
Correct |
11 ms |
3200 KB |
Output is correct |
77 |
Correct |
11 ms |
3200 KB |
Output is correct |
78 |
Correct |
10 ms |
3200 KB |
Output is correct |
79 |
Correct |
10 ms |
3200 KB |
Output is correct |
80 |
Correct |
11 ms |
3200 KB |
Output is correct |
81 |
Correct |
11 ms |
3200 KB |
Output is correct |
82 |
Correct |
12 ms |
3200 KB |
Output is correct |
83 |
Correct |
12 ms |
3200 KB |
Output is correct |
84 |
Correct |
11 ms |
3200 KB |
Output is correct |
85 |
Correct |
11 ms |
3200 KB |
Output is correct |
86 |
Correct |
11 ms |
3200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2816 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
6 ms |
2816 KB |
Output is correct |
4 |
Correct |
6 ms |
2816 KB |
Output is correct |
5 |
Correct |
7 ms |
2816 KB |
Output is correct |
6 |
Correct |
7 ms |
2816 KB |
Output is correct |
7 |
Correct |
7 ms |
2816 KB |
Output is correct |
8 |
Correct |
6 ms |
2816 KB |
Output is correct |
9 |
Correct |
6 ms |
2816 KB |
Output is correct |
10 |
Correct |
6 ms |
2816 KB |
Output is correct |
11 |
Correct |
7 ms |
2816 KB |
Output is correct |
12 |
Correct |
7 ms |
2816 KB |
Output is correct |
13 |
Correct |
6 ms |
2816 KB |
Output is correct |
14 |
Correct |
6 ms |
2816 KB |
Output is correct |
15 |
Correct |
7 ms |
2816 KB |
Output is correct |
16 |
Correct |
7 ms |
2816 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
7 ms |
2816 KB |
Output is correct |
19 |
Correct |
6 ms |
2816 KB |
Output is correct |
20 |
Correct |
6 ms |
2816 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2816 KB |
Output is correct |
23 |
Correct |
7 ms |
2816 KB |
Output is correct |
24 |
Correct |
7 ms |
2816 KB |
Output is correct |
25 |
Correct |
7 ms |
2816 KB |
Output is correct |
26 |
Correct |
6 ms |
2816 KB |
Output is correct |
27 |
Correct |
6 ms |
2816 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2816 KB |
Output is correct |
30 |
Correct |
7 ms |
2816 KB |
Output is correct |
31 |
Correct |
7 ms |
2816 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
2816 KB |
Output is correct |
34 |
Correct |
6 ms |
2816 KB |
Output is correct |
35 |
Correct |
6 ms |
2816 KB |
Output is correct |
36 |
Correct |
6 ms |
2816 KB |
Output is correct |
37 |
Correct |
7 ms |
2816 KB |
Output is correct |
38 |
Correct |
6 ms |
2816 KB |
Output is correct |
39 |
Correct |
6 ms |
2816 KB |
Output is correct |
40 |
Correct |
6 ms |
2816 KB |
Output is correct |
41 |
Correct |
6 ms |
2816 KB |
Output is correct |
42 |
Correct |
6 ms |
2816 KB |
Output is correct |
43 |
Correct |
6 ms |
2816 KB |
Output is correct |
44 |
Correct |
6 ms |
2816 KB |
Output is correct |
45 |
Correct |
9 ms |
2944 KB |
Output is correct |
46 |
Correct |
18 ms |
3200 KB |
Output is correct |
47 |
Correct |
19 ms |
3200 KB |
Output is correct |
48 |
Correct |
18 ms |
3200 KB |
Output is correct |
49 |
Correct |
9 ms |
3328 KB |
Output is correct |
50 |
Correct |
9 ms |
3328 KB |
Output is correct |
51 |
Correct |
9 ms |
3328 KB |
Output is correct |
52 |
Correct |
12 ms |
3456 KB |
Output is correct |
53 |
Correct |
10 ms |
3456 KB |
Output is correct |
54 |
Correct |
10 ms |
3456 KB |
Output is correct |
55 |
Correct |
13 ms |
3456 KB |
Output is correct |
56 |
Correct |
10 ms |
3328 KB |
Output is correct |
57 |
Correct |
20 ms |
3196 KB |
Output is correct |
58 |
Correct |
21 ms |
3200 KB |
Output is correct |
59 |
Correct |
21 ms |
3328 KB |
Output is correct |
60 |
Correct |
21 ms |
3328 KB |
Output is correct |
61 |
Correct |
11 ms |
3328 KB |
Output is correct |
62 |
Correct |
11 ms |
3328 KB |
Output is correct |
63 |
Correct |
11 ms |
3328 KB |
Output is correct |
64 |
Correct |
18 ms |
3200 KB |
Output is correct |
65 |
Correct |
18 ms |
3200 KB |
Output is correct |
66 |
Correct |
20 ms |
3148 KB |
Output is correct |
67 |
Correct |
18 ms |
3200 KB |
Output is correct |
68 |
Correct |
9 ms |
3328 KB |
Output is correct |
69 |
Correct |
10 ms |
3456 KB |
Output is correct |
70 |
Correct |
10 ms |
3456 KB |
Output is correct |
71 |
Correct |
10 ms |
3200 KB |
Output is correct |
72 |
Correct |
20 ms |
3200 KB |
Output is correct |
73 |
Correct |
20 ms |
3200 KB |
Output is correct |
74 |
Correct |
10 ms |
3328 KB |
Output is correct |
75 |
Correct |
11 ms |
3200 KB |
Output is correct |
76 |
Correct |
11 ms |
3200 KB |
Output is correct |
77 |
Correct |
11 ms |
3200 KB |
Output is correct |
78 |
Correct |
10 ms |
3200 KB |
Output is correct |
79 |
Correct |
10 ms |
3200 KB |
Output is correct |
80 |
Correct |
11 ms |
3200 KB |
Output is correct |
81 |
Correct |
11 ms |
3200 KB |
Output is correct |
82 |
Correct |
12 ms |
3200 KB |
Output is correct |
83 |
Correct |
12 ms |
3200 KB |
Output is correct |
84 |
Correct |
11 ms |
3200 KB |
Output is correct |
85 |
Correct |
11 ms |
3200 KB |
Output is correct |
86 |
Correct |
11 ms |
3200 KB |
Output is correct |
87 |
Correct |
44 ms |
3840 KB |
Output is correct |
88 |
Correct |
135 ms |
6264 KB |
Output is correct |
89 |
Correct |
618 ms |
14528 KB |
Output is correct |
90 |
Correct |
578 ms |
14580 KB |
Output is correct |
91 |
Correct |
604 ms |
14452 KB |
Output is correct |
92 |
Correct |
124 ms |
18736 KB |
Output is correct |
93 |
Correct |
122 ms |
18676 KB |
Output is correct |
94 |
Correct |
124 ms |
18660 KB |
Output is correct |
95 |
Correct |
148 ms |
19828 KB |
Output is correct |
96 |
Correct |
160 ms |
20212 KB |
Output is correct |
97 |
Correct |
157 ms |
20316 KB |
Output is correct |
98 |
Correct |
153 ms |
20212 KB |
Output is correct |
99 |
Correct |
166 ms |
16968 KB |
Output is correct |
100 |
Correct |
596 ms |
14576 KB |
Output is correct |
101 |
Correct |
644 ms |
14916 KB |
Output is correct |
102 |
Correct |
657 ms |
15024 KB |
Output is correct |
103 |
Correct |
642 ms |
14964 KB |
Output is correct |
104 |
Correct |
163 ms |
16756 KB |
Output is correct |
105 |
Correct |
163 ms |
16756 KB |
Output is correct |
106 |
Correct |
182 ms |
16752 KB |
Output is correct |
107 |
Correct |
487 ms |
13300 KB |
Output is correct |
108 |
Correct |
535 ms |
13556 KB |
Output is correct |
109 |
Correct |
663 ms |
13940 KB |
Output is correct |
110 |
Correct |
103 ms |
17784 KB |
Output is correct |
111 |
Correct |
149 ms |
19828 KB |
Output is correct |
112 |
Correct |
134 ms |
19316 KB |
Output is correct |
113 |
Correct |
137 ms |
15732 KB |
Output is correct |
114 |
Correct |
613 ms |
14708 KB |
Output is correct |
115 |
Correct |
578 ms |
14044 KB |
Output is correct |
116 |
Correct |
151 ms |
15708 KB |
Output is correct |
117 |
Correct |
173 ms |
14836 KB |
Output is correct |
118 |
Correct |
181 ms |
14320 KB |
Output is correct |
119 |
Correct |
182 ms |
13940 KB |
Output is correct |
120 |
Correct |
155 ms |
14068 KB |
Output is correct |
121 |
Correct |
153 ms |
13552 KB |
Output is correct |
122 |
Correct |
159 ms |
13300 KB |
Output is correct |
123 |
Correct |
205 ms |
14960 KB |
Output is correct |
124 |
Correct |
189 ms |
14328 KB |
Output is correct |
125 |
Correct |
190 ms |
14068 KB |
Output is correct |
126 |
Correct |
176 ms |
14324 KB |
Output is correct |
127 |
Correct |
166 ms |
13684 KB |
Output is correct |
128 |
Correct |
169 ms |
13428 KB |
Output is correct |