#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
struct node {
node *path_par;
node *p;
node *c[2];
int live;
int cnt;
};
bool side(node *v)
{
assert(v->p);
return v->p->c[1] == v;
}
void rot(node *v)
{
assert(v->p);
node *p = v->p;
bool s = side(v);
if (p->p)
p->p->c[side(p)] = v;
v->p = p->p;
if (v->c[!s])
v->c[!s]->p = p;
p->c[s] = v->c[!s];
v->c[!s] = p;
p->p = v;
p->cnt -= v->c[s]? 1 + v->c[s]->cnt: 1;
v->cnt += p->c[!s]? 1 + p->c[!s]->cnt: 1;
v->live = p->live;
v->path_par = p->path_par;
}
void splay(node *v)
{
while (v->p && v->p->p) {
side(v) == side(v->p)? rot(v->p): rot(v);
rot(v);
}
if (v->p)
rot(v);
}
void attach_right(node *v, node *r)
{
assert(!v->c[1]);
assert(!r->p);
v->c[1] = r;
r->p = v;
v->cnt += r->cnt;
}
void detach_right(node *v)
{
if (v->c[1]) {
v->c[1]->p = 0;
v->c[1]->path_par = v;
v->cnt -= v->c[1]->cnt;
}
v->c[1] = 0;
}
vector<pii> access(node *v)
{
splay(v);
detach_right(v);
vector<pii> ans = {{v->cnt, v->live}};
while (v->path_par) {
node *u = v->path_par;
splay(u);
detach_right(u);
ans.push_back({u->cnt, u->live});
attach_right(u, v);
splay(v);
}
return ans;
}
void link(node *v, node *u)
{
assert(!v->p && !v->path_par);
assert(!u->p && !u->path_par && !u->c[0]);
u->path_par = v->path_par;
u->cnt += v->cnt;
u->c[0] = v;
v->p = u;
// live will automatically be ok
}
const int N = 100'010;
node nd[N];
int n;
int fen[N];
int fen_get(int r)
{
int ans = 0;
while (r > 0) {
ans += fen[r];
r -= r & -r;
}
return ans;
}
void fen_add(int i, int x)
{
++i;
while (i <= n) {
fen[i] += x;
i += i & -i;
}
}
vector<int *> cmper;
void compress()
{
sort(cmper.begin(),cmper.end(), [](int *x, int *y){return *x<*y;});
int cnt = 0;
int lst;
Loop (i,0,cmper.size()) {
if (i && lst != *cmper[i])
++cnt;
lst = *cmper[i];
*cmper[i] = cnt;
}
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n;
Loop (i,0,n) {
cin >> nd[i].live;
nd[i].cnt = 1;
cmper.push_back(&nd[i].live);
}
compress();
Loop (i,1,n) {
int v, u;
cin >> v >> u;
--v; --u;
auto tmp = access(&nd[v]);
link(&nd[v], &nd[u]);
ll ans = 0;
for (auto [cnt, x] : tmp) {
ans += (ll)fen_get(x) * cnt;
fen_add(x, cnt);
}
for (auto [cnt, x] : tmp)
fen_add(x, -cnt);
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
284 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
284 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
348 KB |
Output is correct |
46 |
Correct |
4 ms |
596 KB |
Output is correct |
47 |
Correct |
4 ms |
596 KB |
Output is correct |
48 |
Correct |
4 ms |
596 KB |
Output is correct |
49 |
Correct |
3 ms |
596 KB |
Output is correct |
50 |
Correct |
2 ms |
600 KB |
Output is correct |
51 |
Correct |
2 ms |
596 KB |
Output is correct |
52 |
Correct |
3 ms |
596 KB |
Output is correct |
53 |
Correct |
3 ms |
596 KB |
Output is correct |
54 |
Correct |
5 ms |
596 KB |
Output is correct |
55 |
Correct |
3 ms |
596 KB |
Output is correct |
56 |
Correct |
3 ms |
648 KB |
Output is correct |
57 |
Correct |
5 ms |
596 KB |
Output is correct |
58 |
Correct |
6 ms |
604 KB |
Output is correct |
59 |
Correct |
6 ms |
596 KB |
Output is correct |
60 |
Correct |
5 ms |
596 KB |
Output is correct |
61 |
Correct |
3 ms |
596 KB |
Output is correct |
62 |
Correct |
3 ms |
596 KB |
Output is correct |
63 |
Correct |
4 ms |
596 KB |
Output is correct |
64 |
Correct |
4 ms |
596 KB |
Output is correct |
65 |
Correct |
4 ms |
596 KB |
Output is correct |
66 |
Correct |
4 ms |
596 KB |
Output is correct |
67 |
Correct |
5 ms |
596 KB |
Output is correct |
68 |
Correct |
2 ms |
596 KB |
Output is correct |
69 |
Correct |
4 ms |
600 KB |
Output is correct |
70 |
Correct |
3 ms |
600 KB |
Output is correct |
71 |
Correct |
2 ms |
596 KB |
Output is correct |
72 |
Correct |
5 ms |
596 KB |
Output is correct |
73 |
Correct |
5 ms |
596 KB |
Output is correct |
74 |
Correct |
3 ms |
560 KB |
Output is correct |
75 |
Correct |
3 ms |
596 KB |
Output is correct |
76 |
Correct |
3 ms |
596 KB |
Output is correct |
77 |
Correct |
3 ms |
596 KB |
Output is correct |
78 |
Correct |
2 ms |
632 KB |
Output is correct |
79 |
Correct |
3 ms |
596 KB |
Output is correct |
80 |
Correct |
3 ms |
600 KB |
Output is correct |
81 |
Correct |
5 ms |
596 KB |
Output is correct |
82 |
Correct |
3 ms |
596 KB |
Output is correct |
83 |
Correct |
4 ms |
596 KB |
Output is correct |
84 |
Correct |
3 ms |
596 KB |
Output is correct |
85 |
Correct |
3 ms |
560 KB |
Output is correct |
86 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
284 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
348 KB |
Output is correct |
46 |
Correct |
4 ms |
596 KB |
Output is correct |
47 |
Correct |
4 ms |
596 KB |
Output is correct |
48 |
Correct |
4 ms |
596 KB |
Output is correct |
49 |
Correct |
3 ms |
596 KB |
Output is correct |
50 |
Correct |
2 ms |
600 KB |
Output is correct |
51 |
Correct |
2 ms |
596 KB |
Output is correct |
52 |
Correct |
3 ms |
596 KB |
Output is correct |
53 |
Correct |
3 ms |
596 KB |
Output is correct |
54 |
Correct |
5 ms |
596 KB |
Output is correct |
55 |
Correct |
3 ms |
596 KB |
Output is correct |
56 |
Correct |
3 ms |
648 KB |
Output is correct |
57 |
Correct |
5 ms |
596 KB |
Output is correct |
58 |
Correct |
6 ms |
604 KB |
Output is correct |
59 |
Correct |
6 ms |
596 KB |
Output is correct |
60 |
Correct |
5 ms |
596 KB |
Output is correct |
61 |
Correct |
3 ms |
596 KB |
Output is correct |
62 |
Correct |
3 ms |
596 KB |
Output is correct |
63 |
Correct |
4 ms |
596 KB |
Output is correct |
64 |
Correct |
4 ms |
596 KB |
Output is correct |
65 |
Correct |
4 ms |
596 KB |
Output is correct |
66 |
Correct |
4 ms |
596 KB |
Output is correct |
67 |
Correct |
5 ms |
596 KB |
Output is correct |
68 |
Correct |
2 ms |
596 KB |
Output is correct |
69 |
Correct |
4 ms |
600 KB |
Output is correct |
70 |
Correct |
3 ms |
600 KB |
Output is correct |
71 |
Correct |
2 ms |
596 KB |
Output is correct |
72 |
Correct |
5 ms |
596 KB |
Output is correct |
73 |
Correct |
5 ms |
596 KB |
Output is correct |
74 |
Correct |
3 ms |
560 KB |
Output is correct |
75 |
Correct |
3 ms |
596 KB |
Output is correct |
76 |
Correct |
3 ms |
596 KB |
Output is correct |
77 |
Correct |
3 ms |
596 KB |
Output is correct |
78 |
Correct |
2 ms |
632 KB |
Output is correct |
79 |
Correct |
3 ms |
596 KB |
Output is correct |
80 |
Correct |
3 ms |
600 KB |
Output is correct |
81 |
Correct |
5 ms |
596 KB |
Output is correct |
82 |
Correct |
3 ms |
596 KB |
Output is correct |
83 |
Correct |
4 ms |
596 KB |
Output is correct |
84 |
Correct |
3 ms |
596 KB |
Output is correct |
85 |
Correct |
3 ms |
560 KB |
Output is correct |
86 |
Correct |
3 ms |
596 KB |
Output is correct |
87 |
Correct |
11 ms |
1108 KB |
Output is correct |
88 |
Correct |
34 ms |
2684 KB |
Output is correct |
89 |
Correct |
136 ms |
7916 KB |
Output is correct |
90 |
Correct |
169 ms |
7840 KB |
Output is correct |
91 |
Correct |
136 ms |
8016 KB |
Output is correct |
92 |
Correct |
55 ms |
7876 KB |
Output is correct |
93 |
Correct |
56 ms |
7756 KB |
Output is correct |
94 |
Correct |
63 ms |
7840 KB |
Output is correct |
95 |
Correct |
63 ms |
8164 KB |
Output is correct |
96 |
Correct |
63 ms |
8596 KB |
Output is correct |
97 |
Correct |
72 ms |
8728 KB |
Output is correct |
98 |
Correct |
78 ms |
8520 KB |
Output is correct |
99 |
Correct |
80 ms |
7992 KB |
Output is correct |
100 |
Correct |
186 ms |
7624 KB |
Output is correct |
101 |
Correct |
209 ms |
8136 KB |
Output is correct |
102 |
Correct |
226 ms |
7924 KB |
Output is correct |
103 |
Correct |
209 ms |
8028 KB |
Output is correct |
104 |
Correct |
91 ms |
7924 KB |
Output is correct |
105 |
Correct |
114 ms |
7912 KB |
Output is correct |
106 |
Correct |
88 ms |
7920 KB |
Output is correct |
107 |
Correct |
123 ms |
6752 KB |
Output is correct |
108 |
Correct |
119 ms |
6852 KB |
Output is correct |
109 |
Correct |
147 ms |
7240 KB |
Output is correct |
110 |
Correct |
51 ms |
6856 KB |
Output is correct |
111 |
Correct |
78 ms |
8140 KB |
Output is correct |
112 |
Correct |
51 ms |
7624 KB |
Output is correct |
113 |
Correct |
56 ms |
6984 KB |
Output is correct |
114 |
Correct |
200 ms |
7672 KB |
Output is correct |
115 |
Correct |
191 ms |
6936 KB |
Output is correct |
116 |
Correct |
75 ms |
6988 KB |
Output is correct |
117 |
Correct |
64 ms |
7700 KB |
Output is correct |
118 |
Correct |
65 ms |
7552 KB |
Output is correct |
119 |
Correct |
63 ms |
7624 KB |
Output is correct |
120 |
Correct |
62 ms |
6804 KB |
Output is correct |
121 |
Correct |
73 ms |
6904 KB |
Output is correct |
122 |
Correct |
84 ms |
6948 KB |
Output is correct |
123 |
Correct |
97 ms |
7624 KB |
Output is correct |
124 |
Correct |
98 ms |
7596 KB |
Output is correct |
125 |
Correct |
107 ms |
7644 KB |
Output is correct |
126 |
Correct |
87 ms |
6960 KB |
Output is correct |
127 |
Correct |
84 ms |
7004 KB |
Output is correct |
128 |
Correct |
100 ms |
6980 KB |
Output is correct |