#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
#define fi first
#define se second
#define all(v) begin(v), end(v)
#define sz(v) (int)(v.size())
struct interval {
int l, r, x;
bool operator < (const interval& o) const {
return l < o.l;
}
};
int main(int argc, char const *argv[])
{
#ifdef LOCAL
freopen("in2", "r", stdin);
#endif
int n; scanf("%d", &n);
vi C(n);
for(int i = 0; i < n; ++i) {
scanf("%d", &C[i]);
}
vi CC = C; sort(all(CC)); CC.erase(unique(all(CC)), CC.end());
for(int i = 0; i < n; ++i) {
C[i] = int(lower_bound(all(CC), C[i]) - CC.begin());
}
vi A(n-1), B(n-1);
vector<vi> g(n);
for(int i = 0; i < n-1; ++i) {
scanf("%d %d", &A[i], &B[i]);
A[i]--, B[i]--;
g[A[i]].emplace_back(B[i]);
g[B[i]].emplace_back(A[i]);
}
vi in(n), sub(n), who(n), par(n);
int tym = 0;
function<void(int,int)> dfs0 = [&](int u, int dad) {
par[u] = dad;
sub[u] = 1;
for(int v : g[u]) if(v != dad) {
dfs0(v, u);
sub[u] += sub[v];
}
};
dfs0(0, -1);
vi head(n);
function<void(int,int,int)> hld_build = [&](int u, int dad, int Head) {
who[in[u] = tym++] = u;
head[u] = Head;
ii big(0, -1);
for(int v : g[u]) if(v != dad) {
big = max(big, ii(sub[v], v));
}
if(big.fi) hld_build(big.se, u, Head);
for(int v : g[u]) if(v != dad and v != big.se) {
hld_build(v, u, v);
}
};
hld_build(0, -1, 0);
set<interval> st;
auto set_range = [&](int L, int R, int x) {
auto lit = --st.upper_bound({L,0,0});
auto rit = st.upper_bound({R,0,0});
int lb = lit -> l, rb = prev(rit) -> r;
int lx = lit -> x, rx = prev(rit) -> x;
vector<interval> ret;
for(auto it = lit; it != rit; ++it) {
ret.push_back({max(L, it -> l), min(R, it -> r), it -> x});
}
st.erase(lit, rit);
if(lb < L) st.insert(rit, {lb, L-1, lx});
st.insert(rit, {L, R, x});
if(rb > R) st.insert(rit, {R+1, rb, rx});
return ret;
};
for(int i = 0; i < n; ++i) {
st.insert({in[i], in[i], C[i]});
}
auto rev_append = [](vector<interval>& X, vector<interval> Y) {
reverse(all(Y));
X.insert(X.end(), all(Y));
};
vi cnt(n+5);
for(int j = 0; j < n-1; ++j) {
int u = A[j];
vector<interval> ls;
do {
rev_append(ls, set_range(in[head[u]], in[u], C[B[j]]));
u = par[head[u]];
} while(~u);
reverse(all(ls));
vi c(sz(ls)), v(sz(ls));
for(int i = 0; i < sz(ls); ++i) {
c[i] = ls[i].r - ls[i].l + 1;
v[i] = ls[i].x;
}
ll ans = 0;
for(int it = 0; it < 17; ++it) {
for(int i = 0; i < sz(v); ++i) {
if(~v[i] & 1) {
ans += (ll) c[i] * cnt[v[i] | 1];
}
cnt[v[i]] += c[i];
}
for(int i = 0; i < sz(v); ++i) {
cnt[v[i]] -= c[i];
v[i] >>= 1;
}
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message
construction.cpp: In function 'int main(int, const char**)':
construction.cpp:23:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | int n; scanf("%d", &n);
| ~~~~~^~~~~~~~~~
construction.cpp:26:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | scanf("%d", &C[i]);
| ~~~~~^~~~~~~~~~~~~
construction.cpp:35:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d %d", &A[i], &B[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
292 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
280 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
300 KB |
Output is correct |
39 |
Correct |
2 ms |
332 KB |
Output is correct |
40 |
Correct |
2 ms |
296 KB |
Output is correct |
41 |
Correct |
2 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
2 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
292 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
280 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
300 KB |
Output is correct |
39 |
Correct |
2 ms |
332 KB |
Output is correct |
40 |
Correct |
2 ms |
296 KB |
Output is correct |
41 |
Correct |
2 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
2 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
3 ms |
460 KB |
Output is correct |
46 |
Correct |
14 ms |
972 KB |
Output is correct |
47 |
Correct |
14 ms |
972 KB |
Output is correct |
48 |
Correct |
15 ms |
1000 KB |
Output is correct |
49 |
Correct |
8 ms |
1356 KB |
Output is correct |
50 |
Correct |
7 ms |
1356 KB |
Output is correct |
51 |
Correct |
8 ms |
1476 KB |
Output is correct |
52 |
Correct |
7 ms |
1228 KB |
Output is correct |
53 |
Correct |
9 ms |
1276 KB |
Output is correct |
54 |
Correct |
8 ms |
1200 KB |
Output is correct |
55 |
Correct |
8 ms |
1228 KB |
Output is correct |
56 |
Correct |
7 ms |
1212 KB |
Output is correct |
57 |
Correct |
21 ms |
980 KB |
Output is correct |
58 |
Correct |
23 ms |
972 KB |
Output is correct |
59 |
Correct |
23 ms |
1004 KB |
Output is correct |
60 |
Correct |
23 ms |
996 KB |
Output is correct |
61 |
Correct |
8 ms |
1220 KB |
Output is correct |
62 |
Correct |
8 ms |
1184 KB |
Output is correct |
63 |
Correct |
8 ms |
1100 KB |
Output is correct |
64 |
Correct |
12 ms |
880 KB |
Output is correct |
65 |
Correct |
14 ms |
1100 KB |
Output is correct |
66 |
Correct |
16 ms |
972 KB |
Output is correct |
67 |
Correct |
14 ms |
980 KB |
Output is correct |
68 |
Correct |
7 ms |
1356 KB |
Output is correct |
69 |
Correct |
7 ms |
1184 KB |
Output is correct |
70 |
Correct |
7 ms |
1260 KB |
Output is correct |
71 |
Correct |
7 ms |
1200 KB |
Output is correct |
72 |
Correct |
21 ms |
972 KB |
Output is correct |
73 |
Correct |
21 ms |
972 KB |
Output is correct |
74 |
Correct |
7 ms |
1100 KB |
Output is correct |
75 |
Correct |
8 ms |
1076 KB |
Output is correct |
76 |
Correct |
8 ms |
944 KB |
Output is correct |
77 |
Correct |
8 ms |
1032 KB |
Output is correct |
78 |
Correct |
9 ms |
1076 KB |
Output is correct |
79 |
Correct |
8 ms |
964 KB |
Output is correct |
80 |
Correct |
8 ms |
1100 KB |
Output is correct |
81 |
Correct |
8 ms |
1096 KB |
Output is correct |
82 |
Correct |
9 ms |
972 KB |
Output is correct |
83 |
Correct |
9 ms |
1008 KB |
Output is correct |
84 |
Correct |
8 ms |
972 KB |
Output is correct |
85 |
Correct |
8 ms |
972 KB |
Output is correct |
86 |
Correct |
8 ms |
1008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
2 ms |
332 KB |
Output is correct |
15 |
Correct |
3 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
2 ms |
332 KB |
Output is correct |
18 |
Correct |
2 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
296 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
2 ms |
332 KB |
Output is correct |
31 |
Correct |
2 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
2 ms |
292 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
280 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
300 KB |
Output is correct |
39 |
Correct |
2 ms |
332 KB |
Output is correct |
40 |
Correct |
2 ms |
296 KB |
Output is correct |
41 |
Correct |
2 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
2 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
3 ms |
460 KB |
Output is correct |
46 |
Correct |
14 ms |
972 KB |
Output is correct |
47 |
Correct |
14 ms |
972 KB |
Output is correct |
48 |
Correct |
15 ms |
1000 KB |
Output is correct |
49 |
Correct |
8 ms |
1356 KB |
Output is correct |
50 |
Correct |
7 ms |
1356 KB |
Output is correct |
51 |
Correct |
8 ms |
1476 KB |
Output is correct |
52 |
Correct |
7 ms |
1228 KB |
Output is correct |
53 |
Correct |
9 ms |
1276 KB |
Output is correct |
54 |
Correct |
8 ms |
1200 KB |
Output is correct |
55 |
Correct |
8 ms |
1228 KB |
Output is correct |
56 |
Correct |
7 ms |
1212 KB |
Output is correct |
57 |
Correct |
21 ms |
980 KB |
Output is correct |
58 |
Correct |
23 ms |
972 KB |
Output is correct |
59 |
Correct |
23 ms |
1004 KB |
Output is correct |
60 |
Correct |
23 ms |
996 KB |
Output is correct |
61 |
Correct |
8 ms |
1220 KB |
Output is correct |
62 |
Correct |
8 ms |
1184 KB |
Output is correct |
63 |
Correct |
8 ms |
1100 KB |
Output is correct |
64 |
Correct |
12 ms |
880 KB |
Output is correct |
65 |
Correct |
14 ms |
1100 KB |
Output is correct |
66 |
Correct |
16 ms |
972 KB |
Output is correct |
67 |
Correct |
14 ms |
980 KB |
Output is correct |
68 |
Correct |
7 ms |
1356 KB |
Output is correct |
69 |
Correct |
7 ms |
1184 KB |
Output is correct |
70 |
Correct |
7 ms |
1260 KB |
Output is correct |
71 |
Correct |
7 ms |
1200 KB |
Output is correct |
72 |
Correct |
21 ms |
972 KB |
Output is correct |
73 |
Correct |
21 ms |
972 KB |
Output is correct |
74 |
Correct |
7 ms |
1100 KB |
Output is correct |
75 |
Correct |
8 ms |
1076 KB |
Output is correct |
76 |
Correct |
8 ms |
944 KB |
Output is correct |
77 |
Correct |
8 ms |
1032 KB |
Output is correct |
78 |
Correct |
9 ms |
1076 KB |
Output is correct |
79 |
Correct |
8 ms |
964 KB |
Output is correct |
80 |
Correct |
8 ms |
1100 KB |
Output is correct |
81 |
Correct |
8 ms |
1096 KB |
Output is correct |
82 |
Correct |
9 ms |
972 KB |
Output is correct |
83 |
Correct |
9 ms |
1008 KB |
Output is correct |
84 |
Correct |
8 ms |
972 KB |
Output is correct |
85 |
Correct |
8 ms |
972 KB |
Output is correct |
86 |
Correct |
8 ms |
1008 KB |
Output is correct |
87 |
Correct |
39 ms |
1996 KB |
Output is correct |
88 |
Correct |
144 ms |
5620 KB |
Output is correct |
89 |
Correct |
601 ms |
18464 KB |
Output is correct |
90 |
Correct |
606 ms |
18436 KB |
Output is correct |
91 |
Correct |
561 ms |
18392 KB |
Output is correct |
92 |
Correct |
208 ms |
29252 KB |
Output is correct |
93 |
Correct |
211 ms |
29192 KB |
Output is correct |
94 |
Correct |
203 ms |
29208 KB |
Output is correct |
95 |
Correct |
216 ms |
24640 KB |
Output is correct |
96 |
Correct |
237 ms |
25048 KB |
Output is correct |
97 |
Correct |
217 ms |
24976 KB |
Output is correct |
98 |
Correct |
224 ms |
24968 KB |
Output is correct |
99 |
Correct |
206 ms |
24004 KB |
Output is correct |
100 |
Correct |
912 ms |
17952 KB |
Output is correct |
101 |
Correct |
1018 ms |
18376 KB |
Output is correct |
102 |
Correct |
1055 ms |
18348 KB |
Output is correct |
103 |
Correct |
1040 ms |
18392 KB |
Output is correct |
104 |
Correct |
226 ms |
23896 KB |
Output is correct |
105 |
Correct |
219 ms |
23836 KB |
Output is correct |
106 |
Correct |
215 ms |
23840 KB |
Output is correct |
107 |
Correct |
506 ms |
17624 KB |
Output is correct |
108 |
Correct |
541 ms |
17880 KB |
Output is correct |
109 |
Correct |
552 ms |
18028 KB |
Output is correct |
110 |
Correct |
184 ms |
28612 KB |
Output is correct |
111 |
Correct |
206 ms |
24608 KB |
Output is correct |
112 |
Correct |
213 ms |
24348 KB |
Output is correct |
113 |
Correct |
178 ms |
23208 KB |
Output is correct |
114 |
Correct |
872 ms |
18088 KB |
Output is correct |
115 |
Correct |
945 ms |
17756 KB |
Output is correct |
116 |
Correct |
202 ms |
23216 KB |
Output is correct |
117 |
Correct |
225 ms |
20892 KB |
Output is correct |
118 |
Correct |
229 ms |
19848 KB |
Output is correct |
119 |
Correct |
222 ms |
19352 KB |
Output is correct |
120 |
Correct |
204 ms |
20488 KB |
Output is correct |
121 |
Correct |
203 ms |
19484 KB |
Output is correct |
122 |
Correct |
206 ms |
19012 KB |
Output is correct |
123 |
Correct |
253 ms |
20860 KB |
Output is correct |
124 |
Correct |
245 ms |
19932 KB |
Output is correct |
125 |
Correct |
250 ms |
19464 KB |
Output is correct |
126 |
Correct |
281 ms |
20548 KB |
Output is correct |
127 |
Correct |
227 ms |
19624 KB |
Output is correct |
128 |
Correct |
239 ms |
19240 KB |
Output is correct |