#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, c[N];
vector < pair<int,int> > ed;
vector <int> z; // compression
vector <int> g[N];
int skip[N], par[N], dep[N], nchild[N];
struct data {
int low; int cnt; int val;
bool operator < (const data &other) const {
return low < other.low;
}
};
deque <data> dq[N]; // (lowest_depth, cnt, val)
void dfs(int u) {
nchild[u] = 1;
skip[u] = u;
for (int &v : g[u]) {
dep[v] = dep[u] + 1;
par[v] = u;
dfs(v);
nchild[u] += nchild[v];
}
}
void hld(int u) {
for (int &v : g[u]) {
if (nchild[v] * 2 >= nchild[u]) {
skip[v] = skip[u];
}
hld(v);
}
}
// -----------------------------------------------
// BIT
int T[N];
void upd(int x, int v) { for (; x < N; x += x & -x) T[x] += v; }
int get(int x) { int ret = 0; for (; x > 0; x -= x & -x) ret += T[x]; return ret; }
vector < pair<int,int> > buf;
long long calc_inv(int u) {
long long ret = 0;
while(u) {
int root = skip[u];
assert(dq[root].size() > 0);
int pos = upper_bound(dq[root].begin(), dq[root].end(), data({dep[u], 0, 0})) - dq[root].begin() - 1;
int curdep = dep[u];
while(pos >= 0) {
int curcnt = curdep - dq[root][pos].low + 1;
// get
ret += 1LL * curcnt * get(dq[root][pos].val - 1);
// update the current segment to BIT
upd(dq[root][pos].val, curcnt);
buf.push_back(make_pair(dq[root][pos].val, curcnt));
// -> new position
curdep = dq[root][pos].low - 1;
--pos;
}
u = par[skip[u]];
}
// reset
while(buf.size()) {
int x = buf.back().first, v = buf.back().second;
upd(x, -v);
buf.pop_back();
}
return ret;
}
void add(int u) {
//cerr << "ADD " << u << endl;
int val = c[u];
// climb up
while(u) { // par[1] = 0
int curdep = dep[u];
int cnt = 0;
while(dq[skip[u]].size() > 1 && dq[skip[u]][1].low <= curdep) {
cnt += dq[skip[u]].front().cnt;
dq[skip[u]].pop_front();
}
if (dq[skip[u]].size()) {
cnt += curdep - dq[skip[u]][0].low + 1;
dq[skip[u]][0].cnt -= curdep - dq[skip[u]][0].low + 1;
dq[skip[u]][0].low = curdep + 1;
if (dq[skip[u]][0].cnt <= 0) {
dq[skip[u]].pop_front();
}
} else {
cnt = 1;
}
dq[skip[u]].push_front({dep[skip[u]], cnt, val});
u = par[skip[u]];
}
// for (int i = 1; i <= n; ++i) if (i == skip[i] && dq[i].size()) {
// cerr << "dq " << i << endl;
// for (auto &j : dq[i]) cerr << "low " << j.low << " cnt " << j.cnt << " val " << j.val << endl;
// }
// cerr << endl << endl;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> c[i];
z.push_back(c[i]);
}
sort(z.begin(), z.end());
z.erase(unique(z.begin(), z.end()), z.end());
for (int i = 1; i <= n; ++i) {
c[i] = lower_bound(z.begin(), z.end(), c[i]) - z.begin() + 1;
}
for (int i = 1; i < n; ++i) {
int u, v; cin >> u >> v;
ed.push_back(make_pair(u, v));
g[u].push_back(v);
}
// solve
dfs(1);
hld(1);
// for (int i = 1; i <= n; ++i) {
// cerr << i << ' ' << dep[i] << ' ' << skip[i] << endl;
// }
dq[1].push_back({0, 1, c[1]});
for (auto &e : ed) {
int u = e.first, v = e.second;
printf("%lld\n", calc_inv(u));
add(v);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
68472 KB |
Output is correct |
2 |
Correct |
78 ms |
68712 KB |
Output is correct |
3 |
Correct |
69 ms |
68788 KB |
Output is correct |
4 |
Correct |
75 ms |
68788 KB |
Output is correct |
5 |
Correct |
73 ms |
68840 KB |
Output is correct |
6 |
Correct |
80 ms |
68840 KB |
Output is correct |
7 |
Correct |
72 ms |
68840 KB |
Output is correct |
8 |
Correct |
65 ms |
68840 KB |
Output is correct |
9 |
Correct |
69 ms |
68840 KB |
Output is correct |
10 |
Correct |
75 ms |
68840 KB |
Output is correct |
11 |
Correct |
100 ms |
68880 KB |
Output is correct |
12 |
Correct |
92 ms |
68880 KB |
Output is correct |
13 |
Correct |
95 ms |
68912 KB |
Output is correct |
14 |
Correct |
79 ms |
68932 KB |
Output is correct |
15 |
Correct |
76 ms |
68964 KB |
Output is correct |
16 |
Correct |
82 ms |
68964 KB |
Output is correct |
17 |
Correct |
101 ms |
68964 KB |
Output is correct |
18 |
Correct |
94 ms |
68964 KB |
Output is correct |
19 |
Correct |
82 ms |
68964 KB |
Output is correct |
20 |
Correct |
78 ms |
68964 KB |
Output is correct |
21 |
Correct |
73 ms |
68964 KB |
Output is correct |
22 |
Correct |
81 ms |
68964 KB |
Output is correct |
23 |
Correct |
78 ms |
68964 KB |
Output is correct |
24 |
Correct |
72 ms |
68964 KB |
Output is correct |
25 |
Correct |
73 ms |
68964 KB |
Output is correct |
26 |
Correct |
65 ms |
68964 KB |
Output is correct |
27 |
Correct |
78 ms |
68964 KB |
Output is correct |
28 |
Correct |
73 ms |
68964 KB |
Output is correct |
29 |
Correct |
74 ms |
68992 KB |
Output is correct |
30 |
Correct |
82 ms |
69020 KB |
Output is correct |
31 |
Correct |
75 ms |
69092 KB |
Output is correct |
32 |
Correct |
73 ms |
69092 KB |
Output is correct |
33 |
Correct |
75 ms |
69092 KB |
Output is correct |
34 |
Correct |
73 ms |
69092 KB |
Output is correct |
35 |
Correct |
77 ms |
69092 KB |
Output is correct |
36 |
Correct |
79 ms |
69092 KB |
Output is correct |
37 |
Correct |
73 ms |
69092 KB |
Output is correct |
38 |
Correct |
72 ms |
69092 KB |
Output is correct |
39 |
Correct |
88 ms |
69092 KB |
Output is correct |
40 |
Correct |
74 ms |
69092 KB |
Output is correct |
41 |
Correct |
82 ms |
69092 KB |
Output is correct |
42 |
Correct |
74 ms |
69092 KB |
Output is correct |
43 |
Correct |
73 ms |
69092 KB |
Output is correct |
44 |
Correct |
73 ms |
69092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
68472 KB |
Output is correct |
2 |
Correct |
78 ms |
68712 KB |
Output is correct |
3 |
Correct |
69 ms |
68788 KB |
Output is correct |
4 |
Correct |
75 ms |
68788 KB |
Output is correct |
5 |
Correct |
73 ms |
68840 KB |
Output is correct |
6 |
Correct |
80 ms |
68840 KB |
Output is correct |
7 |
Correct |
72 ms |
68840 KB |
Output is correct |
8 |
Correct |
65 ms |
68840 KB |
Output is correct |
9 |
Correct |
69 ms |
68840 KB |
Output is correct |
10 |
Correct |
75 ms |
68840 KB |
Output is correct |
11 |
Correct |
100 ms |
68880 KB |
Output is correct |
12 |
Correct |
92 ms |
68880 KB |
Output is correct |
13 |
Correct |
95 ms |
68912 KB |
Output is correct |
14 |
Correct |
79 ms |
68932 KB |
Output is correct |
15 |
Correct |
76 ms |
68964 KB |
Output is correct |
16 |
Correct |
82 ms |
68964 KB |
Output is correct |
17 |
Correct |
101 ms |
68964 KB |
Output is correct |
18 |
Correct |
94 ms |
68964 KB |
Output is correct |
19 |
Correct |
82 ms |
68964 KB |
Output is correct |
20 |
Correct |
78 ms |
68964 KB |
Output is correct |
21 |
Correct |
73 ms |
68964 KB |
Output is correct |
22 |
Correct |
81 ms |
68964 KB |
Output is correct |
23 |
Correct |
78 ms |
68964 KB |
Output is correct |
24 |
Correct |
72 ms |
68964 KB |
Output is correct |
25 |
Correct |
73 ms |
68964 KB |
Output is correct |
26 |
Correct |
65 ms |
68964 KB |
Output is correct |
27 |
Correct |
78 ms |
68964 KB |
Output is correct |
28 |
Correct |
73 ms |
68964 KB |
Output is correct |
29 |
Correct |
74 ms |
68992 KB |
Output is correct |
30 |
Correct |
82 ms |
69020 KB |
Output is correct |
31 |
Correct |
75 ms |
69092 KB |
Output is correct |
32 |
Correct |
73 ms |
69092 KB |
Output is correct |
33 |
Correct |
75 ms |
69092 KB |
Output is correct |
34 |
Correct |
73 ms |
69092 KB |
Output is correct |
35 |
Correct |
77 ms |
69092 KB |
Output is correct |
36 |
Correct |
79 ms |
69092 KB |
Output is correct |
37 |
Correct |
73 ms |
69092 KB |
Output is correct |
38 |
Correct |
72 ms |
69092 KB |
Output is correct |
39 |
Correct |
88 ms |
69092 KB |
Output is correct |
40 |
Correct |
74 ms |
69092 KB |
Output is correct |
41 |
Correct |
82 ms |
69092 KB |
Output is correct |
42 |
Correct |
74 ms |
69092 KB |
Output is correct |
43 |
Correct |
73 ms |
69092 KB |
Output is correct |
44 |
Correct |
73 ms |
69092 KB |
Output is correct |
45 |
Correct |
75 ms |
69120 KB |
Output is correct |
46 |
Correct |
81 ms |
70140 KB |
Output is correct |
47 |
Correct |
85 ms |
70332 KB |
Output is correct |
48 |
Correct |
84 ms |
70332 KB |
Output is correct |
49 |
Correct |
82 ms |
70332 KB |
Output is correct |
50 |
Correct |
70 ms |
70332 KB |
Output is correct |
51 |
Correct |
75 ms |
70332 KB |
Output is correct |
52 |
Correct |
86 ms |
70332 KB |
Output is correct |
53 |
Correct |
81 ms |
70332 KB |
Output is correct |
54 |
Correct |
79 ms |
70332 KB |
Output is correct |
55 |
Correct |
81 ms |
70332 KB |
Output is correct |
56 |
Correct |
107 ms |
70332 KB |
Output is correct |
57 |
Correct |
106 ms |
70828 KB |
Output is correct |
58 |
Correct |
87 ms |
70828 KB |
Output is correct |
59 |
Correct |
92 ms |
70828 KB |
Output is correct |
60 |
Correct |
92 ms |
70828 KB |
Output is correct |
61 |
Correct |
98 ms |
70828 KB |
Output is correct |
62 |
Correct |
79 ms |
70828 KB |
Output is correct |
63 |
Correct |
78 ms |
70828 KB |
Output is correct |
64 |
Correct |
87 ms |
70828 KB |
Output is correct |
65 |
Correct |
84 ms |
70828 KB |
Output is correct |
66 |
Correct |
88 ms |
70828 KB |
Output is correct |
67 |
Correct |
84 ms |
70828 KB |
Output is correct |
68 |
Correct |
69 ms |
70828 KB |
Output is correct |
69 |
Correct |
80 ms |
70828 KB |
Output is correct |
70 |
Correct |
81 ms |
70828 KB |
Output is correct |
71 |
Correct |
79 ms |
70828 KB |
Output is correct |
72 |
Correct |
81 ms |
70828 KB |
Output is correct |
73 |
Correct |
107 ms |
70828 KB |
Output is correct |
74 |
Correct |
85 ms |
70828 KB |
Output is correct |
75 |
Correct |
100 ms |
70828 KB |
Output is correct |
76 |
Correct |
80 ms |
70828 KB |
Output is correct |
77 |
Correct |
94 ms |
70828 KB |
Output is correct |
78 |
Correct |
92 ms |
70828 KB |
Output is correct |
79 |
Correct |
86 ms |
70828 KB |
Output is correct |
80 |
Correct |
78 ms |
70828 KB |
Output is correct |
81 |
Correct |
83 ms |
70828 KB |
Output is correct |
82 |
Correct |
79 ms |
70828 KB |
Output is correct |
83 |
Correct |
79 ms |
70828 KB |
Output is correct |
84 |
Correct |
88 ms |
70828 KB |
Output is correct |
85 |
Correct |
78 ms |
70828 KB |
Output is correct |
86 |
Correct |
71 ms |
70828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
68472 KB |
Output is correct |
2 |
Correct |
78 ms |
68712 KB |
Output is correct |
3 |
Correct |
69 ms |
68788 KB |
Output is correct |
4 |
Correct |
75 ms |
68788 KB |
Output is correct |
5 |
Correct |
73 ms |
68840 KB |
Output is correct |
6 |
Correct |
80 ms |
68840 KB |
Output is correct |
7 |
Correct |
72 ms |
68840 KB |
Output is correct |
8 |
Correct |
65 ms |
68840 KB |
Output is correct |
9 |
Correct |
69 ms |
68840 KB |
Output is correct |
10 |
Correct |
75 ms |
68840 KB |
Output is correct |
11 |
Correct |
100 ms |
68880 KB |
Output is correct |
12 |
Correct |
92 ms |
68880 KB |
Output is correct |
13 |
Correct |
95 ms |
68912 KB |
Output is correct |
14 |
Correct |
79 ms |
68932 KB |
Output is correct |
15 |
Correct |
76 ms |
68964 KB |
Output is correct |
16 |
Correct |
82 ms |
68964 KB |
Output is correct |
17 |
Correct |
101 ms |
68964 KB |
Output is correct |
18 |
Correct |
94 ms |
68964 KB |
Output is correct |
19 |
Correct |
82 ms |
68964 KB |
Output is correct |
20 |
Correct |
78 ms |
68964 KB |
Output is correct |
21 |
Correct |
73 ms |
68964 KB |
Output is correct |
22 |
Correct |
81 ms |
68964 KB |
Output is correct |
23 |
Correct |
78 ms |
68964 KB |
Output is correct |
24 |
Correct |
72 ms |
68964 KB |
Output is correct |
25 |
Correct |
73 ms |
68964 KB |
Output is correct |
26 |
Correct |
65 ms |
68964 KB |
Output is correct |
27 |
Correct |
78 ms |
68964 KB |
Output is correct |
28 |
Correct |
73 ms |
68964 KB |
Output is correct |
29 |
Correct |
74 ms |
68992 KB |
Output is correct |
30 |
Correct |
82 ms |
69020 KB |
Output is correct |
31 |
Correct |
75 ms |
69092 KB |
Output is correct |
32 |
Correct |
73 ms |
69092 KB |
Output is correct |
33 |
Correct |
75 ms |
69092 KB |
Output is correct |
34 |
Correct |
73 ms |
69092 KB |
Output is correct |
35 |
Correct |
77 ms |
69092 KB |
Output is correct |
36 |
Correct |
79 ms |
69092 KB |
Output is correct |
37 |
Correct |
73 ms |
69092 KB |
Output is correct |
38 |
Correct |
72 ms |
69092 KB |
Output is correct |
39 |
Correct |
88 ms |
69092 KB |
Output is correct |
40 |
Correct |
74 ms |
69092 KB |
Output is correct |
41 |
Correct |
82 ms |
69092 KB |
Output is correct |
42 |
Correct |
74 ms |
69092 KB |
Output is correct |
43 |
Correct |
73 ms |
69092 KB |
Output is correct |
44 |
Correct |
73 ms |
69092 KB |
Output is correct |
45 |
Correct |
75 ms |
69120 KB |
Output is correct |
46 |
Correct |
81 ms |
70140 KB |
Output is correct |
47 |
Correct |
85 ms |
70332 KB |
Output is correct |
48 |
Correct |
84 ms |
70332 KB |
Output is correct |
49 |
Correct |
82 ms |
70332 KB |
Output is correct |
50 |
Correct |
70 ms |
70332 KB |
Output is correct |
51 |
Correct |
75 ms |
70332 KB |
Output is correct |
52 |
Correct |
86 ms |
70332 KB |
Output is correct |
53 |
Correct |
81 ms |
70332 KB |
Output is correct |
54 |
Correct |
79 ms |
70332 KB |
Output is correct |
55 |
Correct |
81 ms |
70332 KB |
Output is correct |
56 |
Correct |
107 ms |
70332 KB |
Output is correct |
57 |
Correct |
106 ms |
70828 KB |
Output is correct |
58 |
Correct |
87 ms |
70828 KB |
Output is correct |
59 |
Correct |
92 ms |
70828 KB |
Output is correct |
60 |
Correct |
92 ms |
70828 KB |
Output is correct |
61 |
Correct |
98 ms |
70828 KB |
Output is correct |
62 |
Correct |
79 ms |
70828 KB |
Output is correct |
63 |
Correct |
78 ms |
70828 KB |
Output is correct |
64 |
Correct |
87 ms |
70828 KB |
Output is correct |
65 |
Correct |
84 ms |
70828 KB |
Output is correct |
66 |
Correct |
88 ms |
70828 KB |
Output is correct |
67 |
Correct |
84 ms |
70828 KB |
Output is correct |
68 |
Correct |
69 ms |
70828 KB |
Output is correct |
69 |
Correct |
80 ms |
70828 KB |
Output is correct |
70 |
Correct |
81 ms |
70828 KB |
Output is correct |
71 |
Correct |
79 ms |
70828 KB |
Output is correct |
72 |
Correct |
81 ms |
70828 KB |
Output is correct |
73 |
Correct |
107 ms |
70828 KB |
Output is correct |
74 |
Correct |
85 ms |
70828 KB |
Output is correct |
75 |
Correct |
100 ms |
70828 KB |
Output is correct |
76 |
Correct |
80 ms |
70828 KB |
Output is correct |
77 |
Correct |
94 ms |
70828 KB |
Output is correct |
78 |
Correct |
92 ms |
70828 KB |
Output is correct |
79 |
Correct |
86 ms |
70828 KB |
Output is correct |
80 |
Correct |
78 ms |
70828 KB |
Output is correct |
81 |
Correct |
83 ms |
70828 KB |
Output is correct |
82 |
Correct |
79 ms |
70828 KB |
Output is correct |
83 |
Correct |
79 ms |
70828 KB |
Output is correct |
84 |
Correct |
88 ms |
70828 KB |
Output is correct |
85 |
Correct |
78 ms |
70828 KB |
Output is correct |
86 |
Correct |
71 ms |
70828 KB |
Output is correct |
87 |
Correct |
106 ms |
72508 KB |
Output is correct |
88 |
Correct |
167 ms |
79668 KB |
Output is correct |
89 |
Correct |
513 ms |
105036 KB |
Output is correct |
90 |
Correct |
543 ms |
105036 KB |
Output is correct |
91 |
Correct |
569 ms |
105140 KB |
Output is correct |
92 |
Correct |
174 ms |
105140 KB |
Output is correct |
93 |
Correct |
177 ms |
105140 KB |
Output is correct |
94 |
Correct |
228 ms |
105140 KB |
Output is correct |
95 |
Correct |
261 ms |
105140 KB |
Output is correct |
96 |
Correct |
260 ms |
105140 KB |
Output is correct |
97 |
Correct |
264 ms |
105140 KB |
Output is correct |
98 |
Correct |
261 ms |
105140 KB |
Output is correct |
99 |
Correct |
255 ms |
105140 KB |
Output is correct |
100 |
Correct |
637 ms |
105396 KB |
Output is correct |
101 |
Correct |
751 ms |
105424 KB |
Output is correct |
102 |
Correct |
713 ms |
105424 KB |
Output is correct |
103 |
Correct |
719 ms |
105564 KB |
Output is correct |
104 |
Correct |
278 ms |
105564 KB |
Output is correct |
105 |
Correct |
256 ms |
105564 KB |
Output is correct |
106 |
Correct |
253 ms |
105564 KB |
Output is correct |
107 |
Correct |
413 ms |
105564 KB |
Output is correct |
108 |
Correct |
429 ms |
105564 KB |
Output is correct |
109 |
Correct |
445 ms |
105564 KB |
Output is correct |
110 |
Correct |
158 ms |
105564 KB |
Output is correct |
111 |
Correct |
245 ms |
105564 KB |
Output is correct |
112 |
Correct |
220 ms |
105564 KB |
Output is correct |
113 |
Correct |
215 ms |
105564 KB |
Output is correct |
114 |
Correct |
621 ms |
105564 KB |
Output is correct |
115 |
Correct |
621 ms |
105564 KB |
Output is correct |
116 |
Correct |
217 ms |
105564 KB |
Output is correct |
117 |
Correct |
237 ms |
105564 KB |
Output is correct |
118 |
Correct |
350 ms |
105564 KB |
Output is correct |
119 |
Correct |
248 ms |
105564 KB |
Output is correct |
120 |
Correct |
228 ms |
105564 KB |
Output is correct |
121 |
Correct |
228 ms |
105564 KB |
Output is correct |
122 |
Correct |
228 ms |
105564 KB |
Output is correct |
123 |
Correct |
248 ms |
105564 KB |
Output is correct |
124 |
Correct |
356 ms |
105564 KB |
Output is correct |
125 |
Correct |
249 ms |
105564 KB |
Output is correct |
126 |
Correct |
233 ms |
105564 KB |
Output is correct |
127 |
Correct |
231 ms |
105564 KB |
Output is correct |
128 |
Correct |
234 ms |
105564 KB |
Output is correct |