#include <string.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void reverse(int *a, int l, int r)
{
for (int temp = --r; l < r; ++l, --r)
temp = a[l], a[l] = a[r], a[r] = temp;
}
void sort(int *a, int l, int r)
{
while (l < r)
{
int i = l, j = l, k = r, t, p = a[l + rand()%(r-l)];
while (j < k)
{
if (a[j] == p) ++j;
else if (a[j] < p) t = a[i], a[i] = a[j], a[j] = t, ++i, ++j;
else t = a[--k], a[k] = a[j], a[j] = t;
}
sort(a, l, i);
l = k;
}
}
#define N 100000
#define M (2*N)
#define L 17
int n, c[N], tin[N], hld[N], dd[N], sz[N], par[N], c2[N];
int *eh[N], eo[N], *eh_[N], eo_[N];
long long fw[N], cost;
void fwadd(int p, long long k)
{
for (; p < N; p |= p + 1) fw[p] += k;
}
long long fwsum(int p)
{
long long z = 0;
for (; p > 0; p &= p - 1) z += fw[p-1];
return z;
}
int vv[N*L], vo;
void inversions()
{
for (int j = 0; j < vo; j += 2)
fwadd(vv[j+1], vv[j]),
cost += vv[j] * fwsum(vv[j+1]);
for (int j = 0; j < vo; j += 2)
fwadd(vv[j+1], -vv[j]);
}
void push(int **eh, int *eo, int i, int j)
{
int o = eo[i]++;
if (!o) eh[i] = (int*)realloc(eh[i], 2*sizeof**eh);
else if (!(o&o-1)) eh[i] = (int*)realloc(eh[i], 2*sizeof**eh*o);
eh[i][o] = j;
}
void dfs1(int u, int p)
{
par[u] = p;
sz[u] = 1;
for (int v, j = 0; j < eo[u]; ++j) if ((v = eh[u][j]) != p)
{
dd[v] = dd[u] + 1;
dfs1(v, u);
sz[u] += sz[v];
if (eh[u][0] == p || sz[v] > sz[eh[u][0]])
eh[u][j] = eh[u][0], eh[u][0] = v;
}
}
void pushchain(int i, int j, int k)
{
if (eo_[i] >= 2 && eh_[i][eo_[i]-2] == j)
eh_[i][eo_[i]-1] += k;
else
push(eh_, eo_, i, j),
push(eh_, eo_, i, k);
}
void dfs2(int u, int p)
{
static int timer = 0;
tin[u] = timer++;
for (int v, j = 0; j < eo[u]; ++j) if ((v = eh[u][j]) != p)
hld[v] = (j == 0) ? hld[u] : v,
dfs2(v, u);
pushchain(hld[u], c[u], 1);
}
int main()
{
srand(time(0));
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", c+i);
memcpy(c2, c, sizeof *c2 * n);
sort(c2, 0, n);
for (int i = 0; i < n; ++i)
{
int lower = -1, upper = n;
while (upper - lower > 1)
{
int mid = lower + (upper - lower) / 2;
if (c2[mid] <= c[i])
lower = mid;
else
upper = mid;
}
c[i] = lower;
}
static int ee[N][2];
for (int i = 1, u, v; i < n; ++i)
{
scanf("%d%d", &u, &v);
--u, --v;
ee[i][0] = u;
ee[i][1] = v;
push(eh, eo, u, v);
push(eh, eo, v, u);
}
dfs1(0, -1);
hld[0] = 0;
dfs2(0, 0);
for (int i = 1, u, v; i < n; ++i)
{
u = ee[i][0], v = ee[i][1];
vo = 0;
/* collect all values into vector to find inversions */
for (int ii = u; ii != -1; ii = par[hld[ii]])
{
int take_here = tin[ii] - tin[hld[ii]] + 1, start = vo;
for (int taken = 0, jj = eo_[hld[ii]] - 1; jj >= 0; jj -= 2)
{
int value = eh_[hld[ii]][jj-1], freq = eh_[hld[ii]][jj];
if (freq + taken >= take_here)
freq = take_here - taken, jj = -1;
if (freq)
vv[vo++] = value, vv[vo++] = freq;
taken += freq;
}
reverse(vv, start, vo);
}
cost = 0;
inversions();
printf("%lld\n", cost);
/* change liveliness of all node on 1-u path to liveliness[v] */
for (int ii = u; ii != -1; ii = par[hld[ii]])
{
int take_here = tin[ii] - tin[hld[ii]] + 1;
for (int taken = 0, jj = eo_[hld[ii]] - 1; jj >= 0; jj -= 2, eo_[hld[ii]] -= 2)
{
int freq = eh_[hld[ii]][jj];
if (freq + taken >= take_here)
{
freq = take_here - taken;
eh_[hld[ii]][jj] -= freq;
if (eh_[hld[ii]][jj] == 0)
eo_[hld[ii]] -= 2;
break;
}
taken += freq;
}
pushchain(hld[ii], c[v], take_here);
}
}
}
/* O(n lg^2 n)
* proof like lct */
Compilation message
construction.c: In function 'push':
construction.c:63:19: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
63 | else if (!(o&o-1)) eh[i] = (int*)realloc(eh[i], 2*sizeof**eh*o);
| ~^~
construction.c: In function 'main':
construction.c:103:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
construction.c:105:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | scanf("%d", c+i);
| ^~~~~~~~~~~~~~~~
construction.c:126:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
126 | scanf("%d%d", &u, &v);
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4512 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4440 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
2 ms |
4700 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
1 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
1 ms |
4444 KB |
Output is correct |
44 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4512 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4440 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
2 ms |
4700 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
1 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
1 ms |
4444 KB |
Output is correct |
44 |
Correct |
1 ms |
4444 KB |
Output is correct |
45 |
Correct |
2 ms |
4444 KB |
Output is correct |
46 |
Correct |
4 ms |
4700 KB |
Output is correct |
47 |
Correct |
4 ms |
4700 KB |
Output is correct |
48 |
Correct |
4 ms |
4956 KB |
Output is correct |
49 |
Correct |
3 ms |
4956 KB |
Output is correct |
50 |
Correct |
3 ms |
4956 KB |
Output is correct |
51 |
Correct |
4 ms |
4956 KB |
Output is correct |
52 |
Correct |
5 ms |
5104 KB |
Output is correct |
53 |
Correct |
3 ms |
4956 KB |
Output is correct |
54 |
Correct |
3 ms |
5016 KB |
Output is correct |
55 |
Correct |
3 ms |
4956 KB |
Output is correct |
56 |
Correct |
4 ms |
4956 KB |
Output is correct |
57 |
Correct |
5 ms |
4956 KB |
Output is correct |
58 |
Correct |
5 ms |
4956 KB |
Output is correct |
59 |
Correct |
5 ms |
4956 KB |
Output is correct |
60 |
Correct |
5 ms |
4956 KB |
Output is correct |
61 |
Correct |
3 ms |
4956 KB |
Output is correct |
62 |
Correct |
3 ms |
5048 KB |
Output is correct |
63 |
Correct |
5 ms |
4956 KB |
Output is correct |
64 |
Correct |
3 ms |
4700 KB |
Output is correct |
65 |
Correct |
4 ms |
4700 KB |
Output is correct |
66 |
Correct |
4 ms |
4956 KB |
Output is correct |
67 |
Correct |
4 ms |
4696 KB |
Output is correct |
68 |
Correct |
3 ms |
4956 KB |
Output is correct |
69 |
Correct |
3 ms |
5072 KB |
Output is correct |
70 |
Correct |
3 ms |
4968 KB |
Output is correct |
71 |
Correct |
3 ms |
4960 KB |
Output is correct |
72 |
Correct |
5 ms |
4960 KB |
Output is correct |
73 |
Correct |
5 ms |
4968 KB |
Output is correct |
74 |
Correct |
3 ms |
4968 KB |
Output is correct |
75 |
Correct |
3 ms |
4968 KB |
Output is correct |
76 |
Correct |
3 ms |
4996 KB |
Output is correct |
77 |
Correct |
3 ms |
4956 KB |
Output is correct |
78 |
Correct |
3 ms |
4956 KB |
Output is correct |
79 |
Correct |
3 ms |
4956 KB |
Output is correct |
80 |
Correct |
3 ms |
4956 KB |
Output is correct |
81 |
Correct |
3 ms |
4956 KB |
Output is correct |
82 |
Correct |
3 ms |
4996 KB |
Output is correct |
83 |
Correct |
3 ms |
4788 KB |
Output is correct |
84 |
Correct |
3 ms |
4956 KB |
Output is correct |
85 |
Correct |
5 ms |
4956 KB |
Output is correct |
86 |
Correct |
3 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
2 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
2 ms |
4444 KB |
Output is correct |
22 |
Correct |
2 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
2 ms |
4512 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4440 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
2 ms |
4700 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
4444 KB |
Output is correct |
41 |
Correct |
1 ms |
4444 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
1 ms |
4444 KB |
Output is correct |
44 |
Correct |
1 ms |
4444 KB |
Output is correct |
45 |
Correct |
2 ms |
4444 KB |
Output is correct |
46 |
Correct |
4 ms |
4700 KB |
Output is correct |
47 |
Correct |
4 ms |
4700 KB |
Output is correct |
48 |
Correct |
4 ms |
4956 KB |
Output is correct |
49 |
Correct |
3 ms |
4956 KB |
Output is correct |
50 |
Correct |
3 ms |
4956 KB |
Output is correct |
51 |
Correct |
4 ms |
4956 KB |
Output is correct |
52 |
Correct |
5 ms |
5104 KB |
Output is correct |
53 |
Correct |
3 ms |
4956 KB |
Output is correct |
54 |
Correct |
3 ms |
5016 KB |
Output is correct |
55 |
Correct |
3 ms |
4956 KB |
Output is correct |
56 |
Correct |
4 ms |
4956 KB |
Output is correct |
57 |
Correct |
5 ms |
4956 KB |
Output is correct |
58 |
Correct |
5 ms |
4956 KB |
Output is correct |
59 |
Correct |
5 ms |
4956 KB |
Output is correct |
60 |
Correct |
5 ms |
4956 KB |
Output is correct |
61 |
Correct |
3 ms |
4956 KB |
Output is correct |
62 |
Correct |
3 ms |
5048 KB |
Output is correct |
63 |
Correct |
5 ms |
4956 KB |
Output is correct |
64 |
Correct |
3 ms |
4700 KB |
Output is correct |
65 |
Correct |
4 ms |
4700 KB |
Output is correct |
66 |
Correct |
4 ms |
4956 KB |
Output is correct |
67 |
Correct |
4 ms |
4696 KB |
Output is correct |
68 |
Correct |
3 ms |
4956 KB |
Output is correct |
69 |
Correct |
3 ms |
5072 KB |
Output is correct |
70 |
Correct |
3 ms |
4968 KB |
Output is correct |
71 |
Correct |
3 ms |
4960 KB |
Output is correct |
72 |
Correct |
5 ms |
4960 KB |
Output is correct |
73 |
Correct |
5 ms |
4968 KB |
Output is correct |
74 |
Correct |
3 ms |
4968 KB |
Output is correct |
75 |
Correct |
3 ms |
4968 KB |
Output is correct |
76 |
Correct |
3 ms |
4996 KB |
Output is correct |
77 |
Correct |
3 ms |
4956 KB |
Output is correct |
78 |
Correct |
3 ms |
4956 KB |
Output is correct |
79 |
Correct |
3 ms |
4956 KB |
Output is correct |
80 |
Correct |
3 ms |
4956 KB |
Output is correct |
81 |
Correct |
3 ms |
4956 KB |
Output is correct |
82 |
Correct |
3 ms |
4996 KB |
Output is correct |
83 |
Correct |
3 ms |
4788 KB |
Output is correct |
84 |
Correct |
3 ms |
4956 KB |
Output is correct |
85 |
Correct |
5 ms |
4956 KB |
Output is correct |
86 |
Correct |
3 ms |
4956 KB |
Output is correct |
87 |
Correct |
11 ms |
5464 KB |
Output is correct |
88 |
Correct |
35 ms |
7396 KB |
Output is correct |
89 |
Correct |
133 ms |
13908 KB |
Output is correct |
90 |
Correct |
132 ms |
13876 KB |
Output is correct |
91 |
Correct |
140 ms |
13944 KB |
Output is correct |
92 |
Correct |
75 ms |
19280 KB |
Output is correct |
93 |
Correct |
78 ms |
19244 KB |
Output is correct |
94 |
Correct |
70 ms |
19244 KB |
Output is correct |
95 |
Correct |
71 ms |
17872 KB |
Output is correct |
96 |
Correct |
75 ms |
18000 KB |
Output is correct |
97 |
Correct |
76 ms |
17868 KB |
Output is correct |
98 |
Correct |
76 ms |
18168 KB |
Output is correct |
99 |
Correct |
78 ms |
17492 KB |
Output is correct |
100 |
Correct |
151 ms |
13344 KB |
Output is correct |
101 |
Correct |
193 ms |
13364 KB |
Output is correct |
102 |
Correct |
201 ms |
13336 KB |
Output is correct |
103 |
Correct |
203 ms |
13920 KB |
Output is correct |
104 |
Correct |
81 ms |
17456 KB |
Output is correct |
105 |
Correct |
79 ms |
17532 KB |
Output is correct |
106 |
Correct |
79 ms |
17488 KB |
Output is correct |
107 |
Correct |
99 ms |
13500 KB |
Output is correct |
108 |
Correct |
120 ms |
13952 KB |
Output is correct |
109 |
Correct |
129 ms |
13908 KB |
Output is correct |
110 |
Correct |
62 ms |
19284 KB |
Output is correct |
111 |
Correct |
74 ms |
17836 KB |
Output is correct |
112 |
Correct |
65 ms |
17920 KB |
Output is correct |
113 |
Correct |
66 ms |
17484 KB |
Output is correct |
114 |
Correct |
144 ms |
13340 KB |
Output is correct |
115 |
Correct |
202 ms |
13636 KB |
Output is correct |
116 |
Correct |
68 ms |
17488 KB |
Output is correct |
117 |
Correct |
74 ms |
15448 KB |
Output is correct |
118 |
Correct |
75 ms |
14856 KB |
Output is correct |
119 |
Correct |
73 ms |
14576 KB |
Output is correct |
120 |
Correct |
66 ms |
15440 KB |
Output is correct |
121 |
Correct |
66 ms |
14832 KB |
Output is correct |
122 |
Correct |
66 ms |
14552 KB |
Output is correct |
123 |
Correct |
100 ms |
15520 KB |
Output is correct |
124 |
Correct |
84 ms |
14804 KB |
Output is correct |
125 |
Correct |
78 ms |
14548 KB |
Output is correct |
126 |
Correct |
68 ms |
15448 KB |
Output is correct |
127 |
Correct |
76 ms |
14884 KB |
Output is correct |
128 |
Correct |
70 ms |
14416 KB |
Output is correct |