#include <stdio.h>
#define N 100100
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int aa[N], n;
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[ii[j]] == aa[i_])
j++;
else if (aa[ii[j]] < aa[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int ft[N];
void update(int i, int x) {
while (i <= n) {
ft[i] += x;
i |= i + 1;
}
}
int query(int i) {
int x = 0;
while (i >= 0) {
x += ft[i];
i &= i + 1, i--;
}
return x;
}
int pp[N], tt[N][2], sz[N];
int dir(int u) {
return tt[pp[u]][0] == u ? 0 : 1;
}
int isroot(int u) {
return tt[pp[u]][dir(u)] != u;
}
void pul(int u) {
sz[u] = sz[tt[u][0]] + sz[tt[u][1]] + 1;
}
void attach(int p, int u, int d) {
pp[u] = p;
if (p)
tt[p][d] = u, pul(p);
}
void rotate(int u) {
int v = pp[u], w = pp[v], du = dir(u), dv = dir(v), is = isroot(v);
attach(v, tt[u][du ^ 1], du), attach(u, v, du ^ 1);
if (is)
pp[u] = w;
else
attach(w, u, dv);
}
void splay(int u) {
while (!isroot(u)) {
int v = pp[u];
if (isroot(v))
rotate(u);
else
rotate(dir(u) == dir(v) ? v : u), rotate(u);
}
}
int root(int u) {
return isroot(u) ? u : root(pp[u]);
}
int aa_[N], kk_[N], n_;
void expose(int u) {
int v, w;
n_ = 0;
for (v = u, w = 0; v; w = v, v = pp[v]) {
int a, k;
a = aa[root(v)];
splay(v);
k = sz[tt[v][0]] + 1;
aa_[n_] = a, kk_[n_] = k, n_++;
aa[tt[v][1]] = a;
attach(v, w, 1);
}
splay(u);
}
long long solve() {
int i;
long long ans;
ans = 0;
for (i = 0; i < n_; i++) {
ans += (long long) query(aa_[i] - 1) * kk_[i];
update(aa_[i], kk_[i]);
}
for (i = 0; i < n_; i++)
update(aa_[i], -kk_[i]);
return ans;
}
int main() {
static int ii[N];
int h, i, j, a;
scanf("%d", &n);
for (i = 1; i <= n; i++)
scanf("%d", &aa[i]);
for (i = 0; i <= n; i++)
ii[i] = i;
sort(ii, 0, n + 1);
for (i = 0, a = 0; i <= n; i++)
aa[ii[i]] = i == n || aa[ii[i + 1]] != aa[ii[i]] ? a++ : a;
sz[1] = 1;
for (h = 0; h < n - 1; h++) {
scanf("%d%d", &i, &j);
expose(i);
attach(j, i, 0);
printf("%lld\n", solve());
}
return 0;
}
Compilation message
construction.c: In function 'main':
construction.c:133:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
construction.c:135:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d", &aa[i]);
| ^~~~~~~~~~~~~~~~~~~
construction.c:143:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | scanf("%d%d", &i, &j);
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
292 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 |
300 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 |
308 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 |
288 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 |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 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 |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
296 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
300 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 |
304 KB |
Output is correct |
42 |
Correct |
1 ms |
300 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
292 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 |
300 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 |
308 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 |
288 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 |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 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 |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
296 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
300 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 |
304 KB |
Output is correct |
42 |
Correct |
1 ms |
300 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
5 ms |
520 KB |
Output is correct |
47 |
Correct |
5 ms |
468 KB |
Output is correct |
48 |
Correct |
4 ms |
468 KB |
Output is correct |
49 |
Correct |
3 ms |
432 KB |
Output is correct |
50 |
Correct |
4 ms |
468 KB |
Output is correct |
51 |
Correct |
4 ms |
468 KB |
Output is correct |
52 |
Correct |
3 ms |
484 KB |
Output is correct |
53 |
Correct |
3 ms |
468 KB |
Output is correct |
54 |
Correct |
3 ms |
432 KB |
Output is correct |
55 |
Correct |
3 ms |
480 KB |
Output is correct |
56 |
Correct |
3 ms |
468 KB |
Output is correct |
57 |
Correct |
6 ms |
468 KB |
Output is correct |
58 |
Correct |
6 ms |
468 KB |
Output is correct |
59 |
Correct |
6 ms |
524 KB |
Output is correct |
60 |
Correct |
7 ms |
468 KB |
Output is correct |
61 |
Correct |
3 ms |
468 KB |
Output is correct |
62 |
Correct |
4 ms |
432 KB |
Output is correct |
63 |
Correct |
4 ms |
468 KB |
Output is correct |
64 |
Correct |
4 ms |
468 KB |
Output is correct |
65 |
Correct |
4 ms |
480 KB |
Output is correct |
66 |
Correct |
4 ms |
468 KB |
Output is correct |
67 |
Correct |
4 ms |
492 KB |
Output is correct |
68 |
Correct |
2 ms |
440 KB |
Output is correct |
69 |
Correct |
3 ms |
492 KB |
Output is correct |
70 |
Correct |
2 ms |
468 KB |
Output is correct |
71 |
Correct |
2 ms |
468 KB |
Output is correct |
72 |
Correct |
6 ms |
468 KB |
Output is correct |
73 |
Correct |
6 ms |
480 KB |
Output is correct |
74 |
Correct |
3 ms |
468 KB |
Output is correct |
75 |
Correct |
3 ms |
468 KB |
Output is correct |
76 |
Correct |
3 ms |
468 KB |
Output is correct |
77 |
Correct |
3 ms |
468 KB |
Output is correct |
78 |
Correct |
3 ms |
468 KB |
Output is correct |
79 |
Correct |
4 ms |
432 KB |
Output is correct |
80 |
Correct |
3 ms |
436 KB |
Output is correct |
81 |
Correct |
5 ms |
468 KB |
Output is correct |
82 |
Correct |
4 ms |
468 KB |
Output is correct |
83 |
Correct |
4 ms |
468 KB |
Output is correct |
84 |
Correct |
3 ms |
468 KB |
Output is correct |
85 |
Correct |
4 ms |
436 KB |
Output is correct |
86 |
Correct |
3 ms |
480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
292 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 |
300 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 |
308 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 |
288 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 |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 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 |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
296 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
300 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 |
304 KB |
Output is correct |
42 |
Correct |
1 ms |
300 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
5 ms |
520 KB |
Output is correct |
47 |
Correct |
5 ms |
468 KB |
Output is correct |
48 |
Correct |
4 ms |
468 KB |
Output is correct |
49 |
Correct |
3 ms |
432 KB |
Output is correct |
50 |
Correct |
4 ms |
468 KB |
Output is correct |
51 |
Correct |
4 ms |
468 KB |
Output is correct |
52 |
Correct |
3 ms |
484 KB |
Output is correct |
53 |
Correct |
3 ms |
468 KB |
Output is correct |
54 |
Correct |
3 ms |
432 KB |
Output is correct |
55 |
Correct |
3 ms |
480 KB |
Output is correct |
56 |
Correct |
3 ms |
468 KB |
Output is correct |
57 |
Correct |
6 ms |
468 KB |
Output is correct |
58 |
Correct |
6 ms |
468 KB |
Output is correct |
59 |
Correct |
6 ms |
524 KB |
Output is correct |
60 |
Correct |
7 ms |
468 KB |
Output is correct |
61 |
Correct |
3 ms |
468 KB |
Output is correct |
62 |
Correct |
4 ms |
432 KB |
Output is correct |
63 |
Correct |
4 ms |
468 KB |
Output is correct |
64 |
Correct |
4 ms |
468 KB |
Output is correct |
65 |
Correct |
4 ms |
480 KB |
Output is correct |
66 |
Correct |
4 ms |
468 KB |
Output is correct |
67 |
Correct |
4 ms |
492 KB |
Output is correct |
68 |
Correct |
2 ms |
440 KB |
Output is correct |
69 |
Correct |
3 ms |
492 KB |
Output is correct |
70 |
Correct |
2 ms |
468 KB |
Output is correct |
71 |
Correct |
2 ms |
468 KB |
Output is correct |
72 |
Correct |
6 ms |
468 KB |
Output is correct |
73 |
Correct |
6 ms |
480 KB |
Output is correct |
74 |
Correct |
3 ms |
468 KB |
Output is correct |
75 |
Correct |
3 ms |
468 KB |
Output is correct |
76 |
Correct |
3 ms |
468 KB |
Output is correct |
77 |
Correct |
3 ms |
468 KB |
Output is correct |
78 |
Correct |
3 ms |
468 KB |
Output is correct |
79 |
Correct |
4 ms |
432 KB |
Output is correct |
80 |
Correct |
3 ms |
436 KB |
Output is correct |
81 |
Correct |
5 ms |
468 KB |
Output is correct |
82 |
Correct |
4 ms |
468 KB |
Output is correct |
83 |
Correct |
4 ms |
468 KB |
Output is correct |
84 |
Correct |
3 ms |
468 KB |
Output is correct |
85 |
Correct |
4 ms |
436 KB |
Output is correct |
86 |
Correct |
3 ms |
480 KB |
Output is correct |
87 |
Correct |
11 ms |
800 KB |
Output is correct |
88 |
Correct |
37 ms |
1792 KB |
Output is correct |
89 |
Correct |
141 ms |
5376 KB |
Output is correct |
90 |
Correct |
145 ms |
5396 KB |
Output is correct |
91 |
Correct |
143 ms |
5324 KB |
Output is correct |
92 |
Correct |
64 ms |
5372 KB |
Output is correct |
93 |
Correct |
65 ms |
5372 KB |
Output is correct |
94 |
Correct |
61 ms |
5212 KB |
Output is correct |
95 |
Correct |
66 ms |
5212 KB |
Output is correct |
96 |
Correct |
69 ms |
5636 KB |
Output is correct |
97 |
Correct |
85 ms |
5664 KB |
Output is correct |
98 |
Correct |
70 ms |
5708 KB |
Output is correct |
99 |
Correct |
80 ms |
5452 KB |
Output is correct |
100 |
Correct |
221 ms |
5084 KB |
Output is correct |
101 |
Correct |
258 ms |
5456 KB |
Output is correct |
102 |
Correct |
249 ms |
5320 KB |
Output is correct |
103 |
Correct |
255 ms |
5420 KB |
Output is correct |
104 |
Correct |
117 ms |
5420 KB |
Output is correct |
105 |
Correct |
108 ms |
5348 KB |
Output is correct |
106 |
Correct |
108 ms |
5376 KB |
Output is correct |
107 |
Correct |
128 ms |
4144 KB |
Output is correct |
108 |
Correct |
132 ms |
4356 KB |
Output is correct |
109 |
Correct |
143 ms |
4816 KB |
Output is correct |
110 |
Correct |
61 ms |
4244 KB |
Output is correct |
111 |
Correct |
89 ms |
5248 KB |
Output is correct |
112 |
Correct |
60 ms |
4832 KB |
Output is correct |
113 |
Correct |
62 ms |
4428 KB |
Output is correct |
114 |
Correct |
220 ms |
5012 KB |
Output is correct |
115 |
Correct |
225 ms |
4396 KB |
Output is correct |
116 |
Correct |
96 ms |
4432 KB |
Output is correct |
117 |
Correct |
77 ms |
5216 KB |
Output is correct |
118 |
Correct |
78 ms |
5072 KB |
Output is correct |
119 |
Correct |
76 ms |
5068 KB |
Output is correct |
120 |
Correct |
65 ms |
4332 KB |
Output is correct |
121 |
Correct |
65 ms |
4376 KB |
Output is correct |
122 |
Correct |
84 ms |
4428 KB |
Output is correct |
123 |
Correct |
107 ms |
5148 KB |
Output is correct |
124 |
Correct |
106 ms |
5076 KB |
Output is correct |
125 |
Correct |
128 ms |
5180 KB |
Output is correct |
126 |
Correct |
108 ms |
4512 KB |
Output is correct |
127 |
Correct |
94 ms |
4440 KB |
Output is correct |
128 |
Correct |
94 ms |
4444 KB |
Output is correct |