#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]);
~~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2812 KB |
Output is correct |
7 |
Correct |
5 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
5 ms |
2680 KB |
Output is correct |
13 |
Correct |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2684 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
5 ms |
2680 KB |
Output is correct |
17 |
Correct |
5 ms |
2680 KB |
Output is correct |
18 |
Correct |
5 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 |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2680 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2680 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 |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2676 KB |
Output is correct |
33 |
Correct |
5 ms |
2680 KB |
Output is correct |
34 |
Correct |
6 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 |
6 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 |
6 ms |
2828 KB |
Output is correct |
41 |
Correct |
6 ms |
2808 KB |
Output is correct |
42 |
Correct |
5 ms |
2680 KB |
Output is correct |
43 |
Correct |
5 ms |
2680 KB |
Output is correct |
44 |
Correct |
6 ms |
2680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2812 KB |
Output is correct |
7 |
Correct |
5 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
5 ms |
2680 KB |
Output is correct |
13 |
Correct |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2684 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
5 ms |
2680 KB |
Output is correct |
17 |
Correct |
5 ms |
2680 KB |
Output is correct |
18 |
Correct |
5 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 |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2680 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2680 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 |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2676 KB |
Output is correct |
33 |
Correct |
5 ms |
2680 KB |
Output is correct |
34 |
Correct |
6 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 |
6 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 |
6 ms |
2828 KB |
Output is correct |
41 |
Correct |
6 ms |
2808 KB |
Output is correct |
42 |
Correct |
5 ms |
2680 KB |
Output is correct |
43 |
Correct |
5 ms |
2680 KB |
Output is correct |
44 |
Correct |
6 ms |
2680 KB |
Output is correct |
45 |
Correct |
6 ms |
2808 KB |
Output is correct |
46 |
Correct |
15 ms |
3196 KB |
Output is correct |
47 |
Correct |
16 ms |
3064 KB |
Output is correct |
48 |
Correct |
15 ms |
3064 KB |
Output is correct |
49 |
Correct |
9 ms |
3340 KB |
Output is correct |
50 |
Correct |
9 ms |
3320 KB |
Output is correct |
51 |
Correct |
9 ms |
3320 KB |
Output is correct |
52 |
Correct |
10 ms |
3192 KB |
Output is correct |
53 |
Correct |
11 ms |
3240 KB |
Output is correct |
54 |
Correct |
10 ms |
3192 KB |
Output is correct |
55 |
Correct |
10 ms |
3192 KB |
Output is correct |
56 |
Correct |
11 ms |
3192 KB |
Output is correct |
57 |
Correct |
21 ms |
3064 KB |
Output is correct |
58 |
Correct |
22 ms |
3064 KB |
Output is correct |
59 |
Correct |
23 ms |
3212 KB |
Output is correct |
60 |
Correct |
24 ms |
3064 KB |
Output is correct |
61 |
Correct |
11 ms |
3220 KB |
Output is correct |
62 |
Correct |
11 ms |
3192 KB |
Output is correct |
63 |
Correct |
11 ms |
3192 KB |
Output is correct |
64 |
Correct |
14 ms |
3064 KB |
Output is correct |
65 |
Correct |
14 ms |
2936 KB |
Output is correct |
66 |
Correct |
15 ms |
3064 KB |
Output is correct |
67 |
Correct |
15 ms |
3064 KB |
Output is correct |
68 |
Correct |
9 ms |
3320 KB |
Output is correct |
69 |
Correct |
11 ms |
3192 KB |
Output is correct |
70 |
Correct |
10 ms |
3192 KB |
Output is correct |
71 |
Correct |
10 ms |
3192 KB |
Output is correct |
72 |
Correct |
22 ms |
3064 KB |
Output is correct |
73 |
Correct |
21 ms |
3064 KB |
Output is correct |
74 |
Correct |
11 ms |
3064 KB |
Output is correct |
75 |
Correct |
11 ms |
3116 KB |
Output is correct |
76 |
Correct |
10 ms |
3064 KB |
Output is correct |
77 |
Correct |
10 ms |
3064 KB |
Output is correct |
78 |
Correct |
10 ms |
3064 KB |
Output is correct |
79 |
Correct |
10 ms |
3064 KB |
Output is correct |
80 |
Correct |
10 ms |
3192 KB |
Output is correct |
81 |
Correct |
11 ms |
3064 KB |
Output is correct |
82 |
Correct |
11 ms |
3064 KB |
Output is correct |
83 |
Correct |
11 ms |
3068 KB |
Output is correct |
84 |
Correct |
10 ms |
3032 KB |
Output is correct |
85 |
Correct |
10 ms |
3064 KB |
Output is correct |
86 |
Correct |
10 ms |
3064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2680 KB |
Output is correct |
2 |
Correct |
4 ms |
2680 KB |
Output is correct |
3 |
Correct |
4 ms |
2808 KB |
Output is correct |
4 |
Correct |
4 ms |
2680 KB |
Output is correct |
5 |
Correct |
5 ms |
2808 KB |
Output is correct |
6 |
Correct |
5 ms |
2812 KB |
Output is correct |
7 |
Correct |
5 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2808 KB |
Output is correct |
9 |
Correct |
4 ms |
2808 KB |
Output is correct |
10 |
Correct |
4 ms |
2808 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
5 ms |
2680 KB |
Output is correct |
13 |
Correct |
5 ms |
2680 KB |
Output is correct |
14 |
Correct |
4 ms |
2684 KB |
Output is correct |
15 |
Correct |
5 ms |
2680 KB |
Output is correct |
16 |
Correct |
5 ms |
2680 KB |
Output is correct |
17 |
Correct |
5 ms |
2680 KB |
Output is correct |
18 |
Correct |
5 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 |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2808 KB |
Output is correct |
24 |
Correct |
5 ms |
2680 KB |
Output is correct |
25 |
Correct |
6 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2680 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 |
5 ms |
2808 KB |
Output is correct |
32 |
Correct |
5 ms |
2676 KB |
Output is correct |
33 |
Correct |
5 ms |
2680 KB |
Output is correct |
34 |
Correct |
6 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 |
6 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 |
6 ms |
2828 KB |
Output is correct |
41 |
Correct |
6 ms |
2808 KB |
Output is correct |
42 |
Correct |
5 ms |
2680 KB |
Output is correct |
43 |
Correct |
5 ms |
2680 KB |
Output is correct |
44 |
Correct |
6 ms |
2680 KB |
Output is correct |
45 |
Correct |
6 ms |
2808 KB |
Output is correct |
46 |
Correct |
15 ms |
3196 KB |
Output is correct |
47 |
Correct |
16 ms |
3064 KB |
Output is correct |
48 |
Correct |
15 ms |
3064 KB |
Output is correct |
49 |
Correct |
9 ms |
3340 KB |
Output is correct |
50 |
Correct |
9 ms |
3320 KB |
Output is correct |
51 |
Correct |
9 ms |
3320 KB |
Output is correct |
52 |
Correct |
10 ms |
3192 KB |
Output is correct |
53 |
Correct |
11 ms |
3240 KB |
Output is correct |
54 |
Correct |
10 ms |
3192 KB |
Output is correct |
55 |
Correct |
10 ms |
3192 KB |
Output is correct |
56 |
Correct |
11 ms |
3192 KB |
Output is correct |
57 |
Correct |
21 ms |
3064 KB |
Output is correct |
58 |
Correct |
22 ms |
3064 KB |
Output is correct |
59 |
Correct |
23 ms |
3212 KB |
Output is correct |
60 |
Correct |
24 ms |
3064 KB |
Output is correct |
61 |
Correct |
11 ms |
3220 KB |
Output is correct |
62 |
Correct |
11 ms |
3192 KB |
Output is correct |
63 |
Correct |
11 ms |
3192 KB |
Output is correct |
64 |
Correct |
14 ms |
3064 KB |
Output is correct |
65 |
Correct |
14 ms |
2936 KB |
Output is correct |
66 |
Correct |
15 ms |
3064 KB |
Output is correct |
67 |
Correct |
15 ms |
3064 KB |
Output is correct |
68 |
Correct |
9 ms |
3320 KB |
Output is correct |
69 |
Correct |
11 ms |
3192 KB |
Output is correct |
70 |
Correct |
10 ms |
3192 KB |
Output is correct |
71 |
Correct |
10 ms |
3192 KB |
Output is correct |
72 |
Correct |
22 ms |
3064 KB |
Output is correct |
73 |
Correct |
21 ms |
3064 KB |
Output is correct |
74 |
Correct |
11 ms |
3064 KB |
Output is correct |
75 |
Correct |
11 ms |
3116 KB |
Output is correct |
76 |
Correct |
10 ms |
3064 KB |
Output is correct |
77 |
Correct |
10 ms |
3064 KB |
Output is correct |
78 |
Correct |
10 ms |
3064 KB |
Output is correct |
79 |
Correct |
10 ms |
3064 KB |
Output is correct |
80 |
Correct |
10 ms |
3192 KB |
Output is correct |
81 |
Correct |
11 ms |
3064 KB |
Output is correct |
82 |
Correct |
11 ms |
3064 KB |
Output is correct |
83 |
Correct |
11 ms |
3068 KB |
Output is correct |
84 |
Correct |
10 ms |
3032 KB |
Output is correct |
85 |
Correct |
10 ms |
3064 KB |
Output is correct |
86 |
Correct |
10 ms |
3064 KB |
Output is correct |
87 |
Correct |
38 ms |
3704 KB |
Output is correct |
88 |
Correct |
124 ms |
5628 KB |
Output is correct |
89 |
Correct |
551 ms |
12248 KB |
Output is correct |
90 |
Correct |
563 ms |
12344 KB |
Output is correct |
91 |
Correct |
567 ms |
12268 KB |
Output is correct |
92 |
Correct |
177 ms |
18332 KB |
Output is correct |
93 |
Correct |
176 ms |
18288 KB |
Output is correct |
94 |
Correct |
180 ms |
18288 KB |
Output is correct |
95 |
Correct |
231 ms |
14852 KB |
Output is correct |
96 |
Correct |
238 ms |
15220 KB |
Output is correct |
97 |
Correct |
248 ms |
15224 KB |
Output is correct |
98 |
Correct |
228 ms |
15192 KB |
Output is correct |
99 |
Correct |
231 ms |
14632 KB |
Output is correct |
100 |
Correct |
965 ms |
12656 KB |
Output is correct |
101 |
Correct |
1018 ms |
12912 KB |
Output is correct |
102 |
Correct |
1002 ms |
12732 KB |
Output is correct |
103 |
Correct |
996 ms |
12692 KB |
Output is correct |
104 |
Correct |
261 ms |
14588 KB |
Output is correct |
105 |
Correct |
257 ms |
14644 KB |
Output is correct |
106 |
Correct |
259 ms |
14564 KB |
Output is correct |
107 |
Correct |
474 ms |
10680 KB |
Output is correct |
108 |
Correct |
510 ms |
10736 KB |
Output is correct |
109 |
Correct |
554 ms |
11392 KB |
Output is correct |
110 |
Correct |
157 ms |
16880 KB |
Output is correct |
111 |
Correct |
217 ms |
14820 KB |
Output is correct |
112 |
Correct |
197 ms |
13780 KB |
Output is correct |
113 |
Correct |
212 ms |
13152 KB |
Output is correct |
114 |
Correct |
960 ms |
12336 KB |
Output is correct |
115 |
Correct |
951 ms |
11536 KB |
Output is correct |
116 |
Correct |
231 ms |
13040 KB |
Output is correct |
117 |
Correct |
220 ms |
13068 KB |
Output is correct |
118 |
Correct |
223 ms |
12692 KB |
Output is correct |
119 |
Correct |
214 ms |
12492 KB |
Output is correct |
120 |
Correct |
197 ms |
11888 KB |
Output is correct |
121 |
Correct |
193 ms |
11568 KB |
Output is correct |
122 |
Correct |
205 ms |
11456 KB |
Output is correct |
123 |
Correct |
255 ms |
13128 KB |
Output is correct |
124 |
Correct |
261 ms |
12656 KB |
Output is correct |
125 |
Correct |
260 ms |
12524 KB |
Output is correct |
126 |
Correct |
233 ms |
12016 KB |
Output is correct |
127 |
Correct |
233 ms |
11632 KB |
Output is correct |
128 |
Correct |
225 ms |
11580 KB |
Output is correct |