#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define piii pair<pii, int>
const int N = 100505;
int ch[N] , pai[N] , C[N] , sz[N], in[N] , tempo = 0 , rinv[N + 100];
vector<int> adj[N];
vector<pii> edge;
deque<piii> Sx[N];
int n;
vector<pii> elements;
void dfs(int x , int y){
sz[x] = 1 ;
pai[x] = -1;
if(x != y) pai[x] = y;
for(auto &p : adj[x]){
if(p == y) continue;
dfs(p, x);
sz[x] += sz[p];
if(adj[x][0] == y || sz[adj[x][0]] < sz[p]) swap(p, adj[x][0]);
}
}
void dfs_hld(int x , int y){
in[x] = ++tempo;
if(x == y) ch[x] = 1;
rinv[tempo] = x;
for(auto p : adj[x]){
if(p == y) continue;
if(p == adj[x][0]) ch[p] = ch[x];
else ch[p] = p;
dfs_hld(p, x);
}
}
void getX(int x){
vector<pii> selements;
for(auto p : Sx[ch[x]]){
if(p.F.S <= in[x]){
selements.push_back(pii(p.S , p.F.S - p.F.F +1));
}
else if(p.F.F <= in[x]){
selements.push_back(pii(p.S , in[x] - p.F.F + 1));
}
else break;
}
reverse(selements.begin() , selements.end());
for(auto p : selements){
elements.push_back(p);
}
}
void addX(int x , piii u){
while(Sx[x].size() && Sx[x].front().F.S <= u.F.S) Sx[x].pop_front();
if(Sx[x].size() && Sx[x].front().F.F <= u.F.S){
Sx[x].front().F.F = in[adj[rinv[u.F.S]][0]];
}
while(Sx[x].size() && Sx[x].front().S == u.S){
u.F.S = Sx[x].front().F.S;
Sx[x].pop_front();
}
Sx[x].push_front(u);
}
set<int> compress;
map<int,int> poscompress;
int vc = 0;
int bit[N + 100];
int get(int x){
int s = 0;
for(int i = x ; i > 0 ; i-=(i&-i)) s+= bit[i];
return s;
}
void add(int x , int vl){
for(int i = x ; i < N + 100 ; i+=(i&-i)) bit[i] += vl;
}
int32_t main(){
scanf("%d" , &n);
for(int i = 1 ; i <= n; i++) scanf("%d" ,&C[i]) , compress.insert(C[i]);
for(auto p : compress){
poscompress[p] = ++vc;
}
for(int i = 1 ; i <= n; i ++) C[i] = poscompress[C[i]];
for(int i = 1 ; i < n ; i ++){
int x, y;
scanf("%d%d" , &x , &y);
adj[x].pb(y);
adj[y].pb(x);
edge.push_back(pii(x,y));
}
dfs(1,1);
dfs_hld(1,1);
addX(ch[1] , piii(pii(in[1] , in[1]) , C[1]));
for(auto p : edge){
elements.clear();
int X = p.F;
while(X != -1){
getX(X);
X = pai[ch[X]];
}
X = p.S;
while(X != -1){
addX(ch[X] , piii(pii(in[ch[X]] , in[X]), C[p.S]));
X = pai[ch[X]];
}
reverse(elements.begin() , elements.end());
long long ans = 0;
for(auto p : elements){
add(p.F, p.S);
long long U = get(N + 90) - get(p.F);
U *= (1LL)*((long long)p.S);
ans += U;
}
for(auto p : elements){
add(p.F , -p.S);
}
printf("%lld\n" , ans);
}
}
Compilation message
construction.cpp: In function 'int32_t main()':
construction.cpp:86:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d" , &n);
~~~~~^~~~~~~~~~~
construction.cpp:87:50: 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]) , compress.insert(C[i]);
~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:94:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d" , &x , &y);
~~~~~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
68984 KB |
Output is correct |
2 |
Correct |
71 ms |
68984 KB |
Output is correct |
3 |
Correct |
75 ms |
69180 KB |
Output is correct |
4 |
Correct |
67 ms |
69180 KB |
Output is correct |
5 |
Correct |
74 ms |
69244 KB |
Output is correct |
6 |
Correct |
76 ms |
69244 KB |
Output is correct |
7 |
Correct |
90 ms |
69272 KB |
Output is correct |
8 |
Correct |
67 ms |
69272 KB |
Output is correct |
9 |
Correct |
70 ms |
69272 KB |
Output is correct |
10 |
Correct |
70 ms |
69272 KB |
Output is correct |
11 |
Correct |
72 ms |
69272 KB |
Output is correct |
12 |
Correct |
73 ms |
69272 KB |
Output is correct |
13 |
Correct |
76 ms |
69304 KB |
Output is correct |
14 |
Correct |
72 ms |
69304 KB |
Output is correct |
15 |
Correct |
73 ms |
69304 KB |
Output is correct |
16 |
Correct |
87 ms |
69376 KB |
Output is correct |
17 |
Correct |
75 ms |
69376 KB |
Output is correct |
18 |
Correct |
92 ms |
69376 KB |
Output is correct |
19 |
Correct |
82 ms |
69376 KB |
Output is correct |
20 |
Correct |
72 ms |
69376 KB |
Output is correct |
21 |
Correct |
77 ms |
69376 KB |
Output is correct |
22 |
Correct |
73 ms |
69376 KB |
Output is correct |
23 |
Correct |
73 ms |
69376 KB |
Output is correct |
24 |
Correct |
77 ms |
69376 KB |
Output is correct |
25 |
Correct |
85 ms |
69376 KB |
Output is correct |
26 |
Correct |
70 ms |
69376 KB |
Output is correct |
27 |
Correct |
76 ms |
69376 KB |
Output is correct |
28 |
Correct |
74 ms |
69376 KB |
Output is correct |
29 |
Correct |
75 ms |
69376 KB |
Output is correct |
30 |
Correct |
77 ms |
69376 KB |
Output is correct |
31 |
Correct |
76 ms |
69376 KB |
Output is correct |
32 |
Correct |
76 ms |
69380 KB |
Output is correct |
33 |
Correct |
77 ms |
69380 KB |
Output is correct |
34 |
Correct |
75 ms |
69380 KB |
Output is correct |
35 |
Correct |
76 ms |
69380 KB |
Output is correct |
36 |
Correct |
73 ms |
69380 KB |
Output is correct |
37 |
Correct |
76 ms |
69380 KB |
Output is correct |
38 |
Correct |
78 ms |
69496 KB |
Output is correct |
39 |
Correct |
73 ms |
69496 KB |
Output is correct |
40 |
Correct |
76 ms |
69496 KB |
Output is correct |
41 |
Correct |
76 ms |
69648 KB |
Output is correct |
42 |
Correct |
75 ms |
69648 KB |
Output is correct |
43 |
Correct |
75 ms |
69648 KB |
Output is correct |
44 |
Correct |
75 ms |
69648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
68984 KB |
Output is correct |
2 |
Correct |
71 ms |
68984 KB |
Output is correct |
3 |
Correct |
75 ms |
69180 KB |
Output is correct |
4 |
Correct |
67 ms |
69180 KB |
Output is correct |
5 |
Correct |
74 ms |
69244 KB |
Output is correct |
6 |
Correct |
76 ms |
69244 KB |
Output is correct |
7 |
Correct |
90 ms |
69272 KB |
Output is correct |
8 |
Correct |
67 ms |
69272 KB |
Output is correct |
9 |
Correct |
70 ms |
69272 KB |
Output is correct |
10 |
Correct |
70 ms |
69272 KB |
Output is correct |
11 |
Correct |
72 ms |
69272 KB |
Output is correct |
12 |
Correct |
73 ms |
69272 KB |
Output is correct |
13 |
Correct |
76 ms |
69304 KB |
Output is correct |
14 |
Correct |
72 ms |
69304 KB |
Output is correct |
15 |
Correct |
73 ms |
69304 KB |
Output is correct |
16 |
Correct |
87 ms |
69376 KB |
Output is correct |
17 |
Correct |
75 ms |
69376 KB |
Output is correct |
18 |
Correct |
92 ms |
69376 KB |
Output is correct |
19 |
Correct |
82 ms |
69376 KB |
Output is correct |
20 |
Correct |
72 ms |
69376 KB |
Output is correct |
21 |
Correct |
77 ms |
69376 KB |
Output is correct |
22 |
Correct |
73 ms |
69376 KB |
Output is correct |
23 |
Correct |
73 ms |
69376 KB |
Output is correct |
24 |
Correct |
77 ms |
69376 KB |
Output is correct |
25 |
Correct |
85 ms |
69376 KB |
Output is correct |
26 |
Correct |
70 ms |
69376 KB |
Output is correct |
27 |
Correct |
76 ms |
69376 KB |
Output is correct |
28 |
Correct |
74 ms |
69376 KB |
Output is correct |
29 |
Correct |
75 ms |
69376 KB |
Output is correct |
30 |
Correct |
77 ms |
69376 KB |
Output is correct |
31 |
Correct |
76 ms |
69376 KB |
Output is correct |
32 |
Correct |
76 ms |
69380 KB |
Output is correct |
33 |
Correct |
77 ms |
69380 KB |
Output is correct |
34 |
Correct |
75 ms |
69380 KB |
Output is correct |
35 |
Correct |
76 ms |
69380 KB |
Output is correct |
36 |
Correct |
73 ms |
69380 KB |
Output is correct |
37 |
Correct |
76 ms |
69380 KB |
Output is correct |
38 |
Correct |
78 ms |
69496 KB |
Output is correct |
39 |
Correct |
73 ms |
69496 KB |
Output is correct |
40 |
Correct |
76 ms |
69496 KB |
Output is correct |
41 |
Correct |
76 ms |
69648 KB |
Output is correct |
42 |
Correct |
75 ms |
69648 KB |
Output is correct |
43 |
Correct |
75 ms |
69648 KB |
Output is correct |
44 |
Correct |
75 ms |
69648 KB |
Output is correct |
45 |
Correct |
74 ms |
69680 KB |
Output is correct |
46 |
Correct |
87 ms |
70984 KB |
Output is correct |
47 |
Correct |
85 ms |
71184 KB |
Output is correct |
48 |
Correct |
103 ms |
71184 KB |
Output is correct |
49 |
Correct |
80 ms |
71184 KB |
Output is correct |
50 |
Correct |
74 ms |
71184 KB |
Output is correct |
51 |
Correct |
98 ms |
71184 KB |
Output is correct |
52 |
Correct |
86 ms |
71652 KB |
Output is correct |
53 |
Correct |
81 ms |
71680 KB |
Output is correct |
54 |
Correct |
83 ms |
71728 KB |
Output is correct |
55 |
Correct |
85 ms |
71820 KB |
Output is correct |
56 |
Correct |
86 ms |
72024 KB |
Output is correct |
57 |
Correct |
89 ms |
72024 KB |
Output is correct |
58 |
Correct |
89 ms |
72024 KB |
Output is correct |
59 |
Correct |
88 ms |
72104 KB |
Output is correct |
60 |
Correct |
94 ms |
72104 KB |
Output is correct |
61 |
Correct |
83 ms |
72256 KB |
Output is correct |
62 |
Correct |
88 ms |
72340 KB |
Output is correct |
63 |
Correct |
83 ms |
72408 KB |
Output is correct |
64 |
Correct |
85 ms |
72408 KB |
Output is correct |
65 |
Correct |
83 ms |
72408 KB |
Output is correct |
66 |
Correct |
88 ms |
72408 KB |
Output is correct |
67 |
Correct |
82 ms |
72408 KB |
Output is correct |
68 |
Correct |
72 ms |
72408 KB |
Output is correct |
69 |
Correct |
85 ms |
72880 KB |
Output is correct |
70 |
Correct |
81 ms |
72880 KB |
Output is correct |
71 |
Correct |
82 ms |
72880 KB |
Output is correct |
72 |
Correct |
89 ms |
72880 KB |
Output is correct |
73 |
Correct |
87 ms |
72880 KB |
Output is correct |
74 |
Correct |
77 ms |
72880 KB |
Output is correct |
75 |
Correct |
88 ms |
72928 KB |
Output is correct |
76 |
Correct |
88 ms |
72992 KB |
Output is correct |
77 |
Correct |
86 ms |
73184 KB |
Output is correct |
78 |
Correct |
83 ms |
73184 KB |
Output is correct |
79 |
Correct |
85 ms |
73184 KB |
Output is correct |
80 |
Correct |
81 ms |
73184 KB |
Output is correct |
81 |
Correct |
85 ms |
73444 KB |
Output is correct |
82 |
Correct |
83 ms |
73444 KB |
Output is correct |
83 |
Correct |
87 ms |
73444 KB |
Output is correct |
84 |
Correct |
80 ms |
73444 KB |
Output is correct |
85 |
Correct |
85 ms |
73444 KB |
Output is correct |
86 |
Correct |
79 ms |
73444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
68984 KB |
Output is correct |
2 |
Correct |
71 ms |
68984 KB |
Output is correct |
3 |
Correct |
75 ms |
69180 KB |
Output is correct |
4 |
Correct |
67 ms |
69180 KB |
Output is correct |
5 |
Correct |
74 ms |
69244 KB |
Output is correct |
6 |
Correct |
76 ms |
69244 KB |
Output is correct |
7 |
Correct |
90 ms |
69272 KB |
Output is correct |
8 |
Correct |
67 ms |
69272 KB |
Output is correct |
9 |
Correct |
70 ms |
69272 KB |
Output is correct |
10 |
Correct |
70 ms |
69272 KB |
Output is correct |
11 |
Correct |
72 ms |
69272 KB |
Output is correct |
12 |
Correct |
73 ms |
69272 KB |
Output is correct |
13 |
Correct |
76 ms |
69304 KB |
Output is correct |
14 |
Correct |
72 ms |
69304 KB |
Output is correct |
15 |
Correct |
73 ms |
69304 KB |
Output is correct |
16 |
Correct |
87 ms |
69376 KB |
Output is correct |
17 |
Correct |
75 ms |
69376 KB |
Output is correct |
18 |
Correct |
92 ms |
69376 KB |
Output is correct |
19 |
Correct |
82 ms |
69376 KB |
Output is correct |
20 |
Correct |
72 ms |
69376 KB |
Output is correct |
21 |
Correct |
77 ms |
69376 KB |
Output is correct |
22 |
Correct |
73 ms |
69376 KB |
Output is correct |
23 |
Correct |
73 ms |
69376 KB |
Output is correct |
24 |
Correct |
77 ms |
69376 KB |
Output is correct |
25 |
Correct |
85 ms |
69376 KB |
Output is correct |
26 |
Correct |
70 ms |
69376 KB |
Output is correct |
27 |
Correct |
76 ms |
69376 KB |
Output is correct |
28 |
Correct |
74 ms |
69376 KB |
Output is correct |
29 |
Correct |
75 ms |
69376 KB |
Output is correct |
30 |
Correct |
77 ms |
69376 KB |
Output is correct |
31 |
Correct |
76 ms |
69376 KB |
Output is correct |
32 |
Correct |
76 ms |
69380 KB |
Output is correct |
33 |
Correct |
77 ms |
69380 KB |
Output is correct |
34 |
Correct |
75 ms |
69380 KB |
Output is correct |
35 |
Correct |
76 ms |
69380 KB |
Output is correct |
36 |
Correct |
73 ms |
69380 KB |
Output is correct |
37 |
Correct |
76 ms |
69380 KB |
Output is correct |
38 |
Correct |
78 ms |
69496 KB |
Output is correct |
39 |
Correct |
73 ms |
69496 KB |
Output is correct |
40 |
Correct |
76 ms |
69496 KB |
Output is correct |
41 |
Correct |
76 ms |
69648 KB |
Output is correct |
42 |
Correct |
75 ms |
69648 KB |
Output is correct |
43 |
Correct |
75 ms |
69648 KB |
Output is correct |
44 |
Correct |
75 ms |
69648 KB |
Output is correct |
45 |
Correct |
74 ms |
69680 KB |
Output is correct |
46 |
Correct |
87 ms |
70984 KB |
Output is correct |
47 |
Correct |
85 ms |
71184 KB |
Output is correct |
48 |
Correct |
103 ms |
71184 KB |
Output is correct |
49 |
Correct |
80 ms |
71184 KB |
Output is correct |
50 |
Correct |
74 ms |
71184 KB |
Output is correct |
51 |
Correct |
98 ms |
71184 KB |
Output is correct |
52 |
Correct |
86 ms |
71652 KB |
Output is correct |
53 |
Correct |
81 ms |
71680 KB |
Output is correct |
54 |
Correct |
83 ms |
71728 KB |
Output is correct |
55 |
Correct |
85 ms |
71820 KB |
Output is correct |
56 |
Correct |
86 ms |
72024 KB |
Output is correct |
57 |
Correct |
89 ms |
72024 KB |
Output is correct |
58 |
Correct |
89 ms |
72024 KB |
Output is correct |
59 |
Correct |
88 ms |
72104 KB |
Output is correct |
60 |
Correct |
94 ms |
72104 KB |
Output is correct |
61 |
Correct |
83 ms |
72256 KB |
Output is correct |
62 |
Correct |
88 ms |
72340 KB |
Output is correct |
63 |
Correct |
83 ms |
72408 KB |
Output is correct |
64 |
Correct |
85 ms |
72408 KB |
Output is correct |
65 |
Correct |
83 ms |
72408 KB |
Output is correct |
66 |
Correct |
88 ms |
72408 KB |
Output is correct |
67 |
Correct |
82 ms |
72408 KB |
Output is correct |
68 |
Correct |
72 ms |
72408 KB |
Output is correct |
69 |
Correct |
85 ms |
72880 KB |
Output is correct |
70 |
Correct |
81 ms |
72880 KB |
Output is correct |
71 |
Correct |
82 ms |
72880 KB |
Output is correct |
72 |
Correct |
89 ms |
72880 KB |
Output is correct |
73 |
Correct |
87 ms |
72880 KB |
Output is correct |
74 |
Correct |
77 ms |
72880 KB |
Output is correct |
75 |
Correct |
88 ms |
72928 KB |
Output is correct |
76 |
Correct |
88 ms |
72992 KB |
Output is correct |
77 |
Correct |
86 ms |
73184 KB |
Output is correct |
78 |
Correct |
83 ms |
73184 KB |
Output is correct |
79 |
Correct |
85 ms |
73184 KB |
Output is correct |
80 |
Correct |
81 ms |
73184 KB |
Output is correct |
81 |
Correct |
85 ms |
73444 KB |
Output is correct |
82 |
Correct |
83 ms |
73444 KB |
Output is correct |
83 |
Correct |
87 ms |
73444 KB |
Output is correct |
84 |
Correct |
80 ms |
73444 KB |
Output is correct |
85 |
Correct |
85 ms |
73444 KB |
Output is correct |
86 |
Correct |
79 ms |
73444 KB |
Output is correct |
87 |
Correct |
109 ms |
76304 KB |
Output is correct |
88 |
Correct |
219 ms |
85192 KB |
Output is correct |
89 |
Correct |
613 ms |
116300 KB |
Output is correct |
90 |
Correct |
555 ms |
118492 KB |
Output is correct |
91 |
Correct |
585 ms |
119324 KB |
Output is correct |
92 |
Correct |
332 ms |
119324 KB |
Output is correct |
93 |
Correct |
309 ms |
119324 KB |
Output is correct |
94 |
Correct |
305 ms |
119324 KB |
Output is correct |
95 |
Correct |
377 ms |
123964 KB |
Output is correct |
96 |
Correct |
394 ms |
124072 KB |
Output is correct |
97 |
Correct |
392 ms |
124116 KB |
Output is correct |
98 |
Correct |
402 ms |
124116 KB |
Output is correct |
99 |
Correct |
453 ms |
124116 KB |
Output is correct |
100 |
Correct |
704 ms |
124116 KB |
Output is correct |
101 |
Correct |
764 ms |
124116 KB |
Output is correct |
102 |
Correct |
774 ms |
124116 KB |
Output is correct |
103 |
Correct |
781 ms |
124116 KB |
Output is correct |
104 |
Correct |
411 ms |
124116 KB |
Output is correct |
105 |
Correct |
415 ms |
124116 KB |
Output is correct |
106 |
Correct |
416 ms |
124116 KB |
Output is correct |
107 |
Correct |
436 ms |
124116 KB |
Output is correct |
108 |
Correct |
442 ms |
124116 KB |
Output is correct |
109 |
Correct |
512 ms |
124116 KB |
Output is correct |
110 |
Correct |
202 ms |
124116 KB |
Output is correct |
111 |
Correct |
410 ms |
124116 KB |
Output is correct |
112 |
Correct |
279 ms |
124116 KB |
Output is correct |
113 |
Correct |
275 ms |
124116 KB |
Output is correct |
114 |
Correct |
702 ms |
124116 KB |
Output is correct |
115 |
Correct |
640 ms |
124116 KB |
Output is correct |
116 |
Correct |
285 ms |
124116 KB |
Output is correct |
117 |
Correct |
396 ms |
124116 KB |
Output is correct |
118 |
Correct |
399 ms |
124116 KB |
Output is correct |
119 |
Correct |
447 ms |
124116 KB |
Output is correct |
120 |
Correct |
269 ms |
124116 KB |
Output is correct |
121 |
Correct |
269 ms |
124116 KB |
Output is correct |
122 |
Correct |
277 ms |
124116 KB |
Output is correct |
123 |
Correct |
399 ms |
124116 KB |
Output is correct |
124 |
Correct |
399 ms |
124116 KB |
Output is correct |
125 |
Correct |
419 ms |
124116 KB |
Output is correct |
126 |
Correct |
280 ms |
124116 KB |
Output is correct |
127 |
Correct |
286 ms |
124116 KB |
Output is correct |
128 |
Correct |
282 ms |
124116 KB |
Output is correct |