Submission #967152

# Submission time Handle Problem Language Result Execution time Memory
967152 2024-04-21T11:01:50 Z sleepntsheep Construction of Highway (JOI18_construction) C
100 / 100
205 ms 21332 KB
#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];

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 = vo - 1; j >= 0; j -= 2)
    {
        int value = vv[j-1], freq = vv[j];
        fwadd(value, freq);
        cost += freq * fwsum(value);
    }
    for (int j = vo - 1; j >= 0; 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)
    {
        v = eh[u][j];
        if (v == p)
            continue;
        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)
    {
        v = eh[u][j];
        if (v == p)
            continue;
        hld[v] = (j == 0) ? hld[u] : v;
        dfs2(v, u);
    }

    //printf(" Pushing %d to %d\n",c[u],h);
    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);

    {
        static int c2[N];
        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);

    //printf(" chain[0] status : \n"); for(int j=0;j<eo_[0];j+=2) printf(" [%d %d]",eh_[0][j],eh_[0][j+1]);puts("");
    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 */

        //printf("collecting\n");
        for (int ii = u; ii != -1; ii = par[hld[ii]])
        {
            int take_here = tin[ii] - tin[hld[ii]] + 1, start = vo;
            //printf(" taking %d from %d\n",take_here,hld[ii]);
            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);
        }
        reverse(vv, 0, vo);

        //for (int ii=0;ii<vo;++++ii) printf(" (%d %d)\n", vv[ii],vv[ii+1]);
        cost = 0;
        inversions();
        printf("%lld\n", cost);

        /* change liveliness of all node on 1-u path to liveliness[v] */

        //printf(" upding with %d\n",c[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);
        }

        //printf(" chain[0] status : \n"); for(int j=0;j<eo_[0];j+=2) printf(" [%d %d]",eh_[0][j],eh_[0][j+1]);puts("");puts("========="); 
    }
}

/* O(n lg^2 n)
 * proof like lct */

Compilation message

construction.c: In function 'push':
construction.c:66:19: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   66 |     else if (!(o&o-1)) eh[i] = (int*)realloc(eh[i], 2*sizeof**eh*o);
      |                  ~^~
construction.c: In function 'main':
construction.c:118:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  118 |     scanf("%d", &n);
      |     ^~~~~~~~~~~~~~~
construction.c:120:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         scanf("%d", c+i);
      |         ^~~~~~~~~~~~~~~~
construction.c:144:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  144 |         scanf("%d%d", &u, &v);
      |         ^~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 ms 4440 KB Output is correct
4 Correct 1 ms 4696 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4532 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 3 ms 4560 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 1 ms 4528 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4592 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4696 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 4444 KB Output is correct
23 Correct 1 ms 4528 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 2 ms 4528 KB Output is correct
27 Correct 1 ms 4440 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 2 ms 4444 KB Output is correct
31 Correct 2 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 4700 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 4440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 ms 4440 KB Output is correct
4 Correct 1 ms 4696 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4532 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 3 ms 4560 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 1 ms 4528 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4592 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4696 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 4444 KB Output is correct
23 Correct 1 ms 4528 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 2 ms 4528 KB Output is correct
27 Correct 1 ms 4440 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 2 ms 4444 KB Output is correct
31 Correct 2 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 4700 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 4440 KB Output is correct
45 Correct 2 ms 4444 KB Output is correct
46 Correct 5 ms 4872 KB Output is correct
47 Correct 6 ms 4956 KB Output is correct
48 Correct 5 ms 4956 KB Output is correct
49 Correct 3 ms 5208 KB Output is correct
50 Correct 3 ms 5044 KB Output is correct
51 Correct 3 ms 5212 KB Output is correct
52 Correct 3 ms 5212 KB Output is correct
53 Correct 3 ms 4956 KB Output is correct
54 Correct 4 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 6 ms 4892 KB Output is correct
60 Correct 6 ms 4960 KB Output is correct
61 Correct 3 ms 4960 KB Output is correct
62 Correct 3 ms 4968 KB Output is correct
63 Correct 3 ms 4968 KB Output is correct
64 Correct 4 ms 4968 KB Output is correct
65 Correct 4 ms 4960 KB Output is correct
66 Correct 4 ms 4964 KB Output is correct
67 Correct 4 ms 4968 KB Output is correct
68 Correct 3 ms 4968 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 4956 KB Output is correct
72 Correct 5 ms 4956 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 4856 KB Output is correct
76 Correct 3 ms 4972 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 4952 KB Output is correct
80 Correct 3 ms 4956 KB Output is correct
81 Correct 3 ms 5072 KB Output is correct
82 Correct 3 ms 4984 KB Output is correct
83 Correct 3 ms 4952 KB Output is correct
84 Correct 3 ms 4956 KB Output is correct
85 Correct 3 ms 4956 KB Output is correct
86 Correct 3 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 2 ms 4440 KB Output is correct
4 Correct 1 ms 4696 KB Output is correct
5 Correct 2 ms 4444 KB Output is correct
6 Correct 1 ms 4444 KB Output is correct
7 Correct 2 ms 4532 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 3 ms 4560 KB Output is correct
12 Correct 1 ms 4440 KB Output is correct
13 Correct 2 ms 4444 KB Output is correct
14 Correct 1 ms 4528 KB Output is correct
15 Correct 2 ms 4444 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4592 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4696 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 4444 KB Output is correct
23 Correct 1 ms 4528 KB Output is correct
24 Correct 1 ms 4444 KB Output is correct
25 Correct 1 ms 4444 KB Output is correct
26 Correct 2 ms 4528 KB Output is correct
27 Correct 1 ms 4440 KB Output is correct
28 Correct 1 ms 4444 KB Output is correct
29 Correct 1 ms 4444 KB Output is correct
30 Correct 2 ms 4444 KB Output is correct
31 Correct 2 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 4700 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 4440 KB Output is correct
45 Correct 2 ms 4444 KB Output is correct
46 Correct 5 ms 4872 KB Output is correct
47 Correct 6 ms 4956 KB Output is correct
48 Correct 5 ms 4956 KB Output is correct
49 Correct 3 ms 5208 KB Output is correct
50 Correct 3 ms 5044 KB Output is correct
51 Correct 3 ms 5212 KB Output is correct
52 Correct 3 ms 5212 KB Output is correct
53 Correct 3 ms 4956 KB Output is correct
54 Correct 4 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 6 ms 4892 KB Output is correct
60 Correct 6 ms 4960 KB Output is correct
61 Correct 3 ms 4960 KB Output is correct
62 Correct 3 ms 4968 KB Output is correct
63 Correct 3 ms 4968 KB Output is correct
64 Correct 4 ms 4968 KB Output is correct
65 Correct 4 ms 4960 KB Output is correct
66 Correct 4 ms 4964 KB Output is correct
67 Correct 4 ms 4968 KB Output is correct
68 Correct 3 ms 4968 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 4956 KB Output is correct
72 Correct 5 ms 4956 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 4856 KB Output is correct
76 Correct 3 ms 4972 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 4952 KB Output is correct
80 Correct 3 ms 4956 KB Output is correct
81 Correct 3 ms 5072 KB Output is correct
82 Correct 3 ms 4984 KB Output is correct
83 Correct 3 ms 4952 KB Output is correct
84 Correct 3 ms 4956 KB Output is correct
85 Correct 3 ms 4956 KB Output is correct
86 Correct 3 ms 4956 KB Output is correct
87 Correct 11 ms 5468 KB Output is correct
88 Correct 33 ms 7976 KB Output is correct
89 Correct 133 ms 16136 KB Output is correct
90 Correct 132 ms 15952 KB Output is correct
91 Correct 149 ms 16212 KB Output is correct
92 Correct 73 ms 21328 KB Output is correct
93 Correct 77 ms 21272 KB Output is correct
94 Correct 75 ms 21332 KB Output is correct
95 Correct 72 ms 19664 KB Output is correct
96 Correct 79 ms 20176 KB Output is correct
97 Correct 78 ms 19920 KB Output is correct
98 Correct 77 ms 20120 KB Output is correct
99 Correct 78 ms 19568 KB Output is correct
100 Correct 161 ms 15236 KB Output is correct
101 Correct 204 ms 15572 KB Output is correct
102 Correct 192 ms 15580 KB Output is correct
103 Correct 205 ms 15568 KB Output is correct
104 Correct 81 ms 19540 KB Output is correct
105 Correct 79 ms 19592 KB Output is correct
106 Correct 78 ms 19580 KB Output is correct
107 Correct 91 ms 15004 KB Output is correct
108 Correct 120 ms 15388 KB Output is correct
109 Correct 132 ms 15696 KB Output is correct
110 Correct 65 ms 20816 KB Output is correct
111 Correct 72 ms 19664 KB Output is correct
112 Correct 67 ms 19404 KB Output is correct
113 Correct 67 ms 18964 KB Output is correct
114 Correct 151 ms 15076 KB Output is correct
115 Correct 186 ms 14960 KB Output is correct
116 Correct 70 ms 19024 KB Output is correct
117 Correct 74 ms 17252 KB Output is correct
118 Correct 77 ms 16680 KB Output is correct
119 Correct 82 ms 16312 KB Output is correct
120 Correct 67 ms 16976 KB Output is correct
121 Correct 68 ms 16336 KB Output is correct
122 Correct 69 ms 16044 KB Output is correct
123 Correct 78 ms 17436 KB Output is correct
124 Correct 79 ms 16724 KB Output is correct
125 Correct 82 ms 16248 KB Output is correct
126 Correct 71 ms 16976 KB Output is correct
127 Correct 72 ms 16388 KB Output is correct
128 Correct 80 ms 16188 KB Output is correct