#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) {
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]];
}
}
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);
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);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
68472 KB |
Output is correct |
2 |
Correct |
80 ms |
68588 KB |
Output is correct |
3 |
Correct |
62 ms |
68656 KB |
Output is correct |
4 |
Correct |
72 ms |
68856 KB |
Output is correct |
5 |
Correct |
69 ms |
69036 KB |
Output is correct |
6 |
Correct |
90 ms |
69040 KB |
Output is correct |
7 |
Correct |
69 ms |
69040 KB |
Output is correct |
8 |
Correct |
62 ms |
69040 KB |
Output is correct |
9 |
Correct |
60 ms |
69040 KB |
Output is correct |
10 |
Correct |
60 ms |
69040 KB |
Output is correct |
11 |
Correct |
71 ms |
69040 KB |
Output is correct |
12 |
Correct |
71 ms |
69040 KB |
Output is correct |
13 |
Correct |
68 ms |
69040 KB |
Output is correct |
14 |
Correct |
72 ms |
69040 KB |
Output is correct |
15 |
Correct |
66 ms |
69040 KB |
Output is correct |
16 |
Correct |
66 ms |
69040 KB |
Output is correct |
17 |
Correct |
66 ms |
69040 KB |
Output is correct |
18 |
Correct |
67 ms |
69040 KB |
Output is correct |
19 |
Correct |
70 ms |
69040 KB |
Output is correct |
20 |
Correct |
65 ms |
69040 KB |
Output is correct |
21 |
Correct |
68 ms |
69040 KB |
Output is correct |
22 |
Correct |
81 ms |
69040 KB |
Output is correct |
23 |
Correct |
66 ms |
69040 KB |
Output is correct |
24 |
Correct |
66 ms |
69040 KB |
Output is correct |
25 |
Correct |
66 ms |
69040 KB |
Output is correct |
26 |
Correct |
60 ms |
69040 KB |
Output is correct |
27 |
Correct |
65 ms |
69040 KB |
Output is correct |
28 |
Correct |
67 ms |
69040 KB |
Output is correct |
29 |
Correct |
66 ms |
69040 KB |
Output is correct |
30 |
Correct |
65 ms |
69040 KB |
Output is correct |
31 |
Correct |
65 ms |
69040 KB |
Output is correct |
32 |
Correct |
65 ms |
69040 KB |
Output is correct |
33 |
Correct |
73 ms |
69040 KB |
Output is correct |
34 |
Correct |
64 ms |
69040 KB |
Output is correct |
35 |
Correct |
64 ms |
69040 KB |
Output is correct |
36 |
Correct |
66 ms |
69040 KB |
Output is correct |
37 |
Correct |
77 ms |
69068 KB |
Output is correct |
38 |
Correct |
66 ms |
69068 KB |
Output is correct |
39 |
Correct |
65 ms |
69068 KB |
Output is correct |
40 |
Correct |
65 ms |
69068 KB |
Output is correct |
41 |
Correct |
66 ms |
69068 KB |
Output is correct |
42 |
Correct |
66 ms |
69068 KB |
Output is correct |
43 |
Correct |
65 ms |
69068 KB |
Output is correct |
44 |
Correct |
80 ms |
69068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
68472 KB |
Output is correct |
2 |
Correct |
80 ms |
68588 KB |
Output is correct |
3 |
Correct |
62 ms |
68656 KB |
Output is correct |
4 |
Correct |
72 ms |
68856 KB |
Output is correct |
5 |
Correct |
69 ms |
69036 KB |
Output is correct |
6 |
Correct |
90 ms |
69040 KB |
Output is correct |
7 |
Correct |
69 ms |
69040 KB |
Output is correct |
8 |
Correct |
62 ms |
69040 KB |
Output is correct |
9 |
Correct |
60 ms |
69040 KB |
Output is correct |
10 |
Correct |
60 ms |
69040 KB |
Output is correct |
11 |
Correct |
71 ms |
69040 KB |
Output is correct |
12 |
Correct |
71 ms |
69040 KB |
Output is correct |
13 |
Correct |
68 ms |
69040 KB |
Output is correct |
14 |
Correct |
72 ms |
69040 KB |
Output is correct |
15 |
Correct |
66 ms |
69040 KB |
Output is correct |
16 |
Correct |
66 ms |
69040 KB |
Output is correct |
17 |
Correct |
66 ms |
69040 KB |
Output is correct |
18 |
Correct |
67 ms |
69040 KB |
Output is correct |
19 |
Correct |
70 ms |
69040 KB |
Output is correct |
20 |
Correct |
65 ms |
69040 KB |
Output is correct |
21 |
Correct |
68 ms |
69040 KB |
Output is correct |
22 |
Correct |
81 ms |
69040 KB |
Output is correct |
23 |
Correct |
66 ms |
69040 KB |
Output is correct |
24 |
Correct |
66 ms |
69040 KB |
Output is correct |
25 |
Correct |
66 ms |
69040 KB |
Output is correct |
26 |
Correct |
60 ms |
69040 KB |
Output is correct |
27 |
Correct |
65 ms |
69040 KB |
Output is correct |
28 |
Correct |
67 ms |
69040 KB |
Output is correct |
29 |
Correct |
66 ms |
69040 KB |
Output is correct |
30 |
Correct |
65 ms |
69040 KB |
Output is correct |
31 |
Correct |
65 ms |
69040 KB |
Output is correct |
32 |
Correct |
65 ms |
69040 KB |
Output is correct |
33 |
Correct |
73 ms |
69040 KB |
Output is correct |
34 |
Correct |
64 ms |
69040 KB |
Output is correct |
35 |
Correct |
64 ms |
69040 KB |
Output is correct |
36 |
Correct |
66 ms |
69040 KB |
Output is correct |
37 |
Correct |
77 ms |
69068 KB |
Output is correct |
38 |
Correct |
66 ms |
69068 KB |
Output is correct |
39 |
Correct |
65 ms |
69068 KB |
Output is correct |
40 |
Correct |
65 ms |
69068 KB |
Output is correct |
41 |
Correct |
66 ms |
69068 KB |
Output is correct |
42 |
Correct |
66 ms |
69068 KB |
Output is correct |
43 |
Correct |
65 ms |
69068 KB |
Output is correct |
44 |
Correct |
80 ms |
69068 KB |
Output is correct |
45 |
Correct |
68 ms |
69244 KB |
Output is correct |
46 |
Correct |
78 ms |
70140 KB |
Output is correct |
47 |
Correct |
75 ms |
70268 KB |
Output is correct |
48 |
Correct |
76 ms |
70268 KB |
Output is correct |
49 |
Correct |
62 ms |
70268 KB |
Output is correct |
50 |
Correct |
67 ms |
70268 KB |
Output is correct |
51 |
Correct |
62 ms |
70268 KB |
Output is correct |
52 |
Correct |
93 ms |
70268 KB |
Output is correct |
53 |
Correct |
82 ms |
70268 KB |
Output is correct |
54 |
Correct |
73 ms |
70268 KB |
Output is correct |
55 |
Correct |
73 ms |
70268 KB |
Output is correct |
56 |
Correct |
70 ms |
70268 KB |
Output is correct |
57 |
Correct |
83 ms |
70828 KB |
Output is correct |
58 |
Correct |
79 ms |
70932 KB |
Output is correct |
59 |
Correct |
80 ms |
70932 KB |
Output is correct |
60 |
Correct |
79 ms |
70932 KB |
Output is correct |
61 |
Correct |
75 ms |
70932 KB |
Output is correct |
62 |
Correct |
106 ms |
70932 KB |
Output is correct |
63 |
Correct |
73 ms |
70932 KB |
Output is correct |
64 |
Correct |
73 ms |
70932 KB |
Output is correct |
65 |
Correct |
77 ms |
70932 KB |
Output is correct |
66 |
Correct |
73 ms |
70932 KB |
Output is correct |
67 |
Correct |
73 ms |
70932 KB |
Output is correct |
68 |
Correct |
68 ms |
70932 KB |
Output is correct |
69 |
Correct |
74 ms |
70932 KB |
Output is correct |
70 |
Correct |
76 ms |
70932 KB |
Output is correct |
71 |
Correct |
72 ms |
70932 KB |
Output is correct |
72 |
Correct |
79 ms |
70932 KB |
Output is correct |
73 |
Correct |
79 ms |
70932 KB |
Output is correct |
74 |
Correct |
86 ms |
70932 KB |
Output is correct |
75 |
Correct |
69 ms |
70932 KB |
Output is correct |
76 |
Correct |
71 ms |
70932 KB |
Output is correct |
77 |
Correct |
71 ms |
70932 KB |
Output is correct |
78 |
Correct |
70 ms |
70932 KB |
Output is correct |
79 |
Correct |
70 ms |
70932 KB |
Output is correct |
80 |
Correct |
72 ms |
70932 KB |
Output is correct |
81 |
Correct |
84 ms |
70932 KB |
Output is correct |
82 |
Correct |
72 ms |
70932 KB |
Output is correct |
83 |
Correct |
74 ms |
70932 KB |
Output is correct |
84 |
Correct |
69 ms |
70932 KB |
Output is correct |
85 |
Correct |
75 ms |
70932 KB |
Output is correct |
86 |
Correct |
71 ms |
70932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
68472 KB |
Output is correct |
2 |
Correct |
80 ms |
68588 KB |
Output is correct |
3 |
Correct |
62 ms |
68656 KB |
Output is correct |
4 |
Correct |
72 ms |
68856 KB |
Output is correct |
5 |
Correct |
69 ms |
69036 KB |
Output is correct |
6 |
Correct |
90 ms |
69040 KB |
Output is correct |
7 |
Correct |
69 ms |
69040 KB |
Output is correct |
8 |
Correct |
62 ms |
69040 KB |
Output is correct |
9 |
Correct |
60 ms |
69040 KB |
Output is correct |
10 |
Correct |
60 ms |
69040 KB |
Output is correct |
11 |
Correct |
71 ms |
69040 KB |
Output is correct |
12 |
Correct |
71 ms |
69040 KB |
Output is correct |
13 |
Correct |
68 ms |
69040 KB |
Output is correct |
14 |
Correct |
72 ms |
69040 KB |
Output is correct |
15 |
Correct |
66 ms |
69040 KB |
Output is correct |
16 |
Correct |
66 ms |
69040 KB |
Output is correct |
17 |
Correct |
66 ms |
69040 KB |
Output is correct |
18 |
Correct |
67 ms |
69040 KB |
Output is correct |
19 |
Correct |
70 ms |
69040 KB |
Output is correct |
20 |
Correct |
65 ms |
69040 KB |
Output is correct |
21 |
Correct |
68 ms |
69040 KB |
Output is correct |
22 |
Correct |
81 ms |
69040 KB |
Output is correct |
23 |
Correct |
66 ms |
69040 KB |
Output is correct |
24 |
Correct |
66 ms |
69040 KB |
Output is correct |
25 |
Correct |
66 ms |
69040 KB |
Output is correct |
26 |
Correct |
60 ms |
69040 KB |
Output is correct |
27 |
Correct |
65 ms |
69040 KB |
Output is correct |
28 |
Correct |
67 ms |
69040 KB |
Output is correct |
29 |
Correct |
66 ms |
69040 KB |
Output is correct |
30 |
Correct |
65 ms |
69040 KB |
Output is correct |
31 |
Correct |
65 ms |
69040 KB |
Output is correct |
32 |
Correct |
65 ms |
69040 KB |
Output is correct |
33 |
Correct |
73 ms |
69040 KB |
Output is correct |
34 |
Correct |
64 ms |
69040 KB |
Output is correct |
35 |
Correct |
64 ms |
69040 KB |
Output is correct |
36 |
Correct |
66 ms |
69040 KB |
Output is correct |
37 |
Correct |
77 ms |
69068 KB |
Output is correct |
38 |
Correct |
66 ms |
69068 KB |
Output is correct |
39 |
Correct |
65 ms |
69068 KB |
Output is correct |
40 |
Correct |
65 ms |
69068 KB |
Output is correct |
41 |
Correct |
66 ms |
69068 KB |
Output is correct |
42 |
Correct |
66 ms |
69068 KB |
Output is correct |
43 |
Correct |
65 ms |
69068 KB |
Output is correct |
44 |
Correct |
80 ms |
69068 KB |
Output is correct |
45 |
Correct |
68 ms |
69244 KB |
Output is correct |
46 |
Correct |
78 ms |
70140 KB |
Output is correct |
47 |
Correct |
75 ms |
70268 KB |
Output is correct |
48 |
Correct |
76 ms |
70268 KB |
Output is correct |
49 |
Correct |
62 ms |
70268 KB |
Output is correct |
50 |
Correct |
67 ms |
70268 KB |
Output is correct |
51 |
Correct |
62 ms |
70268 KB |
Output is correct |
52 |
Correct |
93 ms |
70268 KB |
Output is correct |
53 |
Correct |
82 ms |
70268 KB |
Output is correct |
54 |
Correct |
73 ms |
70268 KB |
Output is correct |
55 |
Correct |
73 ms |
70268 KB |
Output is correct |
56 |
Correct |
70 ms |
70268 KB |
Output is correct |
57 |
Correct |
83 ms |
70828 KB |
Output is correct |
58 |
Correct |
79 ms |
70932 KB |
Output is correct |
59 |
Correct |
80 ms |
70932 KB |
Output is correct |
60 |
Correct |
79 ms |
70932 KB |
Output is correct |
61 |
Correct |
75 ms |
70932 KB |
Output is correct |
62 |
Correct |
106 ms |
70932 KB |
Output is correct |
63 |
Correct |
73 ms |
70932 KB |
Output is correct |
64 |
Correct |
73 ms |
70932 KB |
Output is correct |
65 |
Correct |
77 ms |
70932 KB |
Output is correct |
66 |
Correct |
73 ms |
70932 KB |
Output is correct |
67 |
Correct |
73 ms |
70932 KB |
Output is correct |
68 |
Correct |
68 ms |
70932 KB |
Output is correct |
69 |
Correct |
74 ms |
70932 KB |
Output is correct |
70 |
Correct |
76 ms |
70932 KB |
Output is correct |
71 |
Correct |
72 ms |
70932 KB |
Output is correct |
72 |
Correct |
79 ms |
70932 KB |
Output is correct |
73 |
Correct |
79 ms |
70932 KB |
Output is correct |
74 |
Correct |
86 ms |
70932 KB |
Output is correct |
75 |
Correct |
69 ms |
70932 KB |
Output is correct |
76 |
Correct |
71 ms |
70932 KB |
Output is correct |
77 |
Correct |
71 ms |
70932 KB |
Output is correct |
78 |
Correct |
70 ms |
70932 KB |
Output is correct |
79 |
Correct |
70 ms |
70932 KB |
Output is correct |
80 |
Correct |
72 ms |
70932 KB |
Output is correct |
81 |
Correct |
84 ms |
70932 KB |
Output is correct |
82 |
Correct |
72 ms |
70932 KB |
Output is correct |
83 |
Correct |
74 ms |
70932 KB |
Output is correct |
84 |
Correct |
69 ms |
70932 KB |
Output is correct |
85 |
Correct |
75 ms |
70932 KB |
Output is correct |
86 |
Correct |
71 ms |
70932 KB |
Output is correct |
87 |
Correct |
96 ms |
72380 KB |
Output is correct |
88 |
Correct |
158 ms |
79676 KB |
Output is correct |
89 |
Correct |
457 ms |
105012 KB |
Output is correct |
90 |
Correct |
455 ms |
105012 KB |
Output is correct |
91 |
Correct |
448 ms |
105012 KB |
Output is correct |
92 |
Correct |
189 ms |
105012 KB |
Output is correct |
93 |
Correct |
211 ms |
105012 KB |
Output is correct |
94 |
Correct |
186 ms |
105012 KB |
Output is correct |
95 |
Correct |
231 ms |
105012 KB |
Output is correct |
96 |
Correct |
235 ms |
105012 KB |
Output is correct |
97 |
Correct |
243 ms |
105012 KB |
Output is correct |
98 |
Correct |
245 ms |
105012 KB |
Output is correct |
99 |
Correct |
257 ms |
105012 KB |
Output is correct |
100 |
Correct |
624 ms |
105396 KB |
Output is correct |
101 |
Correct |
672 ms |
105396 KB |
Output is correct |
102 |
Correct |
661 ms |
105396 KB |
Output is correct |
103 |
Correct |
706 ms |
105396 KB |
Output is correct |
104 |
Correct |
265 ms |
105396 KB |
Output is correct |
105 |
Correct |
242 ms |
105396 KB |
Output is correct |
106 |
Correct |
245 ms |
105396 KB |
Output is correct |
107 |
Correct |
419 ms |
105396 KB |
Output is correct |
108 |
Correct |
405 ms |
105396 KB |
Output is correct |
109 |
Correct |
407 ms |
105396 KB |
Output is correct |
110 |
Correct |
156 ms |
105396 KB |
Output is correct |
111 |
Correct |
232 ms |
105396 KB |
Output is correct |
112 |
Correct |
221 ms |
105396 KB |
Output is correct |
113 |
Correct |
221 ms |
105396 KB |
Output is correct |
114 |
Correct |
585 ms |
105428 KB |
Output is correct |
115 |
Correct |
602 ms |
105428 KB |
Output is correct |
116 |
Correct |
223 ms |
105428 KB |
Output is correct |
117 |
Correct |
239 ms |
105428 KB |
Output is correct |
118 |
Correct |
246 ms |
105428 KB |
Output is correct |
119 |
Correct |
239 ms |
105428 KB |
Output is correct |
120 |
Correct |
223 ms |
105428 KB |
Output is correct |
121 |
Correct |
227 ms |
105428 KB |
Output is correct |
122 |
Correct |
224 ms |
105428 KB |
Output is correct |
123 |
Correct |
238 ms |
105428 KB |
Output is correct |
124 |
Correct |
248 ms |
105428 KB |
Output is correct |
125 |
Correct |
260 ms |
105428 KB |
Output is correct |
126 |
Correct |
247 ms |
105428 KB |
Output is correct |
127 |
Correct |
235 ms |
105428 KB |
Output is correct |
128 |
Correct |
240 ms |
105428 KB |
Output is correct |