#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, 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) {
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);
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) {
now = r[seg.get(1, 1, N, in[now], ou[now])];
if(now == 0) break;
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 |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2680 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2680 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 |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
6 ms |
2936 KB |
Output is correct |
14 |
Correct |
6 ms |
2812 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
6 ms |
2780 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2812 KB |
Output is correct |
19 |
Correct |
5 ms |
2812 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 |
2808 KB |
Output is correct |
26 |
Correct |
4 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 |
2856 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2780 KB |
Output is correct |
33 |
Correct |
5 ms |
2680 KB |
Output is correct |
34 |
Correct |
5 ms |
2808 KB |
Output is correct |
35 |
Correct |
4 ms |
2780 KB |
Output is correct |
36 |
Correct |
5 ms |
2808 KB |
Output is correct |
37 |
Correct |
5 ms |
2680 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 |
2780 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
6 ms |
2808 KB |
Output is correct |
43 |
Correct |
5 ms |
2808 KB |
Output is correct |
44 |
Correct |
4 ms |
2680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2680 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2680 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 |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
6 ms |
2936 KB |
Output is correct |
14 |
Correct |
6 ms |
2812 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
6 ms |
2780 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2812 KB |
Output is correct |
19 |
Correct |
5 ms |
2812 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 |
2808 KB |
Output is correct |
26 |
Correct |
4 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 |
2856 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2780 KB |
Output is correct |
33 |
Correct |
5 ms |
2680 KB |
Output is correct |
34 |
Correct |
5 ms |
2808 KB |
Output is correct |
35 |
Correct |
4 ms |
2780 KB |
Output is correct |
36 |
Correct |
5 ms |
2808 KB |
Output is correct |
37 |
Correct |
5 ms |
2680 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 |
2780 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
6 ms |
2808 KB |
Output is correct |
43 |
Correct |
5 ms |
2808 KB |
Output is correct |
44 |
Correct |
4 ms |
2680 KB |
Output is correct |
45 |
Correct |
6 ms |
2808 KB |
Output is correct |
46 |
Correct |
15 ms |
3084 KB |
Output is correct |
47 |
Correct |
16 ms |
2936 KB |
Output is correct |
48 |
Correct |
15 ms |
2936 KB |
Output is correct |
49 |
Correct |
9 ms |
3192 KB |
Output is correct |
50 |
Correct |
11 ms |
3192 KB |
Output is correct |
51 |
Correct |
9 ms |
3320 KB |
Output is correct |
52 |
Correct |
10 ms |
3064 KB |
Output is correct |
53 |
Correct |
10 ms |
3196 KB |
Output is correct |
54 |
Correct |
10 ms |
3064 KB |
Output is correct |
55 |
Correct |
10 ms |
3064 KB |
Output is correct |
56 |
Correct |
10 ms |
3064 KB |
Output is correct |
57 |
Correct |
21 ms |
2936 KB |
Output is correct |
58 |
Correct |
22 ms |
3064 KB |
Output is correct |
59 |
Correct |
22 ms |
3068 KB |
Output is correct |
60 |
Correct |
22 ms |
3064 KB |
Output is correct |
61 |
Correct |
11 ms |
3064 KB |
Output is correct |
62 |
Correct |
11 ms |
3188 KB |
Output is correct |
63 |
Correct |
11 ms |
3064 KB |
Output is correct |
64 |
Correct |
14 ms |
2936 KB |
Output is correct |
65 |
Correct |
14 ms |
2940 KB |
Output is correct |
66 |
Correct |
15 ms |
2936 KB |
Output is correct |
67 |
Correct |
14 ms |
2936 KB |
Output is correct |
68 |
Correct |
8 ms |
3192 KB |
Output is correct |
69 |
Correct |
10 ms |
3064 KB |
Output is correct |
70 |
Correct |
9 ms |
3064 KB |
Output is correct |
71 |
Correct |
10 ms |
3064 KB |
Output is correct |
72 |
Correct |
22 ms |
3064 KB |
Output is correct |
73 |
Correct |
20 ms |
2936 KB |
Output is correct |
74 |
Correct |
11 ms |
3064 KB |
Output is correct |
75 |
Correct |
10 ms |
3020 KB |
Output is correct |
76 |
Correct |
11 ms |
3064 KB |
Output is correct |
77 |
Correct |
10 ms |
3036 KB |
Output is correct |
78 |
Correct |
9 ms |
2936 KB |
Output is correct |
79 |
Correct |
9 ms |
2936 KB |
Output is correct |
80 |
Correct |
10 ms |
3064 KB |
Output is correct |
81 |
Correct |
11 ms |
3064 KB |
Output is correct |
82 |
Correct |
11 ms |
3092 KB |
Output is correct |
83 |
Correct |
11 ms |
3064 KB |
Output is correct |
84 |
Correct |
10 ms |
3064 KB |
Output is correct |
85 |
Correct |
10 ms |
3036 KB |
Output is correct |
86 |
Correct |
10 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2680 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2680 KB |
Output is correct |
6 |
Correct |
5 ms |
2808 KB |
Output is correct |
7 |
Correct |
5 ms |
2680 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 |
2808 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
5 ms |
2808 KB |
Output is correct |
13 |
Correct |
6 ms |
2936 KB |
Output is correct |
14 |
Correct |
6 ms |
2812 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
6 ms |
2780 KB |
Output is correct |
17 |
Correct |
6 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2812 KB |
Output is correct |
19 |
Correct |
5 ms |
2812 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 |
2808 KB |
Output is correct |
26 |
Correct |
4 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 |
2856 KB |
Output is correct |
30 |
Correct |
5 ms |
2808 KB |
Output is correct |
31 |
Correct |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2780 KB |
Output is correct |
33 |
Correct |
5 ms |
2680 KB |
Output is correct |
34 |
Correct |
5 ms |
2808 KB |
Output is correct |
35 |
Correct |
4 ms |
2780 KB |
Output is correct |
36 |
Correct |
5 ms |
2808 KB |
Output is correct |
37 |
Correct |
5 ms |
2680 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 |
2780 KB |
Output is correct |
41 |
Correct |
5 ms |
2808 KB |
Output is correct |
42 |
Correct |
6 ms |
2808 KB |
Output is correct |
43 |
Correct |
5 ms |
2808 KB |
Output is correct |
44 |
Correct |
4 ms |
2680 KB |
Output is correct |
45 |
Correct |
6 ms |
2808 KB |
Output is correct |
46 |
Correct |
15 ms |
3084 KB |
Output is correct |
47 |
Correct |
16 ms |
2936 KB |
Output is correct |
48 |
Correct |
15 ms |
2936 KB |
Output is correct |
49 |
Correct |
9 ms |
3192 KB |
Output is correct |
50 |
Correct |
11 ms |
3192 KB |
Output is correct |
51 |
Correct |
9 ms |
3320 KB |
Output is correct |
52 |
Correct |
10 ms |
3064 KB |
Output is correct |
53 |
Correct |
10 ms |
3196 KB |
Output is correct |
54 |
Correct |
10 ms |
3064 KB |
Output is correct |
55 |
Correct |
10 ms |
3064 KB |
Output is correct |
56 |
Correct |
10 ms |
3064 KB |
Output is correct |
57 |
Correct |
21 ms |
2936 KB |
Output is correct |
58 |
Correct |
22 ms |
3064 KB |
Output is correct |
59 |
Correct |
22 ms |
3068 KB |
Output is correct |
60 |
Correct |
22 ms |
3064 KB |
Output is correct |
61 |
Correct |
11 ms |
3064 KB |
Output is correct |
62 |
Correct |
11 ms |
3188 KB |
Output is correct |
63 |
Correct |
11 ms |
3064 KB |
Output is correct |
64 |
Correct |
14 ms |
2936 KB |
Output is correct |
65 |
Correct |
14 ms |
2940 KB |
Output is correct |
66 |
Correct |
15 ms |
2936 KB |
Output is correct |
67 |
Correct |
14 ms |
2936 KB |
Output is correct |
68 |
Correct |
8 ms |
3192 KB |
Output is correct |
69 |
Correct |
10 ms |
3064 KB |
Output is correct |
70 |
Correct |
9 ms |
3064 KB |
Output is correct |
71 |
Correct |
10 ms |
3064 KB |
Output is correct |
72 |
Correct |
22 ms |
3064 KB |
Output is correct |
73 |
Correct |
20 ms |
2936 KB |
Output is correct |
74 |
Correct |
11 ms |
3064 KB |
Output is correct |
75 |
Correct |
10 ms |
3020 KB |
Output is correct |
76 |
Correct |
11 ms |
3064 KB |
Output is correct |
77 |
Correct |
10 ms |
3036 KB |
Output is correct |
78 |
Correct |
9 ms |
2936 KB |
Output is correct |
79 |
Correct |
9 ms |
2936 KB |
Output is correct |
80 |
Correct |
10 ms |
3064 KB |
Output is correct |
81 |
Correct |
11 ms |
3064 KB |
Output is correct |
82 |
Correct |
11 ms |
3092 KB |
Output is correct |
83 |
Correct |
11 ms |
3064 KB |
Output is correct |
84 |
Correct |
10 ms |
3064 KB |
Output is correct |
85 |
Correct |
10 ms |
3036 KB |
Output is correct |
86 |
Correct |
10 ms |
3064 KB |
Output is correct |
87 |
Correct |
37 ms |
3448 KB |
Output is correct |
88 |
Correct |
126 ms |
4968 KB |
Output is correct |
89 |
Correct |
621 ms |
10020 KB |
Output is correct |
90 |
Correct |
548 ms |
10288 KB |
Output is correct |
91 |
Correct |
553 ms |
10224 KB |
Output is correct |
92 |
Correct |
173 ms |
16112 KB |
Output is correct |
93 |
Correct |
202 ms |
16304 KB |
Output is correct |
94 |
Correct |
174 ms |
16272 KB |
Output is correct |
95 |
Correct |
211 ms |
13104 KB |
Output is correct |
96 |
Correct |
236 ms |
13296 KB |
Output is correct |
97 |
Correct |
227 ms |
13040 KB |
Output is correct |
98 |
Correct |
231 ms |
13040 KB |
Output is correct |
99 |
Correct |
252 ms |
12524 KB |
Output is correct |
100 |
Correct |
983 ms |
10684 KB |
Output is correct |
101 |
Correct |
986 ms |
10604 KB |
Output is correct |
102 |
Correct |
990 ms |
10648 KB |
Output is correct |
103 |
Correct |
994 ms |
10636 KB |
Output is correct |
104 |
Correct |
256 ms |
12480 KB |
Output is correct |
105 |
Correct |
256 ms |
12400 KB |
Output is correct |
106 |
Correct |
249 ms |
12400 KB |
Output is correct |
107 |
Correct |
469 ms |
9324 KB |
Output is correct |
108 |
Correct |
516 ms |
9296 KB |
Output is correct |
109 |
Correct |
547 ms |
9584 KB |
Output is correct |
110 |
Correct |
153 ms |
15344 KB |
Output is correct |
111 |
Correct |
216 ms |
13052 KB |
Output is correct |
112 |
Correct |
195 ms |
12272 KB |
Output is correct |
113 |
Correct |
193 ms |
11632 KB |
Output is correct |
114 |
Correct |
949 ms |
10648 KB |
Output is correct |
115 |
Correct |
938 ms |
9852 KB |
Output is correct |
116 |
Correct |
237 ms |
11692 KB |
Output is correct |
117 |
Correct |
222 ms |
11120 KB |
Output is correct |
118 |
Correct |
215 ms |
10820 KB |
Output is correct |
119 |
Correct |
217 ms |
10692 KB |
Output is correct |
120 |
Correct |
212 ms |
10476 KB |
Output is correct |
121 |
Correct |
196 ms |
10076 KB |
Output is correct |
122 |
Correct |
190 ms |
9864 KB |
Output is correct |
123 |
Correct |
257 ms |
11256 KB |
Output is correct |
124 |
Correct |
254 ms |
10920 KB |
Output is correct |
125 |
Correct |
252 ms |
10552 KB |
Output is correct |
126 |
Correct |
238 ms |
10480 KB |
Output is correct |
127 |
Correct |
230 ms |
10208 KB |
Output is correct |
128 |
Correct |
240 ms |
10072 KB |
Output is correct |