//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, m, c[xn], par[xn], hvy[xn], sz[xn], arshad[xn];
int stm[xn], TM, ptr, b[xn], fen[xn];
pii a[xn];
stack <ppi> sk[xn];
vector <int> adj[xn], cprs;
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);
sk[arshad[v]].push({{stm[v], stm[v]}, c[v]});
}
void add(pii x){
if (m && a[m - 1].A == x.A)
a[m - 1].B += x.B;
else
a[m ++] = x;
}
void solve(int id, int x, int col){
vector <pii> vec;
while (SZ(sk[id]) && sk[id].top().A.B <= x){
vec.push_back({sk[id].top().B, sk[id].top().A.B - sk[id].top().A.A + 1});
sk[id].pop();
}
if (SZ(sk[id]) && sk[id].top().A.A <= x){
vec.push_back({sk[id].top().B, x - sk[id].top().A.A + 1});
sk[id].top().A.A = x + 1;
}
if (SZ(sk[id]) && sk[id].top().B == col)
sk[id].top().A.A = stm[id];
else
sk[id].push({{stm[id], x}, col});
reverse(all(vec));
for (pii x : vec)
add(x);
}
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);
for (int i = 1; i < n; ++ i){
int v = b[i], col = c[v];
v = par[v], m = 0;
while (v){
solve(arshad[v], stm[v], col);
v = par[arshad[v]];
}
ll ans = 0;
for (int i = 0; i < m; ++ i){
ans += 1ll * a[i].B * gefen(a[i].A - 1);
mofen(a[i].A, a[i].B);
}
cout << ans << endl;
for (int i = 0; i < m; ++ i)
mofen(a[i].A, - a[i].B);
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
68396 KB |
Output is correct |
2 |
Correct |
39 ms |
68468 KB |
Output is correct |
3 |
Correct |
41 ms |
68548 KB |
Output is correct |
4 |
Correct |
38 ms |
68464 KB |
Output is correct |
5 |
Correct |
39 ms |
68488 KB |
Output is correct |
6 |
Correct |
39 ms |
68424 KB |
Output is correct |
7 |
Correct |
38 ms |
68516 KB |
Output is correct |
8 |
Correct |
38 ms |
68580 KB |
Output is correct |
9 |
Correct |
40 ms |
68564 KB |
Output is correct |
10 |
Correct |
39 ms |
68544 KB |
Output is correct |
11 |
Correct |
38 ms |
68556 KB |
Output is correct |
12 |
Correct |
39 ms |
68556 KB |
Output is correct |
13 |
Correct |
41 ms |
68456 KB |
Output is correct |
14 |
Correct |
38 ms |
68464 KB |
Output is correct |
15 |
Correct |
39 ms |
68436 KB |
Output is correct |
16 |
Correct |
40 ms |
68508 KB |
Output is correct |
17 |
Correct |
39 ms |
68512 KB |
Output is correct |
18 |
Correct |
40 ms |
68424 KB |
Output is correct |
19 |
Correct |
38 ms |
68560 KB |
Output is correct |
20 |
Correct |
39 ms |
68428 KB |
Output is correct |
21 |
Correct |
38 ms |
68544 KB |
Output is correct |
22 |
Correct |
37 ms |
68456 KB |
Output is correct |
23 |
Correct |
38 ms |
68428 KB |
Output is correct |
24 |
Correct |
39 ms |
68496 KB |
Output is correct |
25 |
Correct |
44 ms |
68572 KB |
Output is correct |
26 |
Correct |
38 ms |
68556 KB |
Output is correct |
27 |
Correct |
39 ms |
68520 KB |
Output is correct |
28 |
Correct |
38 ms |
68528 KB |
Output is correct |
29 |
Correct |
38 ms |
68412 KB |
Output is correct |
30 |
Correct |
40 ms |
68432 KB |
Output is correct |
31 |
Correct |
39 ms |
68428 KB |
Output is correct |
32 |
Correct |
39 ms |
68532 KB |
Output is correct |
33 |
Correct |
38 ms |
68556 KB |
Output is correct |
34 |
Correct |
38 ms |
68448 KB |
Output is correct |
35 |
Correct |
41 ms |
68520 KB |
Output is correct |
36 |
Correct |
42 ms |
68424 KB |
Output is correct |
37 |
Correct |
39 ms |
68428 KB |
Output is correct |
38 |
Correct |
39 ms |
68512 KB |
Output is correct |
39 |
Correct |
38 ms |
68536 KB |
Output is correct |
40 |
Correct |
39 ms |
68644 KB |
Output is correct |
41 |
Correct |
38 ms |
68520 KB |
Output is correct |
42 |
Correct |
39 ms |
68452 KB |
Output is correct |
43 |
Correct |
39 ms |
68504 KB |
Output is correct |
44 |
Correct |
39 ms |
68520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
68396 KB |
Output is correct |
2 |
Correct |
39 ms |
68468 KB |
Output is correct |
3 |
Correct |
41 ms |
68548 KB |
Output is correct |
4 |
Correct |
38 ms |
68464 KB |
Output is correct |
5 |
Correct |
39 ms |
68488 KB |
Output is correct |
6 |
Correct |
39 ms |
68424 KB |
Output is correct |
7 |
Correct |
38 ms |
68516 KB |
Output is correct |
8 |
Correct |
38 ms |
68580 KB |
Output is correct |
9 |
Correct |
40 ms |
68564 KB |
Output is correct |
10 |
Correct |
39 ms |
68544 KB |
Output is correct |
11 |
Correct |
38 ms |
68556 KB |
Output is correct |
12 |
Correct |
39 ms |
68556 KB |
Output is correct |
13 |
Correct |
41 ms |
68456 KB |
Output is correct |
14 |
Correct |
38 ms |
68464 KB |
Output is correct |
15 |
Correct |
39 ms |
68436 KB |
Output is correct |
16 |
Correct |
40 ms |
68508 KB |
Output is correct |
17 |
Correct |
39 ms |
68512 KB |
Output is correct |
18 |
Correct |
40 ms |
68424 KB |
Output is correct |
19 |
Correct |
38 ms |
68560 KB |
Output is correct |
20 |
Correct |
39 ms |
68428 KB |
Output is correct |
21 |
Correct |
38 ms |
68544 KB |
Output is correct |
22 |
Correct |
37 ms |
68456 KB |
Output is correct |
23 |
Correct |
38 ms |
68428 KB |
Output is correct |
24 |
Correct |
39 ms |
68496 KB |
Output is correct |
25 |
Correct |
44 ms |
68572 KB |
Output is correct |
26 |
Correct |
38 ms |
68556 KB |
Output is correct |
27 |
Correct |
39 ms |
68520 KB |
Output is correct |
28 |
Correct |
38 ms |
68528 KB |
Output is correct |
29 |
Correct |
38 ms |
68412 KB |
Output is correct |
30 |
Correct |
40 ms |
68432 KB |
Output is correct |
31 |
Correct |
39 ms |
68428 KB |
Output is correct |
32 |
Correct |
39 ms |
68532 KB |
Output is correct |
33 |
Correct |
38 ms |
68556 KB |
Output is correct |
34 |
Correct |
38 ms |
68448 KB |
Output is correct |
35 |
Correct |
41 ms |
68520 KB |
Output is correct |
36 |
Correct |
42 ms |
68424 KB |
Output is correct |
37 |
Correct |
39 ms |
68428 KB |
Output is correct |
38 |
Correct |
39 ms |
68512 KB |
Output is correct |
39 |
Correct |
38 ms |
68536 KB |
Output is correct |
40 |
Correct |
39 ms |
68644 KB |
Output is correct |
41 |
Correct |
38 ms |
68520 KB |
Output is correct |
42 |
Correct |
39 ms |
68452 KB |
Output is correct |
43 |
Correct |
39 ms |
68504 KB |
Output is correct |
44 |
Correct |
39 ms |
68520 KB |
Output is correct |
45 |
Correct |
44 ms |
68584 KB |
Output is correct |
46 |
Correct |
47 ms |
68876 KB |
Output is correct |
47 |
Correct |
43 ms |
68804 KB |
Output is correct |
48 |
Correct |
43 ms |
68796 KB |
Output is correct |
49 |
Correct |
42 ms |
69268 KB |
Output is correct |
50 |
Correct |
41 ms |
69248 KB |
Output is correct |
51 |
Correct |
51 ms |
69344 KB |
Output is correct |
52 |
Correct |
40 ms |
69012 KB |
Output is correct |
53 |
Correct |
47 ms |
69120 KB |
Output is correct |
54 |
Correct |
42 ms |
69000 KB |
Output is correct |
55 |
Correct |
42 ms |
69120 KB |
Output is correct |
56 |
Correct |
41 ms |
69000 KB |
Output is correct |
57 |
Correct |
44 ms |
68812 KB |
Output is correct |
58 |
Correct |
45 ms |
68852 KB |
Output is correct |
59 |
Correct |
46 ms |
68816 KB |
Output is correct |
60 |
Correct |
45 ms |
68812 KB |
Output is correct |
61 |
Correct |
42 ms |
69048 KB |
Output is correct |
62 |
Correct |
42 ms |
68964 KB |
Output is correct |
63 |
Correct |
42 ms |
69032 KB |
Output is correct |
64 |
Correct |
43 ms |
68676 KB |
Output is correct |
65 |
Correct |
42 ms |
68592 KB |
Output is correct |
66 |
Correct |
42 ms |
68684 KB |
Output is correct |
67 |
Correct |
42 ms |
68660 KB |
Output is correct |
68 |
Correct |
41 ms |
69084 KB |
Output is correct |
69 |
Correct |
41 ms |
69060 KB |
Output is correct |
70 |
Correct |
41 ms |
68920 KB |
Output is correct |
71 |
Correct |
41 ms |
68828 KB |
Output is correct |
72 |
Correct |
44 ms |
68812 KB |
Output is correct |
73 |
Correct |
44 ms |
68656 KB |
Output is correct |
74 |
Correct |
41 ms |
68828 KB |
Output is correct |
75 |
Correct |
41 ms |
68940 KB |
Output is correct |
76 |
Correct |
42 ms |
68960 KB |
Output is correct |
77 |
Correct |
42 ms |
68940 KB |
Output is correct |
78 |
Correct |
41 ms |
68784 KB |
Output is correct |
79 |
Correct |
41 ms |
68748 KB |
Output is correct |
80 |
Correct |
41 ms |
68672 KB |
Output is correct |
81 |
Correct |
42 ms |
68940 KB |
Output is correct |
82 |
Correct |
42 ms |
68940 KB |
Output is correct |
83 |
Correct |
42 ms |
68940 KB |
Output is correct |
84 |
Correct |
41 ms |
68772 KB |
Output is correct |
85 |
Correct |
41 ms |
68672 KB |
Output is correct |
86 |
Correct |
41 ms |
68684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
68396 KB |
Output is correct |
2 |
Correct |
39 ms |
68468 KB |
Output is correct |
3 |
Correct |
41 ms |
68548 KB |
Output is correct |
4 |
Correct |
38 ms |
68464 KB |
Output is correct |
5 |
Correct |
39 ms |
68488 KB |
Output is correct |
6 |
Correct |
39 ms |
68424 KB |
Output is correct |
7 |
Correct |
38 ms |
68516 KB |
Output is correct |
8 |
Correct |
38 ms |
68580 KB |
Output is correct |
9 |
Correct |
40 ms |
68564 KB |
Output is correct |
10 |
Correct |
39 ms |
68544 KB |
Output is correct |
11 |
Correct |
38 ms |
68556 KB |
Output is correct |
12 |
Correct |
39 ms |
68556 KB |
Output is correct |
13 |
Correct |
41 ms |
68456 KB |
Output is correct |
14 |
Correct |
38 ms |
68464 KB |
Output is correct |
15 |
Correct |
39 ms |
68436 KB |
Output is correct |
16 |
Correct |
40 ms |
68508 KB |
Output is correct |
17 |
Correct |
39 ms |
68512 KB |
Output is correct |
18 |
Correct |
40 ms |
68424 KB |
Output is correct |
19 |
Correct |
38 ms |
68560 KB |
Output is correct |
20 |
Correct |
39 ms |
68428 KB |
Output is correct |
21 |
Correct |
38 ms |
68544 KB |
Output is correct |
22 |
Correct |
37 ms |
68456 KB |
Output is correct |
23 |
Correct |
38 ms |
68428 KB |
Output is correct |
24 |
Correct |
39 ms |
68496 KB |
Output is correct |
25 |
Correct |
44 ms |
68572 KB |
Output is correct |
26 |
Correct |
38 ms |
68556 KB |
Output is correct |
27 |
Correct |
39 ms |
68520 KB |
Output is correct |
28 |
Correct |
38 ms |
68528 KB |
Output is correct |
29 |
Correct |
38 ms |
68412 KB |
Output is correct |
30 |
Correct |
40 ms |
68432 KB |
Output is correct |
31 |
Correct |
39 ms |
68428 KB |
Output is correct |
32 |
Correct |
39 ms |
68532 KB |
Output is correct |
33 |
Correct |
38 ms |
68556 KB |
Output is correct |
34 |
Correct |
38 ms |
68448 KB |
Output is correct |
35 |
Correct |
41 ms |
68520 KB |
Output is correct |
36 |
Correct |
42 ms |
68424 KB |
Output is correct |
37 |
Correct |
39 ms |
68428 KB |
Output is correct |
38 |
Correct |
39 ms |
68512 KB |
Output is correct |
39 |
Correct |
38 ms |
68536 KB |
Output is correct |
40 |
Correct |
39 ms |
68644 KB |
Output is correct |
41 |
Correct |
38 ms |
68520 KB |
Output is correct |
42 |
Correct |
39 ms |
68452 KB |
Output is correct |
43 |
Correct |
39 ms |
68504 KB |
Output is correct |
44 |
Correct |
39 ms |
68520 KB |
Output is correct |
45 |
Correct |
44 ms |
68584 KB |
Output is correct |
46 |
Correct |
47 ms |
68876 KB |
Output is correct |
47 |
Correct |
43 ms |
68804 KB |
Output is correct |
48 |
Correct |
43 ms |
68796 KB |
Output is correct |
49 |
Correct |
42 ms |
69268 KB |
Output is correct |
50 |
Correct |
41 ms |
69248 KB |
Output is correct |
51 |
Correct |
51 ms |
69344 KB |
Output is correct |
52 |
Correct |
40 ms |
69012 KB |
Output is correct |
53 |
Correct |
47 ms |
69120 KB |
Output is correct |
54 |
Correct |
42 ms |
69000 KB |
Output is correct |
55 |
Correct |
42 ms |
69120 KB |
Output is correct |
56 |
Correct |
41 ms |
69000 KB |
Output is correct |
57 |
Correct |
44 ms |
68812 KB |
Output is correct |
58 |
Correct |
45 ms |
68852 KB |
Output is correct |
59 |
Correct |
46 ms |
68816 KB |
Output is correct |
60 |
Correct |
45 ms |
68812 KB |
Output is correct |
61 |
Correct |
42 ms |
69048 KB |
Output is correct |
62 |
Correct |
42 ms |
68964 KB |
Output is correct |
63 |
Correct |
42 ms |
69032 KB |
Output is correct |
64 |
Correct |
43 ms |
68676 KB |
Output is correct |
65 |
Correct |
42 ms |
68592 KB |
Output is correct |
66 |
Correct |
42 ms |
68684 KB |
Output is correct |
67 |
Correct |
42 ms |
68660 KB |
Output is correct |
68 |
Correct |
41 ms |
69084 KB |
Output is correct |
69 |
Correct |
41 ms |
69060 KB |
Output is correct |
70 |
Correct |
41 ms |
68920 KB |
Output is correct |
71 |
Correct |
41 ms |
68828 KB |
Output is correct |
72 |
Correct |
44 ms |
68812 KB |
Output is correct |
73 |
Correct |
44 ms |
68656 KB |
Output is correct |
74 |
Correct |
41 ms |
68828 KB |
Output is correct |
75 |
Correct |
41 ms |
68940 KB |
Output is correct |
76 |
Correct |
42 ms |
68960 KB |
Output is correct |
77 |
Correct |
42 ms |
68940 KB |
Output is correct |
78 |
Correct |
41 ms |
68784 KB |
Output is correct |
79 |
Correct |
41 ms |
68748 KB |
Output is correct |
80 |
Correct |
41 ms |
68672 KB |
Output is correct |
81 |
Correct |
42 ms |
68940 KB |
Output is correct |
82 |
Correct |
42 ms |
68940 KB |
Output is correct |
83 |
Correct |
42 ms |
68940 KB |
Output is correct |
84 |
Correct |
41 ms |
68772 KB |
Output is correct |
85 |
Correct |
41 ms |
68672 KB |
Output is correct |
86 |
Correct |
41 ms |
68684 KB |
Output is correct |
87 |
Correct |
52 ms |
69452 KB |
Output is correct |
88 |
Correct |
87 ms |
71484 KB |
Output is correct |
89 |
Correct |
231 ms |
78492 KB |
Output is correct |
90 |
Correct |
237 ms |
78480 KB |
Output is correct |
91 |
Correct |
226 ms |
78476 KB |
Output is correct |
92 |
Correct |
156 ms |
90492 KB |
Output is correct |
93 |
Correct |
172 ms |
90704 KB |
Output is correct |
94 |
Correct |
162 ms |
90552 KB |
Output is correct |
95 |
Correct |
156 ms |
84544 KB |
Output is correct |
96 |
Correct |
162 ms |
84548 KB |
Output is correct |
97 |
Correct |
163 ms |
84676 KB |
Output is correct |
98 |
Correct |
162 ms |
84516 KB |
Output is correct |
99 |
Correct |
162 ms |
83780 KB |
Output is correct |
100 |
Correct |
312 ms |
78980 KB |
Output is correct |
101 |
Correct |
323 ms |
78960 KB |
Output is correct |
102 |
Correct |
372 ms |
79012 KB |
Output is correct |
103 |
Correct |
333 ms |
78908 KB |
Output is correct |
104 |
Correct |
171 ms |
83808 KB |
Output is correct |
105 |
Correct |
182 ms |
83768 KB |
Output is correct |
106 |
Correct |
170 ms |
83804 KB |
Output is correct |
107 |
Correct |
163 ms |
73432 KB |
Output is correct |
108 |
Correct |
182 ms |
73408 KB |
Output is correct |
109 |
Correct |
213 ms |
74948 KB |
Output is correct |
110 |
Correct |
116 ms |
85440 KB |
Output is correct |
111 |
Correct |
159 ms |
84532 KB |
Output is correct |
112 |
Correct |
118 ms |
79508 KB |
Output is correct |
113 |
Correct |
117 ms |
78708 KB |
Output is correct |
114 |
Correct |
309 ms |
78960 KB |
Output is correct |
115 |
Correct |
291 ms |
73920 KB |
Output is correct |
116 |
Correct |
128 ms |
78776 KB |
Output is correct |
117 |
Correct |
161 ms |
81236 KB |
Output is correct |
118 |
Correct |
161 ms |
80356 KB |
Output is correct |
119 |
Correct |
158 ms |
80000 KB |
Output is correct |
120 |
Correct |
120 ms |
76236 KB |
Output is correct |
121 |
Correct |
116 ms |
75580 KB |
Output is correct |
122 |
Correct |
118 ms |
75156 KB |
Output is correct |
123 |
Correct |
173 ms |
81244 KB |
Output is correct |
124 |
Correct |
172 ms |
80484 KB |
Output is correct |
125 |
Correct |
167 ms |
80064 KB |
Output is correct |
126 |
Correct |
128 ms |
76404 KB |
Output is correct |
127 |
Correct |
127 ms |
75644 KB |
Output is correct |
128 |
Correct |
129 ms |
75264 KB |
Output is correct |