#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
struct maxseg {
vector<int> T;
void upd(int idx, int s, int e, int p, int v) {
if(p < s || e < p) return;
if(s == e) {
T[idx] = v;
return;
}
int m = s+e >> 1;
upd(idx*2, s, m, p, v);
upd(idx*2+1, m+1, e, p, v);
T[idx] = max(T[idx*2], T[idx*2+1]);
}
int get(int idx, int s, int e, int l, int r) {
if(r < s || e < l) return 0;
if(l <= s && e <= r) return T[idx];
int m = s+e >> 1;
return max(get(idx*2, s, m, l, r), get(idx*2+1, m+1, e, l, r));
}
maxseg(int N) {
T.resize(4*N);
}
};
const int _N = 100009;
int A[_N], B[_N], C[_N], in[_N], ou[_N], t, p[22][_N], l, d[_N], r[_N];
vector<int> adj[_N], CS;
long long F[_N];
void upd(int x, int y) {
for(int i=x; i<=(int)CS.size(); i+=(i&-i)) F[i] += y;
}
long long get(int x) {
long long ret = 0;
for(int i=x; i>=1; i-=(i&-i)) ret += F[i];
return ret;
}
void dfs(int x, int p, int d) {
::p[0][x] = p; in[x] = ++t; ::d[x] = d;
for(auto& it: adj[x]) dfs(it, x, d+1);
ou[x] = t;
}
int main() {
int N; scanf("%d",&N);
for(int i=1; i<=N; i++) {
scanf("%d",&C[i]);
CS.push_back(C[i]);
}
sort(CS.begin(), CS.end());
CS.resize(unique(CS.begin(), CS.end()) - CS.begin());
for(int i=1; i<=N; i++) C[i] = lower_bound(CS.begin(), CS.end(), C[i]) - CS.begin() + 1;
for(int i=1; i<N; i++) {
scanf("%d%d",&A[i],&B[i]);
adj[A[i]].push_back(B[i]);
}
dfs(1, 1, 1);
for(int i=1; (1<<i)<=N; i++) {
for(int j=1; j<=N; j++) p[i][j] = p[i-1][p[i-1][j]];
l = i;
}
maxseg seg(N);
B[0] = 1; r[0] = 0;
for(int i=1; i<N; i++) {
int now = A[i], ni = seg.get(1, 1, N, in[now], ou[now]);
vector<pii> S = {{ni, now}};
while(1) {
for(int i=l; i>=0; i--) if(ni == seg.get(1, 1, N, in[p[i][now]], ou[p[i][now]])) now = p[i][now];
if(now == 1) break;
now = p[0][now];
ni = seg.get(1, 1, N, in[now], ou[now]);
S.push_back({ni, now});
}
long long ans = 0;
for(auto& it: S) {
ans += 1LL * (d[it.second] - d[r[it.first]]) * get(C[B[it.first]] - 1);
upd(C[B[it.first]], d[it.second] - d[r[it.first]]);
}
for(auto& it: S) {
upd(C[B[it.first]], d[r[it.first]] - d[it.second]);
r[it.first] = it.second;
}
r[i] = 0;
seg.upd(1, 1, N, in[B[i]], i);
printf("%lld\n", ans);
}
return 0;
}
Compilation message
construction.cpp: In member function 'void maxseg::upd(int, int, int, int, int)':
construction.cpp:13:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = s+e >> 1;
~^~
construction.cpp: In member function 'int maxseg::get(int, int, int, int, int)':
construction.cpp:21:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = s+e >> 1;
~^~
construction.cpp: In function 'int main()':
construction.cpp:50:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int N; scanf("%d",&N);
~~~~~^~~~~~~~~
construction.cpp:52:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&C[i]);
~~~~~^~~~~~~~~~~~
construction.cpp:60: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]);
~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2684 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
5 ms |
2808 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
5 ms |
2780 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 |
7 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 |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2812 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 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 |
2808 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 |
2808 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 |
5 ms |
2808 KB |
Output is correct |
44 |
Correct |
5 ms |
2812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2684 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
5 ms |
2808 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
5 ms |
2780 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 |
7 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 |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2812 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 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 |
2808 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 |
2808 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 |
5 ms |
2808 KB |
Output is correct |
44 |
Correct |
5 ms |
2812 KB |
Output is correct |
45 |
Correct |
7 ms |
2936 KB |
Output is correct |
46 |
Correct |
20 ms |
3320 KB |
Output is correct |
47 |
Correct |
21 ms |
3320 KB |
Output is correct |
48 |
Correct |
20 ms |
3324 KB |
Output is correct |
49 |
Correct |
11 ms |
3576 KB |
Output is correct |
50 |
Correct |
10 ms |
3576 KB |
Output is correct |
51 |
Correct |
11 ms |
3576 KB |
Output is correct |
52 |
Correct |
15 ms |
3448 KB |
Output is correct |
53 |
Correct |
14 ms |
3412 KB |
Output is correct |
54 |
Correct |
13 ms |
3448 KB |
Output is correct |
55 |
Correct |
13 ms |
3448 KB |
Output is correct |
56 |
Correct |
15 ms |
3448 KB |
Output is correct |
57 |
Correct |
34 ms |
3368 KB |
Output is correct |
58 |
Correct |
35 ms |
3448 KB |
Output is correct |
59 |
Correct |
34 ms |
3320 KB |
Output is correct |
60 |
Correct |
34 ms |
3320 KB |
Output is correct |
61 |
Correct |
15 ms |
3576 KB |
Output is correct |
62 |
Correct |
15 ms |
3448 KB |
Output is correct |
63 |
Correct |
15 ms |
3448 KB |
Output is correct |
64 |
Correct |
19 ms |
3320 KB |
Output is correct |
65 |
Correct |
19 ms |
3192 KB |
Output is correct |
66 |
Correct |
20 ms |
3320 KB |
Output is correct |
67 |
Correct |
19 ms |
3316 KB |
Output is correct |
68 |
Correct |
9 ms |
3544 KB |
Output is correct |
69 |
Correct |
13 ms |
3448 KB |
Output is correct |
70 |
Correct |
13 ms |
3448 KB |
Output is correct |
71 |
Correct |
14 ms |
3384 KB |
Output is correct |
72 |
Correct |
34 ms |
3320 KB |
Output is correct |
73 |
Correct |
33 ms |
3192 KB |
Output is correct |
74 |
Correct |
14 ms |
3320 KB |
Output is correct |
75 |
Correct |
14 ms |
3448 KB |
Output is correct |
76 |
Correct |
13 ms |
3320 KB |
Output is correct |
77 |
Correct |
13 ms |
3384 KB |
Output is correct |
78 |
Correct |
12 ms |
3320 KB |
Output is correct |
79 |
Correct |
14 ms |
3328 KB |
Output is correct |
80 |
Correct |
13 ms |
3312 KB |
Output is correct |
81 |
Correct |
18 ms |
3320 KB |
Output is correct |
82 |
Correct |
16 ms |
3320 KB |
Output is correct |
83 |
Correct |
15 ms |
3320 KB |
Output is correct |
84 |
Correct |
15 ms |
3320 KB |
Output is correct |
85 |
Correct |
15 ms |
3324 KB |
Output is correct |
86 |
Correct |
15 ms |
3312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2684 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
5 ms |
2808 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2808 KB |
Output is correct |
8 |
Correct |
5 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
5 ms |
2780 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 |
7 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 |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2808 KB |
Output is correct |
25 |
Correct |
5 ms |
2812 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 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 |
2808 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 |
2808 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 |
5 ms |
2808 KB |
Output is correct |
44 |
Correct |
5 ms |
2812 KB |
Output is correct |
45 |
Correct |
7 ms |
2936 KB |
Output is correct |
46 |
Correct |
20 ms |
3320 KB |
Output is correct |
47 |
Correct |
21 ms |
3320 KB |
Output is correct |
48 |
Correct |
20 ms |
3324 KB |
Output is correct |
49 |
Correct |
11 ms |
3576 KB |
Output is correct |
50 |
Correct |
10 ms |
3576 KB |
Output is correct |
51 |
Correct |
11 ms |
3576 KB |
Output is correct |
52 |
Correct |
15 ms |
3448 KB |
Output is correct |
53 |
Correct |
14 ms |
3412 KB |
Output is correct |
54 |
Correct |
13 ms |
3448 KB |
Output is correct |
55 |
Correct |
13 ms |
3448 KB |
Output is correct |
56 |
Correct |
15 ms |
3448 KB |
Output is correct |
57 |
Correct |
34 ms |
3368 KB |
Output is correct |
58 |
Correct |
35 ms |
3448 KB |
Output is correct |
59 |
Correct |
34 ms |
3320 KB |
Output is correct |
60 |
Correct |
34 ms |
3320 KB |
Output is correct |
61 |
Correct |
15 ms |
3576 KB |
Output is correct |
62 |
Correct |
15 ms |
3448 KB |
Output is correct |
63 |
Correct |
15 ms |
3448 KB |
Output is correct |
64 |
Correct |
19 ms |
3320 KB |
Output is correct |
65 |
Correct |
19 ms |
3192 KB |
Output is correct |
66 |
Correct |
20 ms |
3320 KB |
Output is correct |
67 |
Correct |
19 ms |
3316 KB |
Output is correct |
68 |
Correct |
9 ms |
3544 KB |
Output is correct |
69 |
Correct |
13 ms |
3448 KB |
Output is correct |
70 |
Correct |
13 ms |
3448 KB |
Output is correct |
71 |
Correct |
14 ms |
3384 KB |
Output is correct |
72 |
Correct |
34 ms |
3320 KB |
Output is correct |
73 |
Correct |
33 ms |
3192 KB |
Output is correct |
74 |
Correct |
14 ms |
3320 KB |
Output is correct |
75 |
Correct |
14 ms |
3448 KB |
Output is correct |
76 |
Correct |
13 ms |
3320 KB |
Output is correct |
77 |
Correct |
13 ms |
3384 KB |
Output is correct |
78 |
Correct |
12 ms |
3320 KB |
Output is correct |
79 |
Correct |
14 ms |
3328 KB |
Output is correct |
80 |
Correct |
13 ms |
3312 KB |
Output is correct |
81 |
Correct |
18 ms |
3320 KB |
Output is correct |
82 |
Correct |
16 ms |
3320 KB |
Output is correct |
83 |
Correct |
15 ms |
3320 KB |
Output is correct |
84 |
Correct |
15 ms |
3320 KB |
Output is correct |
85 |
Correct |
15 ms |
3324 KB |
Output is correct |
86 |
Correct |
15 ms |
3312 KB |
Output is correct |
87 |
Correct |
57 ms |
4216 KB |
Output is correct |
88 |
Correct |
217 ms |
7332 KB |
Output is correct |
89 |
Correct |
1336 ms |
17380 KB |
Output is correct |
90 |
Correct |
1304 ms |
17328 KB |
Output is correct |
91 |
Correct |
1401 ms |
17284 KB |
Output is correct |
92 |
Correct |
194 ms |
23404 KB |
Output is correct |
93 |
Correct |
201 ms |
23476 KB |
Output is correct |
94 |
Correct |
196 ms |
23408 KB |
Output is correct |
95 |
Correct |
473 ms |
20332 KB |
Output is correct |
96 |
Correct |
481 ms |
20288 KB |
Output is correct |
97 |
Correct |
484 ms |
20336 KB |
Output is correct |
98 |
Correct |
495 ms |
20168 KB |
Output is correct |
99 |
Correct |
507 ms |
19680 KB |
Output is correct |
100 |
Execution timed out |
2037 ms |
17312 KB |
Time limit exceeded |
101 |
Halted |
0 ms |
0 KB |
- |