Submission #823337

# Submission time Handle Problem Language Result Execution time Memory
823337 2023-08-12T10:52:21 Z LucaIlie Construction of Highway (JOI18_construction) C++17
100 / 100
1926 ms 60384 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")

using namespace std;

struct vr {
    int val, rep;
};

const int UNDEF = 0;
const int INF = 1e9;
const int MAX_N = 1e5 + 5;
const int MAX_LOG_N = 17;
int c[MAX_N], parent[MAX_LOG_N + 1][MAX_N], ord[MAX_N];
vector<int> edges[MAX_N];
vr elem[MAX_N];

struct info {
    int minn, maxx;

    info operator + ( info &x ) const {
        return { min( minn, x.minn ), max( maxx, x.maxx ) };
    }
};

struct SegTree {
    info segTree[4 * MAX_N];
    int lazy[4 * MAX_N];

    void init() {
        for ( int v = 0; v < 4 * MAX_N; v++ ) {
            segTree[v] = { INF, -INF };
            lazy[v] = UNDEF;
        }
    }

    void propag( int v, int l, int r ) {
        if ( lazy[v] == UNDEF )
            return;

        segTree[v] = { lazy[v], lazy[v] };
        if ( l != r )
            lazy[v * 2 + 1] = lazy[v * 2 + 2] = lazy[v];
        lazy[v] = UNDEF;
    }

    void update( int v, int l, int r, int lu, int ru, int x ) {
        propag( v, l, r );

        if ( l > ru || r < lu )
            return;

        if ( lu <= l && r <= ru ) {
            lazy[v] = x;
            propag( v, l, r );
            return;
        }

        int mid = (l + r) / 2;
        update( v * 2 + 1, l, mid, lu, ru, x );
        update( v * 2 + 2, mid + 1, r, lu, ru, x );
        segTree[v] = segTree[v * 2 + 1] + segTree[v * 2 + 2];
    }

    info query( int v, int l, int r, int lq, int rq ) {
        propag( v, l, r );

        if ( l > rq || r < lq )
            return { INF, -INF };

        if ( lq <= l && r <= rq )
            return segTree[v];

        int mid = (l + r) / 2;
        info a = query( v * 2 + 1, l, mid, lq, rq );
        info b = query( v * 2 + 2, mid + 1, r, lq, rq );
        return a + b;
    }
};

struct HPD {
    int n, curPos;
    int sz[MAX_N], depth[MAX_N], heavy[MAX_N], head[MAX_N], leftPos[MAX_N], rightPos[MAX_N];
    SegTree aint;

    void dfs( int u, int p ) {
        int maxSz = -1;

        parent[0][u] = p;
        depth[u] = depth[p] + 1;
        sz[u] = 1;
        heavy[u] = UNDEF;
        for ( int v: edges[u] ) {
            if ( v == p )
                continue;
            dfs( v, u );
            sz[u] += sz[v];
            if ( sz[v] > maxSz ) {
                maxSz = sz[v];
                heavy[u] = v;
            }
        }
    }

    void decomp( int u, int p, int h ) {
        head[u] = h;
        leftPos[u] = ++curPos;
        if ( heavy[u] != UNDEF )
            decomp( heavy[u], u, h );
        for ( int v: edges[u] ) {
            if ( v == p || v == heavy[u] )
                continue;
            decomp( v, u, v );
        }
        rightPos[u] = curPos;
    }

    void init( int _n ) {
        n = _n;
        aint.init();
        parent[0][1] = UNDEF;
        dfs( 1, UNDEF );
        decomp( 1, UNDEF, 1 );
    }


    void updatePath( int u, int x ) {
        while ( u != 0 ) {
            aint.update( 0, 1, n, leftPos[head[u]], leftPos[u], x );
            u = parent[0][head[u]];
        }
    }

    info queryPath( int u, int v ) {
       info ans = { INF, -INF };

        while ( head[u] != head[v] ) {
            if ( depth[head[u]] < depth[head[v]] )
                swap( u, v );
            info a = aint.query( 0, 1, n, leftPos[head[u]], leftPos[u] );
            ans = ans + a;
            u = parent[0][head[u]];
            if ( ans.minn != ans.maxx )
                return ans;

        }
        if ( depth[u] > depth[v] )
            swap( u, v );
        info a = aint.query( 0, 1, n, leftPos[u], leftPos[v] );
        ans = ans + a;
        return ans;
    }
};

struct AIB {
    int aib[MAX_N + 1];

    void update( int i, int x ) {
        while ( i <= MAX_N ) {
            aib[i] += x;
            i += (i & -i);
        }
    }

    int query( int i ) {
        int sum = 0;
        while ( i > 0 ) {
            sum += aib[i];
            i -= (i & -i);
        }
        return sum;
    }
};

AIB aib;
HPD arb;
map<int, int> normal;

int main() {
    ios_base::sync_with_stdio( false );
    cin.tie( 0 );
    cout.tie( 0 );
    int n;

    cin >> n;
    for ( int u = 1; u <= n; u++ )
        cin >> c[u], normal[c[u]] = 1;
    int val = 0;
    for ( auto x: normal )
        normal[x.first] = ++val;
    for ( int u = 1; u <= n; u++ )
        c[u] = normal[c[u]];

    for ( int i = 0; i < n - 1; i++ ) {
        int u, v;
        cin >> u >> v;
        parent[0][v] = u;
        edges[u].push_back( v );
        ord[i] = v;
    }

    for ( int pas = 1; pas <= MAX_LOG_N; pas++ ) {
        for ( int u = 1; u <= n; u++ )
            parent[pas][u] = parent[pas - 1][parent[pas - 1][u]];
    }

    arb.init( n );
    arb.updatePath( 1, c[1] );
    int sumSize = 0;
    for ( int i = 0; i < n - 1; i++ ) {
        int u = ord[i];
        int elemSize = 0;

        int v = parent[0][u];
        while( v != UNDEF ) {
            int c = arb.queryPath( v, v ).minn;
            int nr = 1;
            for ( int pas = MAX_LOG_N; pas >= 0; pas-- ) {
                if ( parent[pas][v] != UNDEF && arb.queryPath( v, parent[pas][v] ).maxx == c && arb.queryPath( v, parent[pas][v] ).minn == c ) {
                    v = parent[pas][v];
                    nr += (1 << pas);
                }
            }
            elem[elemSize++] = { c, nr };
            v = parent[0][v];
        }
        sumSize += elemSize;

        long long ans = 0;
        for ( int i = 0; i < elemSize; i++ ) {
            vr x = elem[i];
            ans += (long long)x.rep * aib.query( x.val -1 );
            aib.update( x.val, x.rep );
        }
        for ( int i = 0; i < elemSize; i++ ) {
            vr x = elem[i];
            aib.update( x.val, -x.rep );
        }
        cout << ans << "\n";

        arb.updatePath( u, c[u] );
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7508 KB Output is correct
2 Correct 3 ms 7508 KB Output is correct
3 Correct 3 ms 7508 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7560 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 4 ms 7764 KB Output is correct
11 Correct 4 ms 7636 KB Output is correct
12 Correct 4 ms 7636 KB Output is correct
13 Correct 4 ms 7636 KB Output is correct
14 Correct 4 ms 7636 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 5 ms 7508 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 6 ms 7508 KB Output is correct
19 Correct 4 ms 7636 KB Output is correct
20 Correct 4 ms 7664 KB Output is correct
21 Correct 4 ms 7636 KB Output is correct
22 Correct 4 ms 7508 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7508 KB Output is correct
26 Correct 4 ms 7764 KB Output is correct
27 Correct 4 ms 7636 KB Output is correct
28 Correct 4 ms 7636 KB Output is correct
29 Correct 5 ms 7636 KB Output is correct
30 Correct 5 ms 7508 KB Output is correct
31 Correct 5 ms 7508 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 4 ms 7636 KB Output is correct
34 Correct 4 ms 7636 KB Output is correct
35 Correct 4 ms 7508 KB Output is correct
36 Correct 4 ms 7508 KB Output is correct
37 Correct 4 ms 7508 KB Output is correct
38 Correct 4 ms 7508 KB Output is correct
39 Correct 5 ms 7636 KB Output is correct
40 Correct 5 ms 7580 KB Output is correct
41 Correct 4 ms 7508 KB Output is correct
42 Correct 5 ms 7580 KB Output is correct
43 Correct 4 ms 7508 KB Output is correct
44 Correct 4 ms 7508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7508 KB Output is correct
2 Correct 3 ms 7508 KB Output is correct
3 Correct 3 ms 7508 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7560 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 4 ms 7764 KB Output is correct
11 Correct 4 ms 7636 KB Output is correct
12 Correct 4 ms 7636 KB Output is correct
13 Correct 4 ms 7636 KB Output is correct
14 Correct 4 ms 7636 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 5 ms 7508 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 6 ms 7508 KB Output is correct
19 Correct 4 ms 7636 KB Output is correct
20 Correct 4 ms 7664 KB Output is correct
21 Correct 4 ms 7636 KB Output is correct
22 Correct 4 ms 7508 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7508 KB Output is correct
26 Correct 4 ms 7764 KB Output is correct
27 Correct 4 ms 7636 KB Output is correct
28 Correct 4 ms 7636 KB Output is correct
29 Correct 5 ms 7636 KB Output is correct
30 Correct 5 ms 7508 KB Output is correct
31 Correct 5 ms 7508 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 4 ms 7636 KB Output is correct
34 Correct 4 ms 7636 KB Output is correct
35 Correct 4 ms 7508 KB Output is correct
36 Correct 4 ms 7508 KB Output is correct
37 Correct 4 ms 7508 KB Output is correct
38 Correct 4 ms 7508 KB Output is correct
39 Correct 5 ms 7636 KB Output is correct
40 Correct 5 ms 7580 KB Output is correct
41 Correct 4 ms 7508 KB Output is correct
42 Correct 5 ms 7580 KB Output is correct
43 Correct 4 ms 7508 KB Output is correct
44 Correct 4 ms 7508 KB Output is correct
45 Correct 8 ms 7636 KB Output is correct
46 Correct 21 ms 8192 KB Output is correct
47 Correct 23 ms 8148 KB Output is correct
48 Correct 21 ms 8192 KB Output is correct
49 Correct 13 ms 9556 KB Output is correct
50 Correct 14 ms 9600 KB Output is correct
51 Correct 13 ms 9556 KB Output is correct
52 Correct 16 ms 8788 KB Output is correct
53 Correct 20 ms 8788 KB Output is correct
54 Correct 16 ms 8884 KB Output is correct
55 Correct 15 ms 8876 KB Output is correct
56 Correct 18 ms 8876 KB Output is correct
57 Correct 32 ms 8148 KB Output is correct
58 Correct 37 ms 8196 KB Output is correct
59 Correct 36 ms 8196 KB Output is correct
60 Correct 37 ms 8192 KB Output is correct
61 Correct 20 ms 8880 KB Output is correct
62 Correct 19 ms 8788 KB Output is correct
63 Correct 19 ms 8788 KB Output is correct
64 Correct 16 ms 7900 KB Output is correct
65 Correct 19 ms 7892 KB Output is correct
66 Correct 21 ms 8028 KB Output is correct
67 Correct 20 ms 8088 KB Output is correct
68 Correct 12 ms 9364 KB Output is correct
69 Correct 15 ms 8872 KB Output is correct
70 Correct 14 ms 8660 KB Output is correct
71 Correct 14 ms 8680 KB Output is correct
72 Correct 28 ms 8200 KB Output is correct
73 Correct 37 ms 7996 KB Output is correct
74 Correct 20 ms 8660 KB Output is correct
75 Correct 16 ms 8532 KB Output is correct
76 Correct 16 ms 8404 KB Output is correct
77 Correct 16 ms 8364 KB Output is correct
78 Correct 15 ms 8276 KB Output is correct
79 Correct 15 ms 8228 KB Output is correct
80 Correct 14 ms 8172 KB Output is correct
81 Correct 18 ms 8536 KB Output is correct
82 Correct 18 ms 8416 KB Output is correct
83 Correct 17 ms 8360 KB Output is correct
84 Correct 19 ms 8340 KB Output is correct
85 Correct 16 ms 8148 KB Output is correct
86 Correct 16 ms 8176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 7508 KB Output is correct
2 Correct 3 ms 7508 KB Output is correct
3 Correct 3 ms 7508 KB Output is correct
4 Correct 4 ms 7508 KB Output is correct
5 Correct 5 ms 7560 KB Output is correct
6 Correct 4 ms 7508 KB Output is correct
7 Correct 4 ms 7508 KB Output is correct
8 Correct 4 ms 7764 KB Output is correct
9 Correct 4 ms 7764 KB Output is correct
10 Correct 4 ms 7764 KB Output is correct
11 Correct 4 ms 7636 KB Output is correct
12 Correct 4 ms 7636 KB Output is correct
13 Correct 4 ms 7636 KB Output is correct
14 Correct 4 ms 7636 KB Output is correct
15 Correct 5 ms 7508 KB Output is correct
16 Correct 5 ms 7508 KB Output is correct
17 Correct 5 ms 7508 KB Output is correct
18 Correct 6 ms 7508 KB Output is correct
19 Correct 4 ms 7636 KB Output is correct
20 Correct 4 ms 7664 KB Output is correct
21 Correct 4 ms 7636 KB Output is correct
22 Correct 4 ms 7508 KB Output is correct
23 Correct 4 ms 7508 KB Output is correct
24 Correct 4 ms 7508 KB Output is correct
25 Correct 4 ms 7508 KB Output is correct
26 Correct 4 ms 7764 KB Output is correct
27 Correct 4 ms 7636 KB Output is correct
28 Correct 4 ms 7636 KB Output is correct
29 Correct 5 ms 7636 KB Output is correct
30 Correct 5 ms 7508 KB Output is correct
31 Correct 5 ms 7508 KB Output is correct
32 Correct 5 ms 7636 KB Output is correct
33 Correct 4 ms 7636 KB Output is correct
34 Correct 4 ms 7636 KB Output is correct
35 Correct 4 ms 7508 KB Output is correct
36 Correct 4 ms 7508 KB Output is correct
37 Correct 4 ms 7508 KB Output is correct
38 Correct 4 ms 7508 KB Output is correct
39 Correct 5 ms 7636 KB Output is correct
40 Correct 5 ms 7580 KB Output is correct
41 Correct 4 ms 7508 KB Output is correct
42 Correct 5 ms 7580 KB Output is correct
43 Correct 4 ms 7508 KB Output is correct
44 Correct 4 ms 7508 KB Output is correct
45 Correct 8 ms 7636 KB Output is correct
46 Correct 21 ms 8192 KB Output is correct
47 Correct 23 ms 8148 KB Output is correct
48 Correct 21 ms 8192 KB Output is correct
49 Correct 13 ms 9556 KB Output is correct
50 Correct 14 ms 9600 KB Output is correct
51 Correct 13 ms 9556 KB Output is correct
52 Correct 16 ms 8788 KB Output is correct
53 Correct 20 ms 8788 KB Output is correct
54 Correct 16 ms 8884 KB Output is correct
55 Correct 15 ms 8876 KB Output is correct
56 Correct 18 ms 8876 KB Output is correct
57 Correct 32 ms 8148 KB Output is correct
58 Correct 37 ms 8196 KB Output is correct
59 Correct 36 ms 8196 KB Output is correct
60 Correct 37 ms 8192 KB Output is correct
61 Correct 20 ms 8880 KB Output is correct
62 Correct 19 ms 8788 KB Output is correct
63 Correct 19 ms 8788 KB Output is correct
64 Correct 16 ms 7900 KB Output is correct
65 Correct 19 ms 7892 KB Output is correct
66 Correct 21 ms 8028 KB Output is correct
67 Correct 20 ms 8088 KB Output is correct
68 Correct 12 ms 9364 KB Output is correct
69 Correct 15 ms 8872 KB Output is correct
70 Correct 14 ms 8660 KB Output is correct
71 Correct 14 ms 8680 KB Output is correct
72 Correct 28 ms 8200 KB Output is correct
73 Correct 37 ms 7996 KB Output is correct
74 Correct 20 ms 8660 KB Output is correct
75 Correct 16 ms 8532 KB Output is correct
76 Correct 16 ms 8404 KB Output is correct
77 Correct 16 ms 8364 KB Output is correct
78 Correct 15 ms 8276 KB Output is correct
79 Correct 15 ms 8228 KB Output is correct
80 Correct 14 ms 8172 KB Output is correct
81 Correct 18 ms 8536 KB Output is correct
82 Correct 18 ms 8416 KB Output is correct
83 Correct 17 ms 8360 KB Output is correct
84 Correct 19 ms 8340 KB Output is correct
85 Correct 16 ms 8148 KB Output is correct
86 Correct 16 ms 8176 KB Output is correct
87 Correct 65 ms 9308 KB Output is correct
88 Correct 199 ms 12560 KB Output is correct
89 Correct 945 ms 24556 KB Output is correct
90 Correct 944 ms 24516 KB Output is correct
91 Correct 1018 ms 24488 KB Output is correct
92 Correct 495 ms 60256 KB Output is correct
93 Correct 461 ms 60340 KB Output is correct
94 Correct 485 ms 60384 KB Output is correct
95 Correct 654 ms 42064 KB Output is correct
96 Correct 694 ms 41876 KB Output is correct
97 Correct 688 ms 41868 KB Output is correct
98 Correct 658 ms 41948 KB Output is correct
99 Correct 676 ms 41680 KB Output is correct
100 Correct 1519 ms 25020 KB Output is correct
101 Correct 1879 ms 24856 KB Output is correct
102 Correct 1866 ms 24916 KB Output is correct
103 Correct 1926 ms 24952 KB Output is correct
104 Correct 808 ms 41624 KB Output is correct
105 Correct 840 ms 41828 KB Output is correct
106 Correct 795 ms 41620 KB Output is correct
107 Correct 620 ms 19404 KB Output is correct
108 Correct 912 ms 19600 KB Output is correct
109 Correct 1020 ms 20908 KB Output is correct
110 Correct 413 ms 55432 KB Output is correct
111 Correct 576 ms 41972 KB Output is correct
112 Correct 539 ms 36908 KB Output is correct
113 Correct 597 ms 36832 KB Output is correct
114 Correct 1485 ms 25036 KB Output is correct
115 Correct 1833 ms 19896 KB Output is correct
116 Correct 764 ms 36576 KB Output is correct
117 Correct 630 ms 32820 KB Output is correct
118 Correct 633 ms 29908 KB Output is correct
119 Correct 632 ms 28488 KB Output is correct
120 Correct 636 ms 27924 KB Output is correct
121 Correct 604 ms 25092 KB Output is correct
122 Correct 540 ms 23884 KB Output is correct
123 Correct 791 ms 32976 KB Output is correct
124 Correct 763 ms 30004 KB Output is correct
125 Correct 729 ms 28548 KB Output is correct
126 Correct 739 ms 28196 KB Output is correct
127 Correct 722 ms 25216 KB Output is correct
128 Correct 669 ms 23696 KB Output is correct