#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define ll long long
#define pb push_back
#define lch i << 1
#define rch i << 1 | 1
#define all(x) x.begin(),x.end()
const int N = 1e5 + 1;
typedef pair<int,int> ii;
namespace _IT {
int t[N << 2];
int add(int x,int y) {
return x == y ? x : 0;
}
int upd(int i,int l,int r,int L,int R,int v) {
if (l > R || L > r) return 0;
if (L <= l && r <= R) {
t[i] = v;
return 1;
}
int m = (l + r) / 2;
upd(lch,l,m,L,R,v); ++m;
upd(rch,m,r,L,R,v);
t[i] = add(t[lch],t[rch]);
}
int get(int i,int l,int r,int L,int R) {
if (t[i] && l < r)
t[lch] = t[i],
t[rch] = t[i];
if (l > R || L > r) return -1;
if (L <= l && r <= R) return t[i];
int m = (l + r) / 2;
int tmp1 = get(lch,l,m,L,R); ++m;
int tmp2 = get(rch,m,r,L,R);
if (tmp1 < 0) tmp1 = tmp2;
if (tmp2 < 0) tmp2 = tmp1;
return add(tmp1,tmp2);
}
int lef(int i,int l,int r,int p,int v) {
if (t[i] && l < r)
t[lch] = t[i],
t[rch] = t[i];
if (t[i]) return t[i] == v ? l : N;
int m = (l + r + 2) / 2;
if (m > p) return lef(lch,l,m - 1,p,v);
int x = lef(rch,m,r,p,v);
if (x == m--) {
x = lef(lch,l,m,m,v);
if (x == N)
x = m + 1;
}
return x;
}
};
namespace BIT {
vector<int> val;
vector<int> bit;
void add(int x) { val.pb(x); }
void ini() {
sort(all(val));
val.resize(unique(all(val)) - val.begin());
bit.assign(val.size() + 5,0);
}
void upd(int p,int v) {
p = upper_bound(all(val),p) - val.begin() + 1;
int K = bit.size();
for(; p < K ; p += p & -p)
bit[p] += v;
}
int get(int p) {
int ans = 0;
p = upper_bound(all(val),p) - val.begin();
for(; p > 0 ; p -= p & -p)
ans += bit[p];
return ans;
}
};
vector<int> g[N];
int nCh[N], led[N];
int pos[N], par[N];
int arr[N], tot = 0;
int dfs(int u,int p) {
par[u] = p;
nCh[u] = 1;
for(int v : g[u])
dfs(v,u),
nCh[u] += nCh[v];
return 1;
}
int hld(int u,int S) {
if (S) led[u] = u;
else led[u] = led[par[u]];
arr[++tot] = u;
pos[u] = tot;
int B = 0;
for(int v : g[u]) if (nCh[v] > nCh[B])
B = v;
if (B) hld(B,0);
for(int v : g[u]) if (v != B)
hld(v,1);
return 1;
}
int c[N];
int a[N], b[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
for(int i = 1 ; i <= n ; ++i) cin >> c[i];
for(int i = 2 ; i <= n ; ++i) {
cin >> a[i] >> b[i];
g[a[i]].pb(b[i]);
}
dfs(1,0);
hld(1,1);
for(int i = 1 ; i <= n ; ++i)
_IT::upd(1,1,n,pos[i],pos[i],c[i]);
for(int i = 2 ; i <= n ; ++i) {
vector<ii> vec;
BIT::val.clear();
for(int x = a[i] ; x ;) {
int r = pos[x];
int C = _IT::get(1,1,n,r,r);
int l = _IT::lef(1,1,n,r,C);
if (l < pos[led[x]])
l = pos[led[x]];
x = par[arr[l]];
_IT::upd(1,1,n,l,r,c[b[i]]);
BIT::add(C);
vec.pb({C,r - l + 1});
}
BIT::ini();
ll ans = 0;
for(ii p : vec) {
ans += 1ll * p.Y * BIT::get(p.X);
BIT::upd(p.X,p.Y);
}
cout << ans << "\n";
}
}
Compilation message
construction.cpp: In function 'int _IT::upd(int, int, int, int, int, int)':
construction.cpp:33:5: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2788 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
5 ms |
2808 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
6 ms |
2808 KB |
Output is correct |
16 |
Correct |
6 ms |
2808 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
6 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
5 ms |
2780 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
6 ms |
2684 KB |
Output is correct |
31 |
Correct |
6 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
5 ms |
2808 KB |
Output is correct |
34 |
Correct |
5 ms |
2808 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
5 ms |
2808 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
5 ms |
2680 KB |
Output is correct |
39 |
Correct |
5 ms |
2808 KB |
Output is correct |
40 |
Correct |
5 ms |
2808 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
5 ms |
2808 KB |
Output is correct |
43 |
Correct |
6 ms |
2808 KB |
Output is correct |
44 |
Correct |
5 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2788 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
5 ms |
2808 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
6 ms |
2808 KB |
Output is correct |
16 |
Correct |
6 ms |
2808 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
6 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
5 ms |
2780 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
6 ms |
2684 KB |
Output is correct |
31 |
Correct |
6 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
5 ms |
2808 KB |
Output is correct |
34 |
Correct |
5 ms |
2808 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
5 ms |
2808 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
5 ms |
2680 KB |
Output is correct |
39 |
Correct |
5 ms |
2808 KB |
Output is correct |
40 |
Correct |
5 ms |
2808 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
5 ms |
2808 KB |
Output is correct |
43 |
Correct |
6 ms |
2808 KB |
Output is correct |
44 |
Correct |
5 ms |
2808 KB |
Output is correct |
45 |
Correct |
7 ms |
2808 KB |
Output is correct |
46 |
Correct |
19 ms |
3064 KB |
Output is correct |
47 |
Correct |
19 ms |
3064 KB |
Output is correct |
48 |
Correct |
19 ms |
3064 KB |
Output is correct |
49 |
Correct |
11 ms |
3320 KB |
Output is correct |
50 |
Correct |
11 ms |
3324 KB |
Output is correct |
51 |
Correct |
10 ms |
3320 KB |
Output is correct |
52 |
Correct |
10 ms |
3064 KB |
Output is correct |
53 |
Correct |
11 ms |
3192 KB |
Output is correct |
54 |
Correct |
11 ms |
3192 KB |
Output is correct |
55 |
Correct |
11 ms |
3192 KB |
Output is correct |
56 |
Correct |
11 ms |
3064 KB |
Output is correct |
57 |
Correct |
26 ms |
3064 KB |
Output is correct |
58 |
Correct |
28 ms |
3064 KB |
Output is correct |
59 |
Correct |
28 ms |
3064 KB |
Output is correct |
60 |
Correct |
28 ms |
3064 KB |
Output is correct |
61 |
Correct |
12 ms |
3164 KB |
Output is correct |
62 |
Correct |
12 ms |
3192 KB |
Output is correct |
63 |
Correct |
12 ms |
3140 KB |
Output is correct |
64 |
Correct |
17 ms |
3064 KB |
Output is correct |
65 |
Correct |
18 ms |
3064 KB |
Output is correct |
66 |
Correct |
18 ms |
3064 KB |
Output is correct |
67 |
Correct |
18 ms |
3064 KB |
Output is correct |
68 |
Correct |
9 ms |
3192 KB |
Output is correct |
69 |
Correct |
10 ms |
3192 KB |
Output is correct |
70 |
Correct |
11 ms |
3064 KB |
Output is correct |
71 |
Correct |
12 ms |
3180 KB |
Output is correct |
72 |
Correct |
26 ms |
2948 KB |
Output is correct |
73 |
Correct |
28 ms |
3064 KB |
Output is correct |
74 |
Correct |
11 ms |
3064 KB |
Output is correct |
75 |
Correct |
11 ms |
3064 KB |
Output is correct |
76 |
Correct |
13 ms |
3064 KB |
Output is correct |
77 |
Correct |
12 ms |
3064 KB |
Output is correct |
78 |
Correct |
11 ms |
3064 KB |
Output is correct |
79 |
Correct |
11 ms |
3064 KB |
Output is correct |
80 |
Correct |
12 ms |
3064 KB |
Output is correct |
81 |
Correct |
13 ms |
3064 KB |
Output is correct |
82 |
Correct |
13 ms |
3064 KB |
Output is correct |
83 |
Correct |
13 ms |
3064 KB |
Output is correct |
84 |
Correct |
13 ms |
3068 KB |
Output is correct |
85 |
Correct |
13 ms |
3064 KB |
Output is correct |
86 |
Correct |
13 ms |
3064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2808 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
5 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2788 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
5 ms |
2808 KB |
Output is correct |
10 |
Correct |
5 ms |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
5 ms |
2808 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
6 ms |
2808 KB |
Output is correct |
16 |
Correct |
6 ms |
2808 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
6 ms |
2808 KB |
Output is correct |
19 |
Correct |
5 ms |
2808 KB |
Output is correct |
20 |
Correct |
5 ms |
2808 KB |
Output is correct |
21 |
Correct |
5 ms |
2808 KB |
Output is correct |
22 |
Correct |
5 ms |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
5 ms |
2780 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
4 ms |
2808 KB |
Output is correct |
28 |
Correct |
5 ms |
2808 KB |
Output is correct |
29 |
Correct |
5 ms |
2808 KB |
Output is correct |
30 |
Correct |
6 ms |
2684 KB |
Output is correct |
31 |
Correct |
6 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2808 KB |
Output is correct |
33 |
Correct |
5 ms |
2808 KB |
Output is correct |
34 |
Correct |
5 ms |
2808 KB |
Output is correct |
35 |
Correct |
5 ms |
2808 KB |
Output is correct |
36 |
Correct |
5 ms |
2808 KB |
Output is correct |
37 |
Correct |
5 ms |
2808 KB |
Output is correct |
38 |
Correct |
5 ms |
2680 KB |
Output is correct |
39 |
Correct |
5 ms |
2808 KB |
Output is correct |
40 |
Correct |
5 ms |
2808 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
5 ms |
2808 KB |
Output is correct |
43 |
Correct |
6 ms |
2808 KB |
Output is correct |
44 |
Correct |
5 ms |
2808 KB |
Output is correct |
45 |
Correct |
7 ms |
2808 KB |
Output is correct |
46 |
Correct |
19 ms |
3064 KB |
Output is correct |
47 |
Correct |
19 ms |
3064 KB |
Output is correct |
48 |
Correct |
19 ms |
3064 KB |
Output is correct |
49 |
Correct |
11 ms |
3320 KB |
Output is correct |
50 |
Correct |
11 ms |
3324 KB |
Output is correct |
51 |
Correct |
10 ms |
3320 KB |
Output is correct |
52 |
Correct |
10 ms |
3064 KB |
Output is correct |
53 |
Correct |
11 ms |
3192 KB |
Output is correct |
54 |
Correct |
11 ms |
3192 KB |
Output is correct |
55 |
Correct |
11 ms |
3192 KB |
Output is correct |
56 |
Correct |
11 ms |
3064 KB |
Output is correct |
57 |
Correct |
26 ms |
3064 KB |
Output is correct |
58 |
Correct |
28 ms |
3064 KB |
Output is correct |
59 |
Correct |
28 ms |
3064 KB |
Output is correct |
60 |
Correct |
28 ms |
3064 KB |
Output is correct |
61 |
Correct |
12 ms |
3164 KB |
Output is correct |
62 |
Correct |
12 ms |
3192 KB |
Output is correct |
63 |
Correct |
12 ms |
3140 KB |
Output is correct |
64 |
Correct |
17 ms |
3064 KB |
Output is correct |
65 |
Correct |
18 ms |
3064 KB |
Output is correct |
66 |
Correct |
18 ms |
3064 KB |
Output is correct |
67 |
Correct |
18 ms |
3064 KB |
Output is correct |
68 |
Correct |
9 ms |
3192 KB |
Output is correct |
69 |
Correct |
10 ms |
3192 KB |
Output is correct |
70 |
Correct |
11 ms |
3064 KB |
Output is correct |
71 |
Correct |
12 ms |
3180 KB |
Output is correct |
72 |
Correct |
26 ms |
2948 KB |
Output is correct |
73 |
Correct |
28 ms |
3064 KB |
Output is correct |
74 |
Correct |
11 ms |
3064 KB |
Output is correct |
75 |
Correct |
11 ms |
3064 KB |
Output is correct |
76 |
Correct |
13 ms |
3064 KB |
Output is correct |
77 |
Correct |
12 ms |
3064 KB |
Output is correct |
78 |
Correct |
11 ms |
3064 KB |
Output is correct |
79 |
Correct |
11 ms |
3064 KB |
Output is correct |
80 |
Correct |
12 ms |
3064 KB |
Output is correct |
81 |
Correct |
13 ms |
3064 KB |
Output is correct |
82 |
Correct |
13 ms |
3064 KB |
Output is correct |
83 |
Correct |
13 ms |
3064 KB |
Output is correct |
84 |
Correct |
13 ms |
3068 KB |
Output is correct |
85 |
Correct |
13 ms |
3064 KB |
Output is correct |
86 |
Correct |
13 ms |
3064 KB |
Output is correct |
87 |
Correct |
46 ms |
3576 KB |
Output is correct |
88 |
Correct |
155 ms |
5124 KB |
Output is correct |
89 |
Correct |
685 ms |
10972 KB |
Output is correct |
90 |
Correct |
666 ms |
11012 KB |
Output is correct |
91 |
Correct |
674 ms |
11040 KB |
Output is correct |
92 |
Correct |
205 ms |
17280 KB |
Output is correct |
93 |
Correct |
202 ms |
17076 KB |
Output is correct |
94 |
Correct |
194 ms |
17144 KB |
Output is correct |
95 |
Correct |
232 ms |
13684 KB |
Output is correct |
96 |
Correct |
232 ms |
13988 KB |
Output is correct |
97 |
Correct |
235 ms |
13984 KB |
Output is correct |
98 |
Correct |
242 ms |
14068 KB |
Output is correct |
99 |
Correct |
219 ms |
13312 KB |
Output is correct |
100 |
Correct |
1187 ms |
11164 KB |
Output is correct |
101 |
Correct |
1234 ms |
11568 KB |
Output is correct |
102 |
Correct |
1235 ms |
11480 KB |
Output is correct |
103 |
Correct |
1241 ms |
11376 KB |
Output is correct |
104 |
Correct |
275 ms |
13284 KB |
Output is correct |
105 |
Correct |
277 ms |
13176 KB |
Output is correct |
106 |
Correct |
265 ms |
13176 KB |
Output is correct |
107 |
Correct |
595 ms |
10148 KB |
Output is correct |
108 |
Correct |
671 ms |
10428 KB |
Output is correct |
109 |
Correct |
685 ms |
10716 KB |
Output is correct |
110 |
Correct |
179 ms |
16524 KB |
Output is correct |
111 |
Correct |
213 ms |
13656 KB |
Output is correct |
112 |
Correct |
223 ms |
13428 KB |
Output is correct |
113 |
Correct |
211 ms |
12692 KB |
Output is correct |
114 |
Correct |
1164 ms |
11112 KB |
Output is correct |
115 |
Correct |
1247 ms |
10788 KB |
Output is correct |
116 |
Correct |
286 ms |
12588 KB |
Output is correct |
117 |
Correct |
266 ms |
11800 KB |
Output is correct |
118 |
Correct |
270 ms |
11412 KB |
Output is correct |
119 |
Correct |
279 ms |
11128 KB |
Output is correct |
120 |
Correct |
262 ms |
11384 KB |
Output is correct |
121 |
Correct |
264 ms |
11028 KB |
Output is correct |
122 |
Correct |
280 ms |
10968 KB |
Output is correct |
123 |
Correct |
346 ms |
11780 KB |
Output is correct |
124 |
Correct |
330 ms |
11328 KB |
Output is correct |
125 |
Correct |
343 ms |
11152 KB |
Output is correct |
126 |
Correct |
327 ms |
11460 KB |
Output is correct |
127 |
Correct |
326 ms |
11112 KB |
Output is correct |
128 |
Correct |
341 ms |
10908 KB |
Output is correct |