//khodaya khodet komak kon
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
typedef pair <pii, pii> ppp;
typedef pair <ll, ll> pll;
# define A first
# define B second
# define endl '\n'
# define sep ' '
# define all(x) x.begin(), x.end()
# define kill(x) return cout << x << endl, 0
# define SZ(x) int(x.size())
# define lc id << 1
# define rc id << 1 | 1
# define fast_io ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
ll power(ll a, ll b, ll md) {return (!b ? 1 : (b & 1 ? a * power(a * a % md, b / 2, md) % md : power(a * a % md, b / 2, md) % md));}
const int xn = 1e5 + 10;
const int xm = - 20 + 10;
const int sq = 320;
const int inf = 1e9 + 10;
const ll INF = 1e18 + 10;
const ld eps = 1e-15;
const int mod = 998244353;
const int base = 257;
int n, c[xn], par[xn], hvy[xn], sz[xn], arshad[xn];
int stm[xn], TM, ptr, b[xn], fen[xn], seg[xn << 2];
vector <int> adj[xn], cprs;
vector <pii> vec;
map <int, int> mp;
void mofen(int pos, int val){
for (pos += 5; pos < xn; pos += pos & - pos)
fen[pos] += val;
}
int gefen(int pos){
int res = 0;
for (pos += 5; 0 < pos; pos -= pos & - pos)
res += fen[pos];
return res;
}
void preDFS(int v){
sz[v] = 1;
for (int u : adj[v]){
preDFS(u);
sz[v] += sz[u];
if (sz[hvy[v]] < sz[u])
hvy[v] = u;
}
}
void DFS(int v){
stm[v] = TM ++;
arshad[v] = v;
if (v == hvy[par[v]])
arshad[v] = arshad[par[v]];
if (hvy[v])
DFS(hvy[v]);
for (int u : adj[v])
if (u != hvy[v])
DFS(u);
}
void modify(int id, int l, int r, int pos, int val){
if (r - l == 1){
seg[id] = val;
return;
}
int mid = l + r >> 1;
if (pos < mid)
modify(lc, l, mid, pos, val);
else
modify(rc, mid, r, pos, val);
seg[id] = seg[lc];
if (seg[rc] - seg[lc])
seg[id] = - 1;
}
void build(int id, int l, int r){
seg[id] = - 1;
if (r - l == 1)
return;
int mid = l + r >> 1;
build(lc, l, mid), build(rc, mid, r);
}
void solve(int id, int l, int r, int ql, int qr, int val){
if (qr <= l || r <= ql || qr <= ql)
return;
if (ql <= l && r <= qr && seg[id] + 1){
vec.push_back({seg[id], r - l});
seg[id] = val;
return;
}
int mid = l + r >> 1;
if (seg[id] + 1)
seg[lc] = seg[rc] = seg[id];
solve(rc, mid, r, ql, qr, val);
solve(lc, l, mid, ql, qr, val);
seg[id] = seg[lc];
if (seg[rc] - seg[id])
seg[id] = - 1;
if (ql <= l && r <= qr)
seg[id] = val;
}
int main(){
fast_io;
cin >> n;
for (int i = 1; i <= n; ++ i)
cin >> c[i], cprs.push_back(c[i]);
sort(all(cprs));
for (int i = 0; i < SZ(cprs); ++ i)
if (!i || cprs[i] != cprs[i - 1])
mp[cprs[i]] = ptr ++;
for (int i = 1; i <= n; ++ i)
c[i] = mp[c[i]];
for (int i = 1; i < n; ++ i){
int v, u;
cin >> v >> u;
par[u] = v;
adj[v].push_back(u);
b[i] = u;
}
preDFS(1);
DFS(1);
build(1, 0, TM);
for (int i = 1; i <= n; ++ i)
modify(1, 0, TM, stm[i], c[i]);
for (int i = 1; i < n; ++ i){
int v = b[i], col = c[v];
vec.clear();
v = par[v];
while (v){
solve(1, 0, TM, stm[arshad[v]], stm[v] + 1, col);
v = par[arshad[v]];
}
ll ans = 0;
for (pii x : vec){
ans += 1ll * x.B * gefen(x.A - 1);
mofen(x.A, x.B);
}
cout << ans << endl;
for (pii x : vec)
mofen(x.A, - x.B);
}
return 0;
}
Compilation message
construction.cpp: In function 'void modify(int, int, int, int, int)':
construction.cpp:77:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
77 | int mid = l + r >> 1;
| ~~^~~
construction.cpp: In function 'void build(int, int, int)':
construction.cpp:90:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
90 | int mid = l + r >> 1;
| ~~^~~
construction.cpp: In function 'void solve(int, int, int, int, int, int)':
construction.cpp:101:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
101 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2672 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2672 KB |
Output is correct |
4 |
Correct |
3 ms |
2668 KB |
Output is correct |
5 |
Correct |
3 ms |
2764 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
2 ms |
2764 KB |
Output is correct |
8 |
Correct |
2 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2804 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
3 ms |
2808 KB |
Output is correct |
13 |
Correct |
2 ms |
2808 KB |
Output is correct |
14 |
Correct |
2 ms |
2672 KB |
Output is correct |
15 |
Correct |
4 ms |
2764 KB |
Output is correct |
16 |
Correct |
3 ms |
2764 KB |
Output is correct |
17 |
Correct |
2 ms |
2676 KB |
Output is correct |
18 |
Correct |
3 ms |
2764 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2812 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
2672 KB |
Output is correct |
23 |
Correct |
2 ms |
2764 KB |
Output is correct |
24 |
Correct |
2 ms |
2764 KB |
Output is correct |
25 |
Correct |
2 ms |
2764 KB |
Output is correct |
26 |
Correct |
3 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2764 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
2 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
31 |
Correct |
2 ms |
2672 KB |
Output is correct |
32 |
Correct |
2 ms |
2764 KB |
Output is correct |
33 |
Correct |
2 ms |
2764 KB |
Output is correct |
34 |
Correct |
2 ms |
2764 KB |
Output is correct |
35 |
Correct |
2 ms |
2764 KB |
Output is correct |
36 |
Correct |
2 ms |
2764 KB |
Output is correct |
37 |
Correct |
2 ms |
2676 KB |
Output is correct |
38 |
Correct |
2 ms |
2764 KB |
Output is correct |
39 |
Correct |
2 ms |
2764 KB |
Output is correct |
40 |
Correct |
2 ms |
2764 KB |
Output is correct |
41 |
Correct |
2 ms |
2764 KB |
Output is correct |
42 |
Correct |
2 ms |
2668 KB |
Output is correct |
43 |
Correct |
2 ms |
2764 KB |
Output is correct |
44 |
Correct |
2 ms |
2764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2672 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2672 KB |
Output is correct |
4 |
Correct |
3 ms |
2668 KB |
Output is correct |
5 |
Correct |
3 ms |
2764 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
2 ms |
2764 KB |
Output is correct |
8 |
Correct |
2 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2804 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
3 ms |
2808 KB |
Output is correct |
13 |
Correct |
2 ms |
2808 KB |
Output is correct |
14 |
Correct |
2 ms |
2672 KB |
Output is correct |
15 |
Correct |
4 ms |
2764 KB |
Output is correct |
16 |
Correct |
3 ms |
2764 KB |
Output is correct |
17 |
Correct |
2 ms |
2676 KB |
Output is correct |
18 |
Correct |
3 ms |
2764 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2812 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
2672 KB |
Output is correct |
23 |
Correct |
2 ms |
2764 KB |
Output is correct |
24 |
Correct |
2 ms |
2764 KB |
Output is correct |
25 |
Correct |
2 ms |
2764 KB |
Output is correct |
26 |
Correct |
3 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2764 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
2 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
31 |
Correct |
2 ms |
2672 KB |
Output is correct |
32 |
Correct |
2 ms |
2764 KB |
Output is correct |
33 |
Correct |
2 ms |
2764 KB |
Output is correct |
34 |
Correct |
2 ms |
2764 KB |
Output is correct |
35 |
Correct |
2 ms |
2764 KB |
Output is correct |
36 |
Correct |
2 ms |
2764 KB |
Output is correct |
37 |
Correct |
2 ms |
2676 KB |
Output is correct |
38 |
Correct |
2 ms |
2764 KB |
Output is correct |
39 |
Correct |
2 ms |
2764 KB |
Output is correct |
40 |
Correct |
2 ms |
2764 KB |
Output is correct |
41 |
Correct |
2 ms |
2764 KB |
Output is correct |
42 |
Correct |
2 ms |
2668 KB |
Output is correct |
43 |
Correct |
2 ms |
2764 KB |
Output is correct |
44 |
Correct |
2 ms |
2764 KB |
Output is correct |
45 |
Correct |
4 ms |
2808 KB |
Output is correct |
46 |
Correct |
9 ms |
3276 KB |
Output is correct |
47 |
Correct |
9 ms |
3192 KB |
Output is correct |
48 |
Correct |
7 ms |
3148 KB |
Output is correct |
49 |
Correct |
6 ms |
3532 KB |
Output is correct |
50 |
Correct |
6 ms |
3532 KB |
Output is correct |
51 |
Correct |
6 ms |
3532 KB |
Output is correct |
52 |
Correct |
6 ms |
3276 KB |
Output is correct |
53 |
Correct |
6 ms |
3324 KB |
Output is correct |
54 |
Correct |
6 ms |
3316 KB |
Output is correct |
55 |
Correct |
6 ms |
3348 KB |
Output is correct |
56 |
Correct |
6 ms |
3320 KB |
Output is correct |
57 |
Correct |
12 ms |
3200 KB |
Output is correct |
58 |
Correct |
9 ms |
3224 KB |
Output is correct |
59 |
Correct |
10 ms |
3148 KB |
Output is correct |
60 |
Correct |
12 ms |
3144 KB |
Output is correct |
61 |
Correct |
9 ms |
3308 KB |
Output is correct |
62 |
Correct |
7 ms |
3324 KB |
Output is correct |
63 |
Correct |
7 ms |
3268 KB |
Output is correct |
64 |
Correct |
8 ms |
3020 KB |
Output is correct |
65 |
Correct |
10 ms |
2880 KB |
Output is correct |
66 |
Correct |
7 ms |
3020 KB |
Output is correct |
67 |
Correct |
7 ms |
3020 KB |
Output is correct |
68 |
Correct |
5 ms |
3196 KB |
Output is correct |
69 |
Correct |
6 ms |
3276 KB |
Output is correct |
70 |
Correct |
5 ms |
3148 KB |
Output is correct |
71 |
Correct |
5 ms |
3092 KB |
Output is correct |
72 |
Correct |
9 ms |
3212 KB |
Output is correct |
73 |
Correct |
8 ms |
2960 KB |
Output is correct |
74 |
Correct |
6 ms |
3060 KB |
Output is correct |
75 |
Correct |
9 ms |
3272 KB |
Output is correct |
76 |
Correct |
7 ms |
3148 KB |
Output is correct |
77 |
Correct |
9 ms |
3172 KB |
Output is correct |
78 |
Correct |
6 ms |
3020 KB |
Output is correct |
79 |
Correct |
6 ms |
3020 KB |
Output is correct |
80 |
Correct |
7 ms |
3020 KB |
Output is correct |
81 |
Correct |
8 ms |
3140 KB |
Output is correct |
82 |
Correct |
8 ms |
3192 KB |
Output is correct |
83 |
Correct |
8 ms |
3148 KB |
Output is correct |
84 |
Correct |
7 ms |
3020 KB |
Output is correct |
85 |
Correct |
8 ms |
3020 KB |
Output is correct |
86 |
Correct |
7 ms |
3020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2672 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2672 KB |
Output is correct |
4 |
Correct |
3 ms |
2668 KB |
Output is correct |
5 |
Correct |
3 ms |
2764 KB |
Output is correct |
6 |
Correct |
4 ms |
2680 KB |
Output is correct |
7 |
Correct |
2 ms |
2764 KB |
Output is correct |
8 |
Correct |
2 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2804 KB |
Output is correct |
11 |
Correct |
2 ms |
2764 KB |
Output is correct |
12 |
Correct |
3 ms |
2808 KB |
Output is correct |
13 |
Correct |
2 ms |
2808 KB |
Output is correct |
14 |
Correct |
2 ms |
2672 KB |
Output is correct |
15 |
Correct |
4 ms |
2764 KB |
Output is correct |
16 |
Correct |
3 ms |
2764 KB |
Output is correct |
17 |
Correct |
2 ms |
2676 KB |
Output is correct |
18 |
Correct |
3 ms |
2764 KB |
Output is correct |
19 |
Correct |
2 ms |
2764 KB |
Output is correct |
20 |
Correct |
2 ms |
2812 KB |
Output is correct |
21 |
Correct |
2 ms |
2764 KB |
Output is correct |
22 |
Correct |
2 ms |
2672 KB |
Output is correct |
23 |
Correct |
2 ms |
2764 KB |
Output is correct |
24 |
Correct |
2 ms |
2764 KB |
Output is correct |
25 |
Correct |
2 ms |
2764 KB |
Output is correct |
26 |
Correct |
3 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2764 KB |
Output is correct |
28 |
Correct |
2 ms |
2764 KB |
Output is correct |
29 |
Correct |
2 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2764 KB |
Output is correct |
31 |
Correct |
2 ms |
2672 KB |
Output is correct |
32 |
Correct |
2 ms |
2764 KB |
Output is correct |
33 |
Correct |
2 ms |
2764 KB |
Output is correct |
34 |
Correct |
2 ms |
2764 KB |
Output is correct |
35 |
Correct |
2 ms |
2764 KB |
Output is correct |
36 |
Correct |
2 ms |
2764 KB |
Output is correct |
37 |
Correct |
2 ms |
2676 KB |
Output is correct |
38 |
Correct |
2 ms |
2764 KB |
Output is correct |
39 |
Correct |
2 ms |
2764 KB |
Output is correct |
40 |
Correct |
2 ms |
2764 KB |
Output is correct |
41 |
Correct |
2 ms |
2764 KB |
Output is correct |
42 |
Correct |
2 ms |
2668 KB |
Output is correct |
43 |
Correct |
2 ms |
2764 KB |
Output is correct |
44 |
Correct |
2 ms |
2764 KB |
Output is correct |
45 |
Correct |
4 ms |
2808 KB |
Output is correct |
46 |
Correct |
9 ms |
3276 KB |
Output is correct |
47 |
Correct |
9 ms |
3192 KB |
Output is correct |
48 |
Correct |
7 ms |
3148 KB |
Output is correct |
49 |
Correct |
6 ms |
3532 KB |
Output is correct |
50 |
Correct |
6 ms |
3532 KB |
Output is correct |
51 |
Correct |
6 ms |
3532 KB |
Output is correct |
52 |
Correct |
6 ms |
3276 KB |
Output is correct |
53 |
Correct |
6 ms |
3324 KB |
Output is correct |
54 |
Correct |
6 ms |
3316 KB |
Output is correct |
55 |
Correct |
6 ms |
3348 KB |
Output is correct |
56 |
Correct |
6 ms |
3320 KB |
Output is correct |
57 |
Correct |
12 ms |
3200 KB |
Output is correct |
58 |
Correct |
9 ms |
3224 KB |
Output is correct |
59 |
Correct |
10 ms |
3148 KB |
Output is correct |
60 |
Correct |
12 ms |
3144 KB |
Output is correct |
61 |
Correct |
9 ms |
3308 KB |
Output is correct |
62 |
Correct |
7 ms |
3324 KB |
Output is correct |
63 |
Correct |
7 ms |
3268 KB |
Output is correct |
64 |
Correct |
8 ms |
3020 KB |
Output is correct |
65 |
Correct |
10 ms |
2880 KB |
Output is correct |
66 |
Correct |
7 ms |
3020 KB |
Output is correct |
67 |
Correct |
7 ms |
3020 KB |
Output is correct |
68 |
Correct |
5 ms |
3196 KB |
Output is correct |
69 |
Correct |
6 ms |
3276 KB |
Output is correct |
70 |
Correct |
5 ms |
3148 KB |
Output is correct |
71 |
Correct |
5 ms |
3092 KB |
Output is correct |
72 |
Correct |
9 ms |
3212 KB |
Output is correct |
73 |
Correct |
8 ms |
2960 KB |
Output is correct |
74 |
Correct |
6 ms |
3060 KB |
Output is correct |
75 |
Correct |
9 ms |
3272 KB |
Output is correct |
76 |
Correct |
7 ms |
3148 KB |
Output is correct |
77 |
Correct |
9 ms |
3172 KB |
Output is correct |
78 |
Correct |
6 ms |
3020 KB |
Output is correct |
79 |
Correct |
6 ms |
3020 KB |
Output is correct |
80 |
Correct |
7 ms |
3020 KB |
Output is correct |
81 |
Correct |
8 ms |
3140 KB |
Output is correct |
82 |
Correct |
8 ms |
3192 KB |
Output is correct |
83 |
Correct |
8 ms |
3148 KB |
Output is correct |
84 |
Correct |
7 ms |
3020 KB |
Output is correct |
85 |
Correct |
8 ms |
3020 KB |
Output is correct |
86 |
Correct |
7 ms |
3020 KB |
Output is correct |
87 |
Correct |
19 ms |
4036 KB |
Output is correct |
88 |
Correct |
68 ms |
6580 KB |
Output is correct |
89 |
Correct |
250 ms |
15848 KB |
Output is correct |
90 |
Correct |
257 ms |
15892 KB |
Output is correct |
91 |
Correct |
267 ms |
15832 KB |
Output is correct |
92 |
Correct |
178 ms |
23656 KB |
Output is correct |
93 |
Correct |
167 ms |
23580 KB |
Output is correct |
94 |
Correct |
167 ms |
23528 KB |
Output is correct |
95 |
Correct |
175 ms |
19208 KB |
Output is correct |
96 |
Correct |
157 ms |
19560 KB |
Output is correct |
97 |
Correct |
170 ms |
19572 KB |
Output is correct |
98 |
Correct |
159 ms |
19652 KB |
Output is correct |
99 |
Correct |
159 ms |
18972 KB |
Output is correct |
100 |
Correct |
372 ms |
16104 KB |
Output is correct |
101 |
Correct |
334 ms |
16280 KB |
Output is correct |
102 |
Correct |
343 ms |
16368 KB |
Output is correct |
103 |
Correct |
341 ms |
16352 KB |
Output is correct |
104 |
Correct |
247 ms |
19068 KB |
Output is correct |
105 |
Correct |
191 ms |
18984 KB |
Output is correct |
106 |
Correct |
202 ms |
19012 KB |
Output is correct |
107 |
Correct |
200 ms |
9980 KB |
Output is correct |
108 |
Correct |
224 ms |
10068 KB |
Output is correct |
109 |
Correct |
242 ms |
11928 KB |
Output is correct |
110 |
Correct |
128 ms |
17936 KB |
Output is correct |
111 |
Correct |
159 ms |
19172 KB |
Output is correct |
112 |
Correct |
126 ms |
13944 KB |
Output is correct |
113 |
Correct |
124 ms |
13316 KB |
Output is correct |
114 |
Correct |
340 ms |
16004 KB |
Output is correct |
115 |
Correct |
305 ms |
10620 KB |
Output is correct |
116 |
Correct |
167 ms |
13548 KB |
Output is correct |
117 |
Correct |
190 ms |
16884 KB |
Output is correct |
118 |
Correct |
191 ms |
16324 KB |
Output is correct |
119 |
Correct |
227 ms |
16156 KB |
Output is correct |
120 |
Correct |
159 ms |
11624 KB |
Output is correct |
121 |
Correct |
173 ms |
11204 KB |
Output is correct |
122 |
Correct |
181 ms |
10992 KB |
Output is correct |
123 |
Correct |
234 ms |
16972 KB |
Output is correct |
124 |
Correct |
234 ms |
16504 KB |
Output is correct |
125 |
Correct |
241 ms |
16104 KB |
Output is correct |
126 |
Correct |
206 ms |
11736 KB |
Output is correct |
127 |
Correct |
213 ms |
11408 KB |
Output is correct |
128 |
Correct |
224 ms |
10984 KB |
Output is correct |