#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
typedef long long lint;
typedef pair<int, int> pi;
vector<int> gph[MAXN];
int n, s[MAXN], e[MAXN], a[MAXN];
int dep[MAXN], din[MAXN], dout[MAXN], piv;
int ddin[MAXN], ddout[MAXN], pivv;
pi spt[18][2 * MAXN];
int lg[2 * MAXN];
int lca(int s, int e){
if(ddin[s] > ddin[e]) swap(s, e);
if(ddout[e] <= ddout[s]) return s;
int st = ddout[s], ed = ddin[e];
int l = lg[ed - st + 1];
return min(spt[l][st], spt[l][ed - (1<<l) + 1]).second;
}
void dfs(int x, int p){
din[x] = ++piv;
ddin[x] = ++pivv;
spt[0][pivv] = pi(dep[x] - 1, p);
for(auto &i : gph[x]){
if(i != p){
dep[i] = dep[x] + 1;
dfs(i, x);
}
}
dout[x] = piv;
ddout[x] = ++pivv;
spt[0][pivv] = pi(dep[x] - 1, p);
}
struct segtree{
int tree[270000], lim;
void init(){
memset(tree, 0, sizeof(tree));
for(lim = 1; lim <= n; lim <<= 1);
}
void upd(int x, int v){
x += lim;
tree[x] = v;
while(x > 1){
x >>= 1;
tree[x] = max(tree[2*x], tree[2*x+1]);
}
}
int query(int s, int e){
s += lim;
e += lim;
int ret = 0;
while(s < e){
if(s%2 == 1) ret = max(ret, tree[s++]);
if(e%2 == 0) ret = max(ret, tree[e--]);
s >>= 1;
e >>= 1;
}
if(s == e) ret = max(ret, tree[s]);
return ret;
}
int left_max(int s, int e, int thres){
s += lim;
e += lim;
while(s < e){
if(s % 2 == 1) s++;
if(e % 2 == 0){
if(tree[e] > thres){
while(e < lim){
if(tree[2*e+1] > thres) e = 2 * e + 1;
else e = 2 * e;
}
return tree[e];
}
e--;
}
s >>= 1;
e >>= 1;
}
if(s == e && tree[e] > thres){
while(e < lim){
if(tree[2*e+1] > thres) e = 2 * e + 1;
else e = 2 * e;
}
return tree[e];
}
return 0;
}
int right_max(int s, int e, int thres){
s += lim;
e += lim;
while(s < e){
if(s % 2 == 1){
if(tree[s] > thres){
while(s < lim){
if(tree[2*s] > thres) s = 2 * s;
else s = 2 * s + 1;
}
return tree[s];
}
s++;
}
if(e % 2 == 0) e--;
s >>= 1;
e >>= 1;
}
if(s == e && tree[s] > thres){
while(s < lim){
if(tree[2*s] > thres) s = 2 * s;
else s = 2 * s + 1;
}
return tree[s];
}
return 0;
}
};
struct bit{
int tree[MAXN];
void add(int x, int v){
while(x <= n){
tree[x] += v;
x += x & -x;
}
}
int query(int x){
int ret = 0;
while(x){
ret += tree[x];
x -= x & -x;
}
return ret;
}
}bit;
int main(){
for(int i=1; i<2*MAXN; i++){
lg[i] = lg[i-1];
while((2 << lg[i]) <= i) lg[i]++;
}
vector<int> v;
scanf("%d",&n);
for(int i=1; i<=n; i++){
scanf("%d",&a[i]);
v.push_back(a[i]);
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for(int i=1; i<=n; i++) a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;
for(int i=2; i<=n; i++){
scanf("%d %d",&s[i],&e[i]);
gph[s[i]].push_back(e[i]);
gph[e[i]].push_back(s[i]);
}
segtree seg;
seg.init();
dfs(1, -1);
for(int i=1; i<18; i++){
for(int j=1; j<=2*n; j++){
spt[i][j] = spt[i-1][j];
if(j + (1<<(i-1)) <= 2 * n){
spt[i][j] = min(spt[i][j], spt[i-1][j + (1<<(i-1))]);
}
}
}
e[1] = 1;
seg.upd(din[1], 1);
for(int i=2; i<=n; i++){
int pos = s[i];
vector<pi> mem;
int x;
while((x = seg.query(din[pos], dout[pos])) != seg.tree[1]){
int pl = seg.left_max(0, din[pos] - 1, x);
int pr = seg.right_max(dout[pos] + 1, seg.lim-1, x);
int nxtpos = 1;
if(pl > x){
int aux = lca(pos, e[pl]);
if(dep[aux] > dep[nxtpos]) nxtpos = aux;
}
if(pr > x){
int aux = lca(pos, e[pr]);
if(dep[aux] > dep[nxtpos]) nxtpos = aux;
}
mem.push_back(pi(dep[pos] - dep[nxtpos], a[e[x]]));
pos = nxtpos;
}
lint ans = 0;
for(auto &i : mem){
ans += 1ll * bit.query(i.second - 1) * i.first;
bit.add(i.second, i.first);
}
ans += 1ll * bit.query(a[e[seg.tree[1]]] - 1) * (dep[pos] + 1);
printf("%lld\n", ans);
for(auto &i : mem) bit.add(i.second, -i.first);
seg.upd(din[e[i]], i);
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:144:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
construction.cpp:146:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i]);
~~~~~^~~~~~~~~~~~
construction.cpp:153:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d",&s[i],&e[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4600 KB |
Output is correct |
2 |
Correct |
6 ms |
4728 KB |
Output is correct |
3 |
Correct |
6 ms |
4728 KB |
Output is correct |
4 |
Correct |
7 ms |
4728 KB |
Output is correct |
5 |
Correct |
7 ms |
4856 KB |
Output is correct |
6 |
Correct |
7 ms |
4856 KB |
Output is correct |
7 |
Correct |
7 ms |
4856 KB |
Output is correct |
8 |
Correct |
6 ms |
4856 KB |
Output is correct |
9 |
Correct |
6 ms |
4856 KB |
Output is correct |
10 |
Correct |
6 ms |
4856 KB |
Output is correct |
11 |
Correct |
7 ms |
4856 KB |
Output is correct |
12 |
Correct |
7 ms |
4856 KB |
Output is correct |
13 |
Correct |
6 ms |
4856 KB |
Output is correct |
14 |
Correct |
7 ms |
4856 KB |
Output is correct |
15 |
Correct |
7 ms |
4856 KB |
Output is correct |
16 |
Correct |
7 ms |
4856 KB |
Output is correct |
17 |
Correct |
7 ms |
4856 KB |
Output is correct |
18 |
Correct |
7 ms |
4856 KB |
Output is correct |
19 |
Correct |
6 ms |
4856 KB |
Output is correct |
20 |
Correct |
7 ms |
4828 KB |
Output is correct |
21 |
Correct |
7 ms |
4856 KB |
Output is correct |
22 |
Correct |
7 ms |
4856 KB |
Output is correct |
23 |
Correct |
7 ms |
4856 KB |
Output is correct |
24 |
Correct |
7 ms |
4856 KB |
Output is correct |
25 |
Correct |
7 ms |
4856 KB |
Output is correct |
26 |
Correct |
6 ms |
4856 KB |
Output is correct |
27 |
Correct |
6 ms |
4828 KB |
Output is correct |
28 |
Correct |
6 ms |
4832 KB |
Output is correct |
29 |
Correct |
6 ms |
4856 KB |
Output is correct |
30 |
Correct |
7 ms |
4856 KB |
Output is correct |
31 |
Correct |
7 ms |
4856 KB |
Output is correct |
32 |
Correct |
7 ms |
4856 KB |
Output is correct |
33 |
Correct |
7 ms |
4856 KB |
Output is correct |
34 |
Correct |
7 ms |
4856 KB |
Output is correct |
35 |
Correct |
6 ms |
4856 KB |
Output is correct |
36 |
Correct |
7 ms |
4856 KB |
Output is correct |
37 |
Correct |
7 ms |
4856 KB |
Output is correct |
38 |
Correct |
7 ms |
4856 KB |
Output is correct |
39 |
Correct |
7 ms |
4856 KB |
Output is correct |
40 |
Correct |
7 ms |
4856 KB |
Output is correct |
41 |
Correct |
7 ms |
4856 KB |
Output is correct |
42 |
Correct |
7 ms |
4856 KB |
Output is correct |
43 |
Correct |
6 ms |
4860 KB |
Output is correct |
44 |
Correct |
7 ms |
4856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4600 KB |
Output is correct |
2 |
Correct |
6 ms |
4728 KB |
Output is correct |
3 |
Correct |
6 ms |
4728 KB |
Output is correct |
4 |
Correct |
7 ms |
4728 KB |
Output is correct |
5 |
Correct |
7 ms |
4856 KB |
Output is correct |
6 |
Correct |
7 ms |
4856 KB |
Output is correct |
7 |
Correct |
7 ms |
4856 KB |
Output is correct |
8 |
Correct |
6 ms |
4856 KB |
Output is correct |
9 |
Correct |
6 ms |
4856 KB |
Output is correct |
10 |
Correct |
6 ms |
4856 KB |
Output is correct |
11 |
Correct |
7 ms |
4856 KB |
Output is correct |
12 |
Correct |
7 ms |
4856 KB |
Output is correct |
13 |
Correct |
6 ms |
4856 KB |
Output is correct |
14 |
Correct |
7 ms |
4856 KB |
Output is correct |
15 |
Correct |
7 ms |
4856 KB |
Output is correct |
16 |
Correct |
7 ms |
4856 KB |
Output is correct |
17 |
Correct |
7 ms |
4856 KB |
Output is correct |
18 |
Correct |
7 ms |
4856 KB |
Output is correct |
19 |
Correct |
6 ms |
4856 KB |
Output is correct |
20 |
Correct |
7 ms |
4828 KB |
Output is correct |
21 |
Correct |
7 ms |
4856 KB |
Output is correct |
22 |
Correct |
7 ms |
4856 KB |
Output is correct |
23 |
Correct |
7 ms |
4856 KB |
Output is correct |
24 |
Correct |
7 ms |
4856 KB |
Output is correct |
25 |
Correct |
7 ms |
4856 KB |
Output is correct |
26 |
Correct |
6 ms |
4856 KB |
Output is correct |
27 |
Correct |
6 ms |
4828 KB |
Output is correct |
28 |
Correct |
6 ms |
4832 KB |
Output is correct |
29 |
Correct |
6 ms |
4856 KB |
Output is correct |
30 |
Correct |
7 ms |
4856 KB |
Output is correct |
31 |
Correct |
7 ms |
4856 KB |
Output is correct |
32 |
Correct |
7 ms |
4856 KB |
Output is correct |
33 |
Correct |
7 ms |
4856 KB |
Output is correct |
34 |
Correct |
7 ms |
4856 KB |
Output is correct |
35 |
Correct |
6 ms |
4856 KB |
Output is correct |
36 |
Correct |
7 ms |
4856 KB |
Output is correct |
37 |
Correct |
7 ms |
4856 KB |
Output is correct |
38 |
Correct |
7 ms |
4856 KB |
Output is correct |
39 |
Correct |
7 ms |
4856 KB |
Output is correct |
40 |
Correct |
7 ms |
4856 KB |
Output is correct |
41 |
Correct |
7 ms |
4856 KB |
Output is correct |
42 |
Correct |
7 ms |
4856 KB |
Output is correct |
43 |
Correct |
6 ms |
4860 KB |
Output is correct |
44 |
Correct |
7 ms |
4856 KB |
Output is correct |
45 |
Correct |
8 ms |
4988 KB |
Output is correct |
46 |
Correct |
16 ms |
6136 KB |
Output is correct |
47 |
Correct |
16 ms |
6136 KB |
Output is correct |
48 |
Correct |
16 ms |
6136 KB |
Output is correct |
49 |
Correct |
11 ms |
6264 KB |
Output is correct |
50 |
Correct |
11 ms |
6264 KB |
Output is correct |
51 |
Correct |
11 ms |
6392 KB |
Output is correct |
52 |
Correct |
11 ms |
6264 KB |
Output is correct |
53 |
Correct |
11 ms |
6268 KB |
Output is correct |
54 |
Correct |
12 ms |
6264 KB |
Output is correct |
55 |
Correct |
12 ms |
6308 KB |
Output is correct |
56 |
Correct |
12 ms |
6264 KB |
Output is correct |
57 |
Correct |
22 ms |
6136 KB |
Output is correct |
58 |
Correct |
23 ms |
6136 KB |
Output is correct |
59 |
Correct |
22 ms |
6136 KB |
Output is correct |
60 |
Correct |
23 ms |
6136 KB |
Output is correct |
61 |
Correct |
12 ms |
6264 KB |
Output is correct |
62 |
Correct |
12 ms |
6308 KB |
Output is correct |
63 |
Correct |
12 ms |
6264 KB |
Output is correct |
64 |
Correct |
15 ms |
6136 KB |
Output is correct |
65 |
Correct |
16 ms |
6152 KB |
Output is correct |
66 |
Correct |
16 ms |
6136 KB |
Output is correct |
67 |
Correct |
16 ms |
6200 KB |
Output is correct |
68 |
Correct |
10 ms |
6264 KB |
Output is correct |
69 |
Correct |
11 ms |
6264 KB |
Output is correct |
70 |
Correct |
11 ms |
6236 KB |
Output is correct |
71 |
Correct |
11 ms |
6264 KB |
Output is correct |
72 |
Correct |
22 ms |
6136 KB |
Output is correct |
73 |
Correct |
22 ms |
6136 KB |
Output is correct |
74 |
Correct |
11 ms |
6140 KB |
Output is correct |
75 |
Correct |
12 ms |
6140 KB |
Output is correct |
76 |
Correct |
12 ms |
6136 KB |
Output is correct |
77 |
Correct |
12 ms |
6136 KB |
Output is correct |
78 |
Correct |
11 ms |
6136 KB |
Output is correct |
79 |
Correct |
11 ms |
6136 KB |
Output is correct |
80 |
Correct |
11 ms |
6136 KB |
Output is correct |
81 |
Correct |
13 ms |
6264 KB |
Output is correct |
82 |
Correct |
15 ms |
6140 KB |
Output is correct |
83 |
Correct |
15 ms |
6136 KB |
Output is correct |
84 |
Correct |
12 ms |
6108 KB |
Output is correct |
85 |
Correct |
12 ms |
6180 KB |
Output is correct |
86 |
Correct |
12 ms |
6136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4600 KB |
Output is correct |
2 |
Correct |
6 ms |
4728 KB |
Output is correct |
3 |
Correct |
6 ms |
4728 KB |
Output is correct |
4 |
Correct |
7 ms |
4728 KB |
Output is correct |
5 |
Correct |
7 ms |
4856 KB |
Output is correct |
6 |
Correct |
7 ms |
4856 KB |
Output is correct |
7 |
Correct |
7 ms |
4856 KB |
Output is correct |
8 |
Correct |
6 ms |
4856 KB |
Output is correct |
9 |
Correct |
6 ms |
4856 KB |
Output is correct |
10 |
Correct |
6 ms |
4856 KB |
Output is correct |
11 |
Correct |
7 ms |
4856 KB |
Output is correct |
12 |
Correct |
7 ms |
4856 KB |
Output is correct |
13 |
Correct |
6 ms |
4856 KB |
Output is correct |
14 |
Correct |
7 ms |
4856 KB |
Output is correct |
15 |
Correct |
7 ms |
4856 KB |
Output is correct |
16 |
Correct |
7 ms |
4856 KB |
Output is correct |
17 |
Correct |
7 ms |
4856 KB |
Output is correct |
18 |
Correct |
7 ms |
4856 KB |
Output is correct |
19 |
Correct |
6 ms |
4856 KB |
Output is correct |
20 |
Correct |
7 ms |
4828 KB |
Output is correct |
21 |
Correct |
7 ms |
4856 KB |
Output is correct |
22 |
Correct |
7 ms |
4856 KB |
Output is correct |
23 |
Correct |
7 ms |
4856 KB |
Output is correct |
24 |
Correct |
7 ms |
4856 KB |
Output is correct |
25 |
Correct |
7 ms |
4856 KB |
Output is correct |
26 |
Correct |
6 ms |
4856 KB |
Output is correct |
27 |
Correct |
6 ms |
4828 KB |
Output is correct |
28 |
Correct |
6 ms |
4832 KB |
Output is correct |
29 |
Correct |
6 ms |
4856 KB |
Output is correct |
30 |
Correct |
7 ms |
4856 KB |
Output is correct |
31 |
Correct |
7 ms |
4856 KB |
Output is correct |
32 |
Correct |
7 ms |
4856 KB |
Output is correct |
33 |
Correct |
7 ms |
4856 KB |
Output is correct |
34 |
Correct |
7 ms |
4856 KB |
Output is correct |
35 |
Correct |
6 ms |
4856 KB |
Output is correct |
36 |
Correct |
7 ms |
4856 KB |
Output is correct |
37 |
Correct |
7 ms |
4856 KB |
Output is correct |
38 |
Correct |
7 ms |
4856 KB |
Output is correct |
39 |
Correct |
7 ms |
4856 KB |
Output is correct |
40 |
Correct |
7 ms |
4856 KB |
Output is correct |
41 |
Correct |
7 ms |
4856 KB |
Output is correct |
42 |
Correct |
7 ms |
4856 KB |
Output is correct |
43 |
Correct |
6 ms |
4860 KB |
Output is correct |
44 |
Correct |
7 ms |
4856 KB |
Output is correct |
45 |
Correct |
8 ms |
4988 KB |
Output is correct |
46 |
Correct |
16 ms |
6136 KB |
Output is correct |
47 |
Correct |
16 ms |
6136 KB |
Output is correct |
48 |
Correct |
16 ms |
6136 KB |
Output is correct |
49 |
Correct |
11 ms |
6264 KB |
Output is correct |
50 |
Correct |
11 ms |
6264 KB |
Output is correct |
51 |
Correct |
11 ms |
6392 KB |
Output is correct |
52 |
Correct |
11 ms |
6264 KB |
Output is correct |
53 |
Correct |
11 ms |
6268 KB |
Output is correct |
54 |
Correct |
12 ms |
6264 KB |
Output is correct |
55 |
Correct |
12 ms |
6308 KB |
Output is correct |
56 |
Correct |
12 ms |
6264 KB |
Output is correct |
57 |
Correct |
22 ms |
6136 KB |
Output is correct |
58 |
Correct |
23 ms |
6136 KB |
Output is correct |
59 |
Correct |
22 ms |
6136 KB |
Output is correct |
60 |
Correct |
23 ms |
6136 KB |
Output is correct |
61 |
Correct |
12 ms |
6264 KB |
Output is correct |
62 |
Correct |
12 ms |
6308 KB |
Output is correct |
63 |
Correct |
12 ms |
6264 KB |
Output is correct |
64 |
Correct |
15 ms |
6136 KB |
Output is correct |
65 |
Correct |
16 ms |
6152 KB |
Output is correct |
66 |
Correct |
16 ms |
6136 KB |
Output is correct |
67 |
Correct |
16 ms |
6200 KB |
Output is correct |
68 |
Correct |
10 ms |
6264 KB |
Output is correct |
69 |
Correct |
11 ms |
6264 KB |
Output is correct |
70 |
Correct |
11 ms |
6236 KB |
Output is correct |
71 |
Correct |
11 ms |
6264 KB |
Output is correct |
72 |
Correct |
22 ms |
6136 KB |
Output is correct |
73 |
Correct |
22 ms |
6136 KB |
Output is correct |
74 |
Correct |
11 ms |
6140 KB |
Output is correct |
75 |
Correct |
12 ms |
6140 KB |
Output is correct |
76 |
Correct |
12 ms |
6136 KB |
Output is correct |
77 |
Correct |
12 ms |
6136 KB |
Output is correct |
78 |
Correct |
11 ms |
6136 KB |
Output is correct |
79 |
Correct |
11 ms |
6136 KB |
Output is correct |
80 |
Correct |
11 ms |
6136 KB |
Output is correct |
81 |
Correct |
13 ms |
6264 KB |
Output is correct |
82 |
Correct |
15 ms |
6140 KB |
Output is correct |
83 |
Correct |
15 ms |
6136 KB |
Output is correct |
84 |
Correct |
12 ms |
6108 KB |
Output is correct |
85 |
Correct |
12 ms |
6180 KB |
Output is correct |
86 |
Correct |
12 ms |
6136 KB |
Output is correct |
87 |
Correct |
38 ms |
8440 KB |
Output is correct |
88 |
Correct |
127 ms |
16108 KB |
Output is correct |
89 |
Correct |
530 ms |
42332 KB |
Output is correct |
90 |
Correct |
567 ms |
42352 KB |
Output is correct |
91 |
Correct |
528 ms |
42336 KB |
Output is correct |
92 |
Correct |
172 ms |
46504 KB |
Output is correct |
93 |
Correct |
172 ms |
46384 KB |
Output is correct |
94 |
Correct |
170 ms |
46320 KB |
Output is correct |
95 |
Correct |
183 ms |
44400 KB |
Output is correct |
96 |
Correct |
188 ms |
44924 KB |
Output is correct |
97 |
Correct |
189 ms |
45004 KB |
Output is correct |
98 |
Correct |
184 ms |
44888 KB |
Output is correct |
99 |
Correct |
191 ms |
44340 KB |
Output is correct |
100 |
Correct |
988 ms |
41860 KB |
Output is correct |
101 |
Correct |
985 ms |
42312 KB |
Output is correct |
102 |
Correct |
1146 ms |
42276 KB |
Output is correct |
103 |
Correct |
1091 ms |
42176 KB |
Output is correct |
104 |
Correct |
221 ms |
44656 KB |
Output is correct |
105 |
Correct |
221 ms |
44656 KB |
Output is correct |
106 |
Correct |
275 ms |
44656 KB |
Output is correct |
107 |
Correct |
610 ms |
41340 KB |
Output is correct |
108 |
Correct |
551 ms |
41344 KB |
Output is correct |
109 |
Correct |
625 ms |
41712 KB |
Output is correct |
110 |
Correct |
154 ms |
45676 KB |
Output is correct |
111 |
Correct |
211 ms |
44376 KB |
Output is correct |
112 |
Correct |
169 ms |
44272 KB |
Output is correct |
113 |
Correct |
172 ms |
43608 KB |
Output is correct |
114 |
Correct |
980 ms |
42052 KB |
Output is correct |
115 |
Correct |
928 ms |
41328 KB |
Output is correct |
116 |
Correct |
195 ms |
43632 KB |
Output is correct |
117 |
Correct |
191 ms |
42656 KB |
Output is correct |
118 |
Correct |
190 ms |
42352 KB |
Output is correct |
119 |
Correct |
189 ms |
42096 KB |
Output is correct |
120 |
Correct |
176 ms |
42388 KB |
Output is correct |
121 |
Correct |
172 ms |
41968 KB |
Output is correct |
122 |
Correct |
172 ms |
41712 KB |
Output is correct |
123 |
Correct |
235 ms |
43076 KB |
Output is correct |
124 |
Correct |
236 ms |
42608 KB |
Output is correct |
125 |
Correct |
246 ms |
42480 KB |
Output is correct |
126 |
Correct |
223 ms |
42480 KB |
Output is correct |
127 |
Correct |
231 ms |
42012 KB |
Output is correct |
128 |
Correct |
225 ms |
41896 KB |
Output is correct |