#pragma GCC optimize("O3,unroll-loops")
#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:64:19: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
64 | else if (!(o&o-1)) eh[i] = (int*)realloc(eh[i], 2*sizeof**eh*o);
| ~^~
construction.c: In function 'main':
construction.c:104:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
construction.c:106:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | scanf("%d", c+i);
| ^~~~~~~~~~~~~~~~
construction.c:127:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d%d", &u, &v);
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 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 |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4528 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 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 |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4696 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 |
1 ms |
4440 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 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 |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 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 |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4528 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 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 |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4696 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 |
1 ms |
4440 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 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 |
6 ms |
4956 KB |
Output is correct |
47 |
Correct |
4 ms |
4700 KB |
Output is correct |
48 |
Correct |
4 ms |
4700 KB |
Output is correct |
49 |
Correct |
3 ms |
4956 KB |
Output is correct |
50 |
Correct |
3 ms |
5208 KB |
Output is correct |
51 |
Correct |
3 ms |
5208 KB |
Output is correct |
52 |
Correct |
3 ms |
4956 KB |
Output is correct |
53 |
Correct |
3 ms |
4956 KB |
Output is correct |
54 |
Correct |
3 ms |
4956 KB |
Output is correct |
55 |
Correct |
3 ms |
4956 KB |
Output is correct |
56 |
Correct |
3 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 |
4912 KB |
Output is correct |
61 |
Correct |
3 ms |
4956 KB |
Output is correct |
62 |
Correct |
4 ms |
4956 KB |
Output is correct |
63 |
Correct |
3 ms |
4956 KB |
Output is correct |
64 |
Correct |
4 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 |
4700 KB |
Output is correct |
68 |
Correct |
3 ms |
4956 KB |
Output is correct |
69 |
Correct |
3 ms |
4956 KB |
Output is correct |
70 |
Correct |
3 ms |
4956 KB |
Output is correct |
71 |
Correct |
3 ms |
4952 KB |
Output is correct |
72 |
Correct |
5 ms |
4744 KB |
Output is correct |
73 |
Correct |
5 ms |
4956 KB |
Output is correct |
74 |
Correct |
3 ms |
4956 KB |
Output is correct |
75 |
Correct |
4 ms |
4952 KB |
Output is correct |
76 |
Correct |
3 ms |
4956 KB |
Output is correct |
77 |
Correct |
3 ms |
4956 KB |
Output is correct |
78 |
Correct |
3 ms |
4952 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 |
4956 KB |
Output is correct |
83 |
Correct |
3 ms |
4788 KB |
Output is correct |
84 |
Correct |
3 ms |
4956 KB |
Output is correct |
85 |
Correct |
3 ms |
4956 KB |
Output is correct |
86 |
Correct |
4 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 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 |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4528 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 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 |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4696 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 |
1 ms |
4440 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 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 |
6 ms |
4956 KB |
Output is correct |
47 |
Correct |
4 ms |
4700 KB |
Output is correct |
48 |
Correct |
4 ms |
4700 KB |
Output is correct |
49 |
Correct |
3 ms |
4956 KB |
Output is correct |
50 |
Correct |
3 ms |
5208 KB |
Output is correct |
51 |
Correct |
3 ms |
5208 KB |
Output is correct |
52 |
Correct |
3 ms |
4956 KB |
Output is correct |
53 |
Correct |
3 ms |
4956 KB |
Output is correct |
54 |
Correct |
3 ms |
4956 KB |
Output is correct |
55 |
Correct |
3 ms |
4956 KB |
Output is correct |
56 |
Correct |
3 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 |
4912 KB |
Output is correct |
61 |
Correct |
3 ms |
4956 KB |
Output is correct |
62 |
Correct |
4 ms |
4956 KB |
Output is correct |
63 |
Correct |
3 ms |
4956 KB |
Output is correct |
64 |
Correct |
4 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 |
4700 KB |
Output is correct |
68 |
Correct |
3 ms |
4956 KB |
Output is correct |
69 |
Correct |
3 ms |
4956 KB |
Output is correct |
70 |
Correct |
3 ms |
4956 KB |
Output is correct |
71 |
Correct |
3 ms |
4952 KB |
Output is correct |
72 |
Correct |
5 ms |
4744 KB |
Output is correct |
73 |
Correct |
5 ms |
4956 KB |
Output is correct |
74 |
Correct |
3 ms |
4956 KB |
Output is correct |
75 |
Correct |
4 ms |
4952 KB |
Output is correct |
76 |
Correct |
3 ms |
4956 KB |
Output is correct |
77 |
Correct |
3 ms |
4956 KB |
Output is correct |
78 |
Correct |
3 ms |
4952 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 |
4956 KB |
Output is correct |
83 |
Correct |
3 ms |
4788 KB |
Output is correct |
84 |
Correct |
3 ms |
4956 KB |
Output is correct |
85 |
Correct |
3 ms |
4956 KB |
Output is correct |
86 |
Correct |
4 ms |
4956 KB |
Output is correct |
87 |
Correct |
12 ms |
5468 KB |
Output is correct |
88 |
Correct |
40 ms |
7412 KB |
Output is correct |
89 |
Correct |
129 ms |
13828 KB |
Output is correct |
90 |
Correct |
130 ms |
14016 KB |
Output is correct |
91 |
Correct |
125 ms |
14156 KB |
Output is correct |
92 |
Correct |
74 ms |
20784 KB |
Output is correct |
93 |
Correct |
70 ms |
20816 KB |
Output is correct |
94 |
Correct |
80 ms |
20944 KB |
Output is correct |
95 |
Correct |
70 ms |
18640 KB |
Output is correct |
96 |
Correct |
73 ms |
18844 KB |
Output is correct |
97 |
Correct |
74 ms |
18596 KB |
Output is correct |
98 |
Correct |
76 ms |
18852 KB |
Output is correct |
99 |
Correct |
78 ms |
18580 KB |
Output is correct |
100 |
Correct |
144 ms |
13392 KB |
Output is correct |
101 |
Correct |
189 ms |
13484 KB |
Output is correct |
102 |
Correct |
195 ms |
14180 KB |
Output is correct |
103 |
Correct |
185 ms |
13312 KB |
Output is correct |
104 |
Correct |
75 ms |
18260 KB |
Output is correct |
105 |
Correct |
86 ms |
18476 KB |
Output is correct |
106 |
Correct |
77 ms |
18528 KB |
Output is correct |
107 |
Correct |
93 ms |
13652 KB |
Output is correct |
108 |
Correct |
114 ms |
13872 KB |
Output is correct |
109 |
Correct |
123 ms |
13908 KB |
Output is correct |
110 |
Correct |
62 ms |
20780 KB |
Output is correct |
111 |
Correct |
70 ms |
18636 KB |
Output is correct |
112 |
Correct |
64 ms |
18636 KB |
Output is correct |
113 |
Correct |
66 ms |
18344 KB |
Output is correct |
114 |
Correct |
144 ms |
13360 KB |
Output is correct |
115 |
Correct |
187 ms |
13288 KB |
Output is correct |
116 |
Correct |
66 ms |
18260 KB |
Output is correct |
117 |
Correct |
74 ms |
16176 KB |
Output is correct |
118 |
Correct |
74 ms |
15072 KB |
Output is correct |
119 |
Correct |
78 ms |
14840 KB |
Output is correct |
120 |
Correct |
65 ms |
15960 KB |
Output is correct |
121 |
Correct |
65 ms |
15152 KB |
Output is correct |
122 |
Correct |
64 ms |
14688 KB |
Output is correct |
123 |
Correct |
85 ms |
15852 KB |
Output is correct |
124 |
Correct |
81 ms |
15056 KB |
Output is correct |
125 |
Correct |
79 ms |
14668 KB |
Output is correct |
126 |
Correct |
71 ms |
15956 KB |
Output is correct |
127 |
Correct |
70 ms |
15080 KB |
Output is correct |
128 |
Correct |
83 ms |
14604 KB |
Output is correct |