#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000001;
const int SIZE = 1<<17;
#define ff first
#define ss second
int A[MAXN], B[MAXN], C[MAXN], dth[MAXN], prt[17][MAXN], dfn[MAXN], edfn[MAXN], dfnn;
vector<int> edge[100001];
void dfs(int x){
dfn[x] = ++dfnn;
for(auto &i: edge[x]){
prt[0][i] = x;
dth[i] = dth[x]+1;
dfs(i);
}
edfn[x] = dfnn;
}
struct ST{
int data[2*SIZE];
void update(int x, int v){
for(x+=SIZE; x; x>>=1)
data[x] = max(data[x], v);
}
int query(int s, int e){
int ret = 0;
for(s+=SIZE, e+=SIZE; s<=e; s>>=1, e>>=1){
if(s&1) ret = max(ret, data[s++]);
if(~e&1) ret = max(ret, data[e--]);
}
return ret;
}
int lb(int x, int v){
for(x+=SIZE; data[x]<=v; x=(x-1)/2);
for(; x<SIZE; x = x<<1|(data[x<<1|1]>v));
return x-SIZE;
}
int rb(int x, int v){
for(x+=SIZE; data[x]<=v; x=(x+1)/2);
for(; x<SIZE; x = (x<<1|1)-(data[x<<1]>v));
return x-SIZE;
}
} seg;
struct BIT{
int data[SIZE+1];
void update(int x, int v){
for(; x<=SIZE; x+=x&-x) data[x] += v;
}
int query(int x){
int ret=0;
for(; x; x-=x&-x) ret+= data[x];
return ret;
}
} bit;
vector<int> cc;
int main(){
int N;
scanf("%d",&N);
for(int i=1; i<=N; ++i) scanf("%d",C+i), cc.push_back(C[i]);
sort(cc.begin(), cc.end());
cc.erase(unique(cc.begin(), cc.end()), cc.end());
for(int i=1; i<=N; ++i) C[i] = lower_bound(cc.begin(), cc.end(), C[i])-cc.begin()+1;
for(int i=1; i<N; ++i){
scanf("%d%d",A+i,B+i);
edge[A[i]].push_back(B[i]);
}
dth[1] = 1;
dfs(1);
for(int i=1; i<17; ++i)for(int j=1; j<=N; ++j) prt[i][j] = prt[i-1][prt[i-1][j]];
seg.update(0, 1e9); seg.update(N+1, 1e9); seg.update(1,1);
for(int i=1; i<N; ++i){
int x = A[i];
long long ans = 0;
vector<pair<int, int> > vec;
while(x){
int t = seg.query(dfn[x], edfn[x]);
int l = seg.lb(dfn[x], t), r = seg.rb(edfn[x], t);
int y = x;
for(int j=16; j>=0; --j) if(l<dfn[prt[j][x]] && r>edfn[prt[j][x]]) x = prt[j][x];
x = prt[0][x];
vec.push_back({C[B[t]], dth[y]-dth[x]});
}
for(auto &i: vec){
ans += 1ll*bit.query(i.ff-1)*i.ss;
bit.update(i.ff, i.ss);
}
printf("%lld\n",ans);
for(auto &i: vec) bit.update(i.ff, -i.ss);
seg.update(dfn[B[i]], i);
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
construction.cpp:58:41: 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), cc.push_back(C[i]);
~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
construction.cpp:63:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",A+i,B+i);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2936 KB |
Output is correct |
2 |
Correct |
3 ms |
2936 KB |
Output is correct |
3 |
Correct |
4 ms |
2936 KB |
Output is correct |
4 |
Correct |
4 ms |
2936 KB |
Output is correct |
5 |
Correct |
4 ms |
3064 KB |
Output is correct |
6 |
Correct |
4 ms |
2936 KB |
Output is correct |
7 |
Correct |
4 ms |
3064 KB |
Output is correct |
8 |
Correct |
4 ms |
2936 KB |
Output is correct |
9 |
Correct |
4 ms |
3064 KB |
Output is correct |
10 |
Correct |
4 ms |
3064 KB |
Output is correct |
11 |
Correct |
4 ms |
3068 KB |
Output is correct |
12 |
Correct |
4 ms |
3064 KB |
Output is correct |
13 |
Correct |
4 ms |
3068 KB |
Output is correct |
14 |
Correct |
4 ms |
3064 KB |
Output is correct |
15 |
Correct |
4 ms |
2972 KB |
Output is correct |
16 |
Correct |
4 ms |
2936 KB |
Output is correct |
17 |
Correct |
4 ms |
2936 KB |
Output is correct |
18 |
Correct |
7 ms |
2936 KB |
Output is correct |
19 |
Correct |
4 ms |
2936 KB |
Output is correct |
20 |
Correct |
4 ms |
3064 KB |
Output is correct |
21 |
Correct |
4 ms |
3064 KB |
Output is correct |
22 |
Correct |
4 ms |
2936 KB |
Output is correct |
23 |
Correct |
5 ms |
3064 KB |
Output is correct |
24 |
Correct |
4 ms |
3064 KB |
Output is correct |
25 |
Correct |
4 ms |
3064 KB |
Output is correct |
26 |
Correct |
4 ms |
3064 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
4 ms |
2936 KB |
Output is correct |
29 |
Correct |
4 ms |
2936 KB |
Output is correct |
30 |
Correct |
5 ms |
2936 KB |
Output is correct |
31 |
Correct |
5 ms |
3064 KB |
Output is correct |
32 |
Correct |
4 ms |
2936 KB |
Output is correct |
33 |
Correct |
4 ms |
2908 KB |
Output is correct |
34 |
Correct |
4 ms |
2936 KB |
Output is correct |
35 |
Correct |
5 ms |
2936 KB |
Output is correct |
36 |
Correct |
4 ms |
2936 KB |
Output is correct |
37 |
Correct |
4 ms |
2936 KB |
Output is correct |
38 |
Correct |
4 ms |
2936 KB |
Output is correct |
39 |
Correct |
4 ms |
2936 KB |
Output is correct |
40 |
Correct |
4 ms |
2936 KB |
Output is correct |
41 |
Correct |
5 ms |
2936 KB |
Output is correct |
42 |
Correct |
4 ms |
3064 KB |
Output is correct |
43 |
Correct |
4 ms |
2936 KB |
Output is correct |
44 |
Correct |
4 ms |
2936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2936 KB |
Output is correct |
2 |
Correct |
3 ms |
2936 KB |
Output is correct |
3 |
Correct |
4 ms |
2936 KB |
Output is correct |
4 |
Correct |
4 ms |
2936 KB |
Output is correct |
5 |
Correct |
4 ms |
3064 KB |
Output is correct |
6 |
Correct |
4 ms |
2936 KB |
Output is correct |
7 |
Correct |
4 ms |
3064 KB |
Output is correct |
8 |
Correct |
4 ms |
2936 KB |
Output is correct |
9 |
Correct |
4 ms |
3064 KB |
Output is correct |
10 |
Correct |
4 ms |
3064 KB |
Output is correct |
11 |
Correct |
4 ms |
3068 KB |
Output is correct |
12 |
Correct |
4 ms |
3064 KB |
Output is correct |
13 |
Correct |
4 ms |
3068 KB |
Output is correct |
14 |
Correct |
4 ms |
3064 KB |
Output is correct |
15 |
Correct |
4 ms |
2972 KB |
Output is correct |
16 |
Correct |
4 ms |
2936 KB |
Output is correct |
17 |
Correct |
4 ms |
2936 KB |
Output is correct |
18 |
Correct |
7 ms |
2936 KB |
Output is correct |
19 |
Correct |
4 ms |
2936 KB |
Output is correct |
20 |
Correct |
4 ms |
3064 KB |
Output is correct |
21 |
Correct |
4 ms |
3064 KB |
Output is correct |
22 |
Correct |
4 ms |
2936 KB |
Output is correct |
23 |
Correct |
5 ms |
3064 KB |
Output is correct |
24 |
Correct |
4 ms |
3064 KB |
Output is correct |
25 |
Correct |
4 ms |
3064 KB |
Output is correct |
26 |
Correct |
4 ms |
3064 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
4 ms |
2936 KB |
Output is correct |
29 |
Correct |
4 ms |
2936 KB |
Output is correct |
30 |
Correct |
5 ms |
2936 KB |
Output is correct |
31 |
Correct |
5 ms |
3064 KB |
Output is correct |
32 |
Correct |
4 ms |
2936 KB |
Output is correct |
33 |
Correct |
4 ms |
2908 KB |
Output is correct |
34 |
Correct |
4 ms |
2936 KB |
Output is correct |
35 |
Correct |
5 ms |
2936 KB |
Output is correct |
36 |
Correct |
4 ms |
2936 KB |
Output is correct |
37 |
Correct |
4 ms |
2936 KB |
Output is correct |
38 |
Correct |
4 ms |
2936 KB |
Output is correct |
39 |
Correct |
4 ms |
2936 KB |
Output is correct |
40 |
Correct |
4 ms |
2936 KB |
Output is correct |
41 |
Correct |
5 ms |
2936 KB |
Output is correct |
42 |
Correct |
4 ms |
3064 KB |
Output is correct |
43 |
Correct |
4 ms |
2936 KB |
Output is correct |
44 |
Correct |
4 ms |
2936 KB |
Output is correct |
45 |
Correct |
5 ms |
3064 KB |
Output is correct |
46 |
Correct |
11 ms |
3448 KB |
Output is correct |
47 |
Correct |
11 ms |
3448 KB |
Output is correct |
48 |
Correct |
11 ms |
3448 KB |
Output is correct |
49 |
Correct |
8 ms |
3704 KB |
Output is correct |
50 |
Correct |
8 ms |
3704 KB |
Output is correct |
51 |
Correct |
8 ms |
3704 KB |
Output is correct |
52 |
Correct |
9 ms |
3576 KB |
Output is correct |
53 |
Correct |
9 ms |
3704 KB |
Output is correct |
54 |
Correct |
9 ms |
3576 KB |
Output is correct |
55 |
Correct |
9 ms |
3576 KB |
Output is correct |
56 |
Correct |
9 ms |
3576 KB |
Output is correct |
57 |
Correct |
15 ms |
3448 KB |
Output is correct |
58 |
Correct |
16 ms |
3448 KB |
Output is correct |
59 |
Correct |
15 ms |
3452 KB |
Output is correct |
60 |
Correct |
15 ms |
3448 KB |
Output is correct |
61 |
Correct |
9 ms |
3576 KB |
Output is correct |
62 |
Correct |
9 ms |
3576 KB |
Output is correct |
63 |
Correct |
9 ms |
3576 KB |
Output is correct |
64 |
Correct |
11 ms |
3448 KB |
Output is correct |
65 |
Correct |
11 ms |
3448 KB |
Output is correct |
66 |
Correct |
12 ms |
3448 KB |
Output is correct |
67 |
Correct |
11 ms |
3576 KB |
Output is correct |
68 |
Correct |
8 ms |
3704 KB |
Output is correct |
69 |
Correct |
9 ms |
3576 KB |
Output is correct |
70 |
Correct |
9 ms |
3576 KB |
Output is correct |
71 |
Correct |
9 ms |
3576 KB |
Output is correct |
72 |
Correct |
15 ms |
3448 KB |
Output is correct |
73 |
Correct |
15 ms |
3448 KB |
Output is correct |
74 |
Correct |
10 ms |
3448 KB |
Output is correct |
75 |
Correct |
9 ms |
3528 KB |
Output is correct |
76 |
Correct |
9 ms |
3448 KB |
Output is correct |
77 |
Correct |
9 ms |
3448 KB |
Output is correct |
78 |
Correct |
8 ms |
3448 KB |
Output is correct |
79 |
Correct |
9 ms |
3448 KB |
Output is correct |
80 |
Correct |
9 ms |
3448 KB |
Output is correct |
81 |
Correct |
10 ms |
3448 KB |
Output is correct |
82 |
Correct |
10 ms |
3448 KB |
Output is correct |
83 |
Correct |
10 ms |
3448 KB |
Output is correct |
84 |
Correct |
9 ms |
3448 KB |
Output is correct |
85 |
Correct |
9 ms |
3448 KB |
Output is correct |
86 |
Correct |
9 ms |
3448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2936 KB |
Output is correct |
2 |
Correct |
3 ms |
2936 KB |
Output is correct |
3 |
Correct |
4 ms |
2936 KB |
Output is correct |
4 |
Correct |
4 ms |
2936 KB |
Output is correct |
5 |
Correct |
4 ms |
3064 KB |
Output is correct |
6 |
Correct |
4 ms |
2936 KB |
Output is correct |
7 |
Correct |
4 ms |
3064 KB |
Output is correct |
8 |
Correct |
4 ms |
2936 KB |
Output is correct |
9 |
Correct |
4 ms |
3064 KB |
Output is correct |
10 |
Correct |
4 ms |
3064 KB |
Output is correct |
11 |
Correct |
4 ms |
3068 KB |
Output is correct |
12 |
Correct |
4 ms |
3064 KB |
Output is correct |
13 |
Correct |
4 ms |
3068 KB |
Output is correct |
14 |
Correct |
4 ms |
3064 KB |
Output is correct |
15 |
Correct |
4 ms |
2972 KB |
Output is correct |
16 |
Correct |
4 ms |
2936 KB |
Output is correct |
17 |
Correct |
4 ms |
2936 KB |
Output is correct |
18 |
Correct |
7 ms |
2936 KB |
Output is correct |
19 |
Correct |
4 ms |
2936 KB |
Output is correct |
20 |
Correct |
4 ms |
3064 KB |
Output is correct |
21 |
Correct |
4 ms |
3064 KB |
Output is correct |
22 |
Correct |
4 ms |
2936 KB |
Output is correct |
23 |
Correct |
5 ms |
3064 KB |
Output is correct |
24 |
Correct |
4 ms |
3064 KB |
Output is correct |
25 |
Correct |
4 ms |
3064 KB |
Output is correct |
26 |
Correct |
4 ms |
3064 KB |
Output is correct |
27 |
Correct |
4 ms |
3064 KB |
Output is correct |
28 |
Correct |
4 ms |
2936 KB |
Output is correct |
29 |
Correct |
4 ms |
2936 KB |
Output is correct |
30 |
Correct |
5 ms |
2936 KB |
Output is correct |
31 |
Correct |
5 ms |
3064 KB |
Output is correct |
32 |
Correct |
4 ms |
2936 KB |
Output is correct |
33 |
Correct |
4 ms |
2908 KB |
Output is correct |
34 |
Correct |
4 ms |
2936 KB |
Output is correct |
35 |
Correct |
5 ms |
2936 KB |
Output is correct |
36 |
Correct |
4 ms |
2936 KB |
Output is correct |
37 |
Correct |
4 ms |
2936 KB |
Output is correct |
38 |
Correct |
4 ms |
2936 KB |
Output is correct |
39 |
Correct |
4 ms |
2936 KB |
Output is correct |
40 |
Correct |
4 ms |
2936 KB |
Output is correct |
41 |
Correct |
5 ms |
2936 KB |
Output is correct |
42 |
Correct |
4 ms |
3064 KB |
Output is correct |
43 |
Correct |
4 ms |
2936 KB |
Output is correct |
44 |
Correct |
4 ms |
2936 KB |
Output is correct |
45 |
Correct |
5 ms |
3064 KB |
Output is correct |
46 |
Correct |
11 ms |
3448 KB |
Output is correct |
47 |
Correct |
11 ms |
3448 KB |
Output is correct |
48 |
Correct |
11 ms |
3448 KB |
Output is correct |
49 |
Correct |
8 ms |
3704 KB |
Output is correct |
50 |
Correct |
8 ms |
3704 KB |
Output is correct |
51 |
Correct |
8 ms |
3704 KB |
Output is correct |
52 |
Correct |
9 ms |
3576 KB |
Output is correct |
53 |
Correct |
9 ms |
3704 KB |
Output is correct |
54 |
Correct |
9 ms |
3576 KB |
Output is correct |
55 |
Correct |
9 ms |
3576 KB |
Output is correct |
56 |
Correct |
9 ms |
3576 KB |
Output is correct |
57 |
Correct |
15 ms |
3448 KB |
Output is correct |
58 |
Correct |
16 ms |
3448 KB |
Output is correct |
59 |
Correct |
15 ms |
3452 KB |
Output is correct |
60 |
Correct |
15 ms |
3448 KB |
Output is correct |
61 |
Correct |
9 ms |
3576 KB |
Output is correct |
62 |
Correct |
9 ms |
3576 KB |
Output is correct |
63 |
Correct |
9 ms |
3576 KB |
Output is correct |
64 |
Correct |
11 ms |
3448 KB |
Output is correct |
65 |
Correct |
11 ms |
3448 KB |
Output is correct |
66 |
Correct |
12 ms |
3448 KB |
Output is correct |
67 |
Correct |
11 ms |
3576 KB |
Output is correct |
68 |
Correct |
8 ms |
3704 KB |
Output is correct |
69 |
Correct |
9 ms |
3576 KB |
Output is correct |
70 |
Correct |
9 ms |
3576 KB |
Output is correct |
71 |
Correct |
9 ms |
3576 KB |
Output is correct |
72 |
Correct |
15 ms |
3448 KB |
Output is correct |
73 |
Correct |
15 ms |
3448 KB |
Output is correct |
74 |
Correct |
10 ms |
3448 KB |
Output is correct |
75 |
Correct |
9 ms |
3528 KB |
Output is correct |
76 |
Correct |
9 ms |
3448 KB |
Output is correct |
77 |
Correct |
9 ms |
3448 KB |
Output is correct |
78 |
Correct |
8 ms |
3448 KB |
Output is correct |
79 |
Correct |
9 ms |
3448 KB |
Output is correct |
80 |
Correct |
9 ms |
3448 KB |
Output is correct |
81 |
Correct |
10 ms |
3448 KB |
Output is correct |
82 |
Correct |
10 ms |
3448 KB |
Output is correct |
83 |
Correct |
10 ms |
3448 KB |
Output is correct |
84 |
Correct |
9 ms |
3448 KB |
Output is correct |
85 |
Correct |
9 ms |
3448 KB |
Output is correct |
86 |
Correct |
9 ms |
3448 KB |
Output is correct |
87 |
Correct |
26 ms |
4344 KB |
Output is correct |
88 |
Correct |
85 ms |
7288 KB |
Output is correct |
89 |
Correct |
432 ms |
16084 KB |
Output is correct |
90 |
Correct |
409 ms |
15984 KB |
Output is correct |
91 |
Correct |
445 ms |
16184 KB |
Output is correct |
92 |
Correct |
180 ms |
22128 KB |
Output is correct |
93 |
Correct |
179 ms |
22124 KB |
Output is correct |
94 |
Correct |
198 ms |
22204 KB |
Output is correct |
95 |
Correct |
304 ms |
19024 KB |
Output is correct |
96 |
Correct |
299 ms |
18928 KB |
Output is correct |
97 |
Correct |
308 ms |
18928 KB |
Output is correct |
98 |
Correct |
304 ms |
18920 KB |
Output is correct |
99 |
Correct |
302 ms |
18496 KB |
Output is correct |
100 |
Correct |
702 ms |
16728 KB |
Output is correct |
101 |
Correct |
799 ms |
16724 KB |
Output is correct |
102 |
Correct |
781 ms |
16476 KB |
Output is correct |
103 |
Correct |
778 ms |
16412 KB |
Output is correct |
104 |
Correct |
297 ms |
18312 KB |
Output is correct |
105 |
Correct |
293 ms |
18432 KB |
Output is correct |
106 |
Correct |
327 ms |
18364 KB |
Output is correct |
107 |
Correct |
364 ms |
15616 KB |
Output is correct |
108 |
Correct |
373 ms |
15636 KB |
Output is correct |
109 |
Correct |
451 ms |
15788 KB |
Output is correct |
110 |
Correct |
173 ms |
21740 KB |
Output is correct |
111 |
Correct |
328 ms |
18980 KB |
Output is correct |
112 |
Correct |
317 ms |
18520 KB |
Output is correct |
113 |
Correct |
318 ms |
18012 KB |
Output is correct |
114 |
Correct |
788 ms |
16528 KB |
Output is correct |
115 |
Correct |
724 ms |
16092 KB |
Output is correct |
116 |
Correct |
313 ms |
17920 KB |
Output is correct |
117 |
Correct |
312 ms |
17272 KB |
Output is correct |
118 |
Correct |
279 ms |
16784 KB |
Output is correct |
119 |
Correct |
278 ms |
16512 KB |
Output is correct |
120 |
Correct |
305 ms |
16736 KB |
Output is correct |
121 |
Correct |
282 ms |
16340 KB |
Output is correct |
122 |
Correct |
288 ms |
16040 KB |
Output is correct |
123 |
Correct |
317 ms |
16896 KB |
Output is correct |
124 |
Correct |
287 ms |
16424 KB |
Output is correct |
125 |
Correct |
280 ms |
16056 KB |
Output is correct |
126 |
Correct |
274 ms |
16352 KB |
Output is correct |
127 |
Correct |
269 ms |
15968 KB |
Output is correct |
128 |
Correct |
267 ms |
15768 KB |
Output is correct |