#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define szz(x) (int)x.size()
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) x.begin(), x.end()
typedef tuple<int, int, int> t3;
int N, C[100010];
int In[100010][2];
vector <int> E[100010];
int st[100010], en[100010], ce;
int up[100010][20], dep[100010];
void dfs(int x, int fa) {
st[x] = ++ce;
for(int e : E[x]) if(e != fa) {
dep[e] = dep[x] + 1;
up[e][0] = x;
for(int i=1;i<20;i++) up[e][i] = up[ up[e][i-1] ][i-1];
dfs(e, x);
}
en[x] = ce;
}
const int TADD = 1<<17;
int T[1<<18];
int get_max(int l, int r) {
l += TADD, r += TADD;
int res = 0;
while(l <= r) {
if(l&1) res = max(res, T[l++]);
if(!(r&1)) res = max(res, T[r--]);
l >>= 1, r >>= 1;
}
return res;
}
int get_node(int x) { return get_max(st[x], en[x]); }
void upd(int x, int v) {
x += TADD;
while(x) T[x] = v, x >>= 1;
}
int stamp;
int max_bit[100010];
int bt[100010];
void updb(int x, int v) {
for(int i=x;i<100010;i+=(i&-i)) bt[i] += v;
}
int readb(int x) {
int res = 0;
for(int i=x;i;i-=(i&-i)) res += bt[i];
return res;
}
ll Do(vector <t3> &X) {
sort(all(X), [](t3 a, t3 b) {
if(get<1>(a) != get<1>(b)) return get<1>(a) < get<1>(b);
return get<2>(a) > get<2>(b);
});
ll res = 0;
for(t3 &e : X) {
res += (ll) get<0>(e) * readb(get<2>(e));
updb(get<2>(e), get<0>(e));
}
for(t3 &e : X) {
updb(get<2>(e), -get<0>(e));
}
return res;
}
int cc[100010];
void solve(){
for(int i=1;i<100010;i++) max_bit[i] = 31 - __builtin_clz(i);
scanf("%d", &N);
vector <int> vc;
for(int i=1;i<=N;i++) scanf("%d", C + i), vc.pb(C[i]);
sort(all(vc)); vc.resize(unique(all(vc)) - vc.begin());
for(int i=1;i<=N;i++) C[i] = (int)(lower_bound(all(vc), C[i]) - vc.begin() + 1);
for(int i=1;i<N;i++) {
int x, y; scanf("%d%d", &x, &y);
In[i][0] = x, In[i][1] = y;
E[x].pb(y);
}
dfs(1, -1);
upd(st[1], ++stamp); cc[stamp] = C[1];
for(int i=1;i<N;i++) {
int b = In[i][1], a = In[i][0];
vector <t3> R;
while(a) {
int val = get_node(a);
int cnt = 1;
for(int j=max_bit[dep[a]];j>=0;j--) {
int val2 = get_node(up[a][j]);
if(val == val2) {
cnt += 1<<j;
a = up[a][j];
}
}
int nr = szz(R);
R.pb(t3(cnt, cc[val], 1 + nr));
a = up[a][0];
}
printf("%lld\n", Do(R));
upd(st[b], ++stamp);
cc[stamp] = C[b];
}
}
int main(){
int Tc = 1; //scanf("%d", &Tc);
for(int tc=1;tc<=Tc;tc++){
solve();
}
return 0;
}
Compilation message
construction.cpp: In function 'void solve()':
construction.cpp:93:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
construction.cpp:95:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1;i<=N;i++) scanf("%d", C + i), vc.pb(C[i]);
^
construction.cpp:99:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y; scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3192 KB |
Output is correct |
2 |
Correct |
7 ms |
3256 KB |
Output is correct |
3 |
Correct |
6 ms |
3256 KB |
Output is correct |
4 |
Correct |
7 ms |
3256 KB |
Output is correct |
5 |
Correct |
7 ms |
3288 KB |
Output is correct |
6 |
Correct |
6 ms |
3312 KB |
Output is correct |
7 |
Correct |
6 ms |
3452 KB |
Output is correct |
8 |
Correct |
6 ms |
3488 KB |
Output is correct |
9 |
Correct |
7 ms |
3492 KB |
Output is correct |
10 |
Correct |
6 ms |
3492 KB |
Output is correct |
11 |
Correct |
7 ms |
3504 KB |
Output is correct |
12 |
Correct |
6 ms |
3504 KB |
Output is correct |
13 |
Correct |
7 ms |
3556 KB |
Output is correct |
14 |
Correct |
8 ms |
3692 KB |
Output is correct |
15 |
Correct |
9 ms |
3692 KB |
Output is correct |
16 |
Correct |
8 ms |
3692 KB |
Output is correct |
17 |
Correct |
6 ms |
3692 KB |
Output is correct |
18 |
Correct |
7 ms |
3692 KB |
Output is correct |
19 |
Correct |
7 ms |
3692 KB |
Output is correct |
20 |
Correct |
6 ms |
3692 KB |
Output is correct |
21 |
Correct |
7 ms |
3692 KB |
Output is correct |
22 |
Correct |
6 ms |
3692 KB |
Output is correct |
23 |
Correct |
6 ms |
3692 KB |
Output is correct |
24 |
Correct |
8 ms |
3692 KB |
Output is correct |
25 |
Correct |
5 ms |
3692 KB |
Output is correct |
26 |
Correct |
5 ms |
3912 KB |
Output is correct |
27 |
Correct |
6 ms |
3912 KB |
Output is correct |
28 |
Correct |
6 ms |
3912 KB |
Output is correct |
29 |
Correct |
5 ms |
3912 KB |
Output is correct |
30 |
Correct |
6 ms |
3912 KB |
Output is correct |
31 |
Correct |
6 ms |
3912 KB |
Output is correct |
32 |
Correct |
5 ms |
3912 KB |
Output is correct |
33 |
Correct |
6 ms |
3912 KB |
Output is correct |
34 |
Correct |
5 ms |
3912 KB |
Output is correct |
35 |
Correct |
5 ms |
3912 KB |
Output is correct |
36 |
Correct |
6 ms |
3912 KB |
Output is correct |
37 |
Correct |
10 ms |
3912 KB |
Output is correct |
38 |
Correct |
7 ms |
3912 KB |
Output is correct |
39 |
Correct |
7 ms |
3912 KB |
Output is correct |
40 |
Correct |
9 ms |
3912 KB |
Output is correct |
41 |
Correct |
6 ms |
3912 KB |
Output is correct |
42 |
Correct |
6 ms |
3912 KB |
Output is correct |
43 |
Correct |
6 ms |
3912 KB |
Output is correct |
44 |
Correct |
6 ms |
3912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3192 KB |
Output is correct |
2 |
Correct |
7 ms |
3256 KB |
Output is correct |
3 |
Correct |
6 ms |
3256 KB |
Output is correct |
4 |
Correct |
7 ms |
3256 KB |
Output is correct |
5 |
Correct |
7 ms |
3288 KB |
Output is correct |
6 |
Correct |
6 ms |
3312 KB |
Output is correct |
7 |
Correct |
6 ms |
3452 KB |
Output is correct |
8 |
Correct |
6 ms |
3488 KB |
Output is correct |
9 |
Correct |
7 ms |
3492 KB |
Output is correct |
10 |
Correct |
6 ms |
3492 KB |
Output is correct |
11 |
Correct |
7 ms |
3504 KB |
Output is correct |
12 |
Correct |
6 ms |
3504 KB |
Output is correct |
13 |
Correct |
7 ms |
3556 KB |
Output is correct |
14 |
Correct |
8 ms |
3692 KB |
Output is correct |
15 |
Correct |
9 ms |
3692 KB |
Output is correct |
16 |
Correct |
8 ms |
3692 KB |
Output is correct |
17 |
Correct |
6 ms |
3692 KB |
Output is correct |
18 |
Correct |
7 ms |
3692 KB |
Output is correct |
19 |
Correct |
7 ms |
3692 KB |
Output is correct |
20 |
Correct |
6 ms |
3692 KB |
Output is correct |
21 |
Correct |
7 ms |
3692 KB |
Output is correct |
22 |
Correct |
6 ms |
3692 KB |
Output is correct |
23 |
Correct |
6 ms |
3692 KB |
Output is correct |
24 |
Correct |
8 ms |
3692 KB |
Output is correct |
25 |
Correct |
5 ms |
3692 KB |
Output is correct |
26 |
Correct |
5 ms |
3912 KB |
Output is correct |
27 |
Correct |
6 ms |
3912 KB |
Output is correct |
28 |
Correct |
6 ms |
3912 KB |
Output is correct |
29 |
Correct |
5 ms |
3912 KB |
Output is correct |
30 |
Correct |
6 ms |
3912 KB |
Output is correct |
31 |
Correct |
6 ms |
3912 KB |
Output is correct |
32 |
Correct |
5 ms |
3912 KB |
Output is correct |
33 |
Correct |
6 ms |
3912 KB |
Output is correct |
34 |
Correct |
5 ms |
3912 KB |
Output is correct |
35 |
Correct |
5 ms |
3912 KB |
Output is correct |
36 |
Correct |
6 ms |
3912 KB |
Output is correct |
37 |
Correct |
10 ms |
3912 KB |
Output is correct |
38 |
Correct |
7 ms |
3912 KB |
Output is correct |
39 |
Correct |
7 ms |
3912 KB |
Output is correct |
40 |
Correct |
9 ms |
3912 KB |
Output is correct |
41 |
Correct |
6 ms |
3912 KB |
Output is correct |
42 |
Correct |
6 ms |
3912 KB |
Output is correct |
43 |
Correct |
6 ms |
3912 KB |
Output is correct |
44 |
Correct |
6 ms |
3912 KB |
Output is correct |
45 |
Correct |
9 ms |
3972 KB |
Output is correct |
46 |
Correct |
21 ms |
4484 KB |
Output is correct |
47 |
Correct |
15 ms |
4564 KB |
Output is correct |
48 |
Correct |
17 ms |
4652 KB |
Output is correct |
49 |
Correct |
11 ms |
4944 KB |
Output is correct |
50 |
Correct |
18 ms |
4948 KB |
Output is correct |
51 |
Correct |
12 ms |
5152 KB |
Output is correct |
52 |
Correct |
14 ms |
5152 KB |
Output is correct |
53 |
Correct |
14 ms |
5152 KB |
Output is correct |
54 |
Correct |
16 ms |
5152 KB |
Output is correct |
55 |
Correct |
19 ms |
5152 KB |
Output is correct |
56 |
Correct |
16 ms |
5152 KB |
Output is correct |
57 |
Correct |
30 ms |
5152 KB |
Output is correct |
58 |
Correct |
23 ms |
5152 KB |
Output is correct |
59 |
Correct |
33 ms |
5152 KB |
Output is correct |
60 |
Correct |
26 ms |
5152 KB |
Output is correct |
61 |
Correct |
18 ms |
5152 KB |
Output is correct |
62 |
Correct |
14 ms |
5152 KB |
Output is correct |
63 |
Correct |
15 ms |
5152 KB |
Output is correct |
64 |
Correct |
16 ms |
5152 KB |
Output is correct |
65 |
Correct |
13 ms |
5152 KB |
Output is correct |
66 |
Correct |
17 ms |
5152 KB |
Output is correct |
67 |
Correct |
17 ms |
5152 KB |
Output is correct |
68 |
Correct |
11 ms |
5220 KB |
Output is correct |
69 |
Correct |
19 ms |
5220 KB |
Output is correct |
70 |
Correct |
14 ms |
5220 KB |
Output is correct |
71 |
Correct |
13 ms |
5220 KB |
Output is correct |
72 |
Correct |
21 ms |
5220 KB |
Output is correct |
73 |
Correct |
33 ms |
5220 KB |
Output is correct |
74 |
Correct |
16 ms |
5220 KB |
Output is correct |
75 |
Correct |
13 ms |
5220 KB |
Output is correct |
76 |
Correct |
12 ms |
5220 KB |
Output is correct |
77 |
Correct |
16 ms |
5220 KB |
Output is correct |
78 |
Correct |
13 ms |
5220 KB |
Output is correct |
79 |
Correct |
14 ms |
5220 KB |
Output is correct |
80 |
Correct |
16 ms |
5220 KB |
Output is correct |
81 |
Correct |
16 ms |
5220 KB |
Output is correct |
82 |
Correct |
18 ms |
5220 KB |
Output is correct |
83 |
Correct |
20 ms |
5220 KB |
Output is correct |
84 |
Correct |
18 ms |
5220 KB |
Output is correct |
85 |
Correct |
19 ms |
5220 KB |
Output is correct |
86 |
Correct |
12 ms |
5220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3192 KB |
Output is correct |
2 |
Correct |
7 ms |
3256 KB |
Output is correct |
3 |
Correct |
6 ms |
3256 KB |
Output is correct |
4 |
Correct |
7 ms |
3256 KB |
Output is correct |
5 |
Correct |
7 ms |
3288 KB |
Output is correct |
6 |
Correct |
6 ms |
3312 KB |
Output is correct |
7 |
Correct |
6 ms |
3452 KB |
Output is correct |
8 |
Correct |
6 ms |
3488 KB |
Output is correct |
9 |
Correct |
7 ms |
3492 KB |
Output is correct |
10 |
Correct |
6 ms |
3492 KB |
Output is correct |
11 |
Correct |
7 ms |
3504 KB |
Output is correct |
12 |
Correct |
6 ms |
3504 KB |
Output is correct |
13 |
Correct |
7 ms |
3556 KB |
Output is correct |
14 |
Correct |
8 ms |
3692 KB |
Output is correct |
15 |
Correct |
9 ms |
3692 KB |
Output is correct |
16 |
Correct |
8 ms |
3692 KB |
Output is correct |
17 |
Correct |
6 ms |
3692 KB |
Output is correct |
18 |
Correct |
7 ms |
3692 KB |
Output is correct |
19 |
Correct |
7 ms |
3692 KB |
Output is correct |
20 |
Correct |
6 ms |
3692 KB |
Output is correct |
21 |
Correct |
7 ms |
3692 KB |
Output is correct |
22 |
Correct |
6 ms |
3692 KB |
Output is correct |
23 |
Correct |
6 ms |
3692 KB |
Output is correct |
24 |
Correct |
8 ms |
3692 KB |
Output is correct |
25 |
Correct |
5 ms |
3692 KB |
Output is correct |
26 |
Correct |
5 ms |
3912 KB |
Output is correct |
27 |
Correct |
6 ms |
3912 KB |
Output is correct |
28 |
Correct |
6 ms |
3912 KB |
Output is correct |
29 |
Correct |
5 ms |
3912 KB |
Output is correct |
30 |
Correct |
6 ms |
3912 KB |
Output is correct |
31 |
Correct |
6 ms |
3912 KB |
Output is correct |
32 |
Correct |
5 ms |
3912 KB |
Output is correct |
33 |
Correct |
6 ms |
3912 KB |
Output is correct |
34 |
Correct |
5 ms |
3912 KB |
Output is correct |
35 |
Correct |
5 ms |
3912 KB |
Output is correct |
36 |
Correct |
6 ms |
3912 KB |
Output is correct |
37 |
Correct |
10 ms |
3912 KB |
Output is correct |
38 |
Correct |
7 ms |
3912 KB |
Output is correct |
39 |
Correct |
7 ms |
3912 KB |
Output is correct |
40 |
Correct |
9 ms |
3912 KB |
Output is correct |
41 |
Correct |
6 ms |
3912 KB |
Output is correct |
42 |
Correct |
6 ms |
3912 KB |
Output is correct |
43 |
Correct |
6 ms |
3912 KB |
Output is correct |
44 |
Correct |
6 ms |
3912 KB |
Output is correct |
45 |
Correct |
9 ms |
3972 KB |
Output is correct |
46 |
Correct |
21 ms |
4484 KB |
Output is correct |
47 |
Correct |
15 ms |
4564 KB |
Output is correct |
48 |
Correct |
17 ms |
4652 KB |
Output is correct |
49 |
Correct |
11 ms |
4944 KB |
Output is correct |
50 |
Correct |
18 ms |
4948 KB |
Output is correct |
51 |
Correct |
12 ms |
5152 KB |
Output is correct |
52 |
Correct |
14 ms |
5152 KB |
Output is correct |
53 |
Correct |
14 ms |
5152 KB |
Output is correct |
54 |
Correct |
16 ms |
5152 KB |
Output is correct |
55 |
Correct |
19 ms |
5152 KB |
Output is correct |
56 |
Correct |
16 ms |
5152 KB |
Output is correct |
57 |
Correct |
30 ms |
5152 KB |
Output is correct |
58 |
Correct |
23 ms |
5152 KB |
Output is correct |
59 |
Correct |
33 ms |
5152 KB |
Output is correct |
60 |
Correct |
26 ms |
5152 KB |
Output is correct |
61 |
Correct |
18 ms |
5152 KB |
Output is correct |
62 |
Correct |
14 ms |
5152 KB |
Output is correct |
63 |
Correct |
15 ms |
5152 KB |
Output is correct |
64 |
Correct |
16 ms |
5152 KB |
Output is correct |
65 |
Correct |
13 ms |
5152 KB |
Output is correct |
66 |
Correct |
17 ms |
5152 KB |
Output is correct |
67 |
Correct |
17 ms |
5152 KB |
Output is correct |
68 |
Correct |
11 ms |
5220 KB |
Output is correct |
69 |
Correct |
19 ms |
5220 KB |
Output is correct |
70 |
Correct |
14 ms |
5220 KB |
Output is correct |
71 |
Correct |
13 ms |
5220 KB |
Output is correct |
72 |
Correct |
21 ms |
5220 KB |
Output is correct |
73 |
Correct |
33 ms |
5220 KB |
Output is correct |
74 |
Correct |
16 ms |
5220 KB |
Output is correct |
75 |
Correct |
13 ms |
5220 KB |
Output is correct |
76 |
Correct |
12 ms |
5220 KB |
Output is correct |
77 |
Correct |
16 ms |
5220 KB |
Output is correct |
78 |
Correct |
13 ms |
5220 KB |
Output is correct |
79 |
Correct |
14 ms |
5220 KB |
Output is correct |
80 |
Correct |
16 ms |
5220 KB |
Output is correct |
81 |
Correct |
16 ms |
5220 KB |
Output is correct |
82 |
Correct |
18 ms |
5220 KB |
Output is correct |
83 |
Correct |
20 ms |
5220 KB |
Output is correct |
84 |
Correct |
18 ms |
5220 KB |
Output is correct |
85 |
Correct |
19 ms |
5220 KB |
Output is correct |
86 |
Correct |
12 ms |
5220 KB |
Output is correct |
87 |
Correct |
41 ms |
5732 KB |
Output is correct |
88 |
Correct |
143 ms |
8456 KB |
Output is correct |
89 |
Correct |
577 ms |
18140 KB |
Output is correct |
90 |
Correct |
555 ms |
18140 KB |
Output is correct |
91 |
Correct |
632 ms |
18268 KB |
Output is correct |
92 |
Correct |
319 ms |
24284 KB |
Output is correct |
93 |
Correct |
341 ms |
24284 KB |
Output is correct |
94 |
Correct |
349 ms |
24284 KB |
Output is correct |
95 |
Correct |
393 ms |
24284 KB |
Output is correct |
96 |
Correct |
499 ms |
24284 KB |
Output is correct |
97 |
Correct |
453 ms |
24284 KB |
Output is correct |
98 |
Correct |
516 ms |
24284 KB |
Output is correct |
99 |
Correct |
558 ms |
24284 KB |
Output is correct |
100 |
Correct |
1029 ms |
24284 KB |
Output is correct |
101 |
Correct |
977 ms |
24284 KB |
Output is correct |
102 |
Correct |
1152 ms |
24284 KB |
Output is correct |
103 |
Correct |
1089 ms |
24284 KB |
Output is correct |
104 |
Correct |
526 ms |
24284 KB |
Output is correct |
105 |
Correct |
702 ms |
24284 KB |
Output is correct |
106 |
Correct |
500 ms |
24284 KB |
Output is correct |
107 |
Correct |
492 ms |
24284 KB |
Output is correct |
108 |
Correct |
508 ms |
24284 KB |
Output is correct |
109 |
Correct |
570 ms |
24284 KB |
Output is correct |
110 |
Correct |
480 ms |
24284 KB |
Output is correct |
111 |
Correct |
499 ms |
24284 KB |
Output is correct |
112 |
Correct |
459 ms |
24284 KB |
Output is correct |
113 |
Correct |
464 ms |
24284 KB |
Output is correct |
114 |
Correct |
1130 ms |
24284 KB |
Output is correct |
115 |
Correct |
954 ms |
24284 KB |
Output is correct |
116 |
Correct |
660 ms |
24284 KB |
Output is correct |
117 |
Correct |
443 ms |
24284 KB |
Output is correct |
118 |
Correct |
348 ms |
24284 KB |
Output is correct |
119 |
Correct |
441 ms |
24284 KB |
Output is correct |
120 |
Correct |
364 ms |
24284 KB |
Output is correct |
121 |
Correct |
298 ms |
24284 KB |
Output is correct |
122 |
Correct |
363 ms |
24284 KB |
Output is correct |
123 |
Correct |
429 ms |
24284 KB |
Output is correct |
124 |
Correct |
405 ms |
24284 KB |
Output is correct |
125 |
Correct |
460 ms |
24284 KB |
Output is correct |
126 |
Correct |
489 ms |
24284 KB |
Output is correct |
127 |
Correct |
409 ms |
24284 KB |
Output is correct |
128 |
Correct |
399 ms |
24284 KB |
Output is correct |