Submission #821907

# Submission time Handle Problem Language Result Execution time Memory
821907 2023-08-11T20:58:51 Z LucaIlie Construction of Highway (JOI18_construction) C++17
16 / 100
2000 ms 25352 KB
#include <bits/stdc++.h>

using namespace std;

const int UNDEF = 0;
const int MAX_N = 1e5;
int c[MAX_N], parent[MAX_N], ord[MAX_N];
vector<int> edges[MAX_N];

struct vr {
    int val, rep;
};

struct info {
    vector<vr> v;

    info operator + ( info &a ) {
        vector<vr> ans = v;
        for ( vr x: a.v ) {
            if ( !ans.empty() && ans.back().val == x.val )
                ans[ans.size() - 1].rep += x.rep;
            else
                ans.push_back( x );
        }

        return { ans };
    }
};


struct SegTree {

    info segTree[4 * MAX_N];
    int lazy[4 * MAX_N];

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

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

        segTree[v].v.clear();
        segTree[v].v.push_back( { lazy[v], r - l + 1 } );
        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 ) {
        //if ( v == 0 )
            //printf( "%d %d %d\n", lu, ru, 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 {};

        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[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[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[head[u]];
        }
    }

    info queryPath( int u ) {
        info ans;

        while ( u != 0 ) {
            info a = aint.query( 0, 1, n, leftPos[head[u]], leftPos[u] );
            ans = a + ans;
            u = parent[head[u]];
        }

        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() {
    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[v] = u;
        edges[u].push_back( v );
        ord[i] = v;
    }

    arb.init( n );
    arb.updatePath( 1, c[1] );
    for ( int i = 0; i < n - 1; i++ ) {
        int u = ord[i];
        vector<vr> v = arb.queryPath( parent[u] ).v;

        int sum = 0;
        long long ans = 0;
        for ( vr x: v ) {
            ans += (long long)x.rep * (sum - aib.query( x.val ));
            aib.update( x.val, x.rep );
            sum += x.rep;
        }
        for ( vr x: v )
            aib.update( x.val, -x.rep );
        cout << ans << "\n";

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

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 13652 KB Output is correct
2 Correct 6 ms 13676 KB Output is correct
3 Correct 7 ms 13652 KB Output is correct
4 Correct 8 ms 13732 KB Output is correct
5 Correct 11 ms 13860 KB Output is correct
6 Correct 10 ms 13852 KB Output is correct
7 Correct 10 ms 13884 KB Output is correct
8 Correct 7 ms 13908 KB Output is correct
9 Correct 8 ms 13864 KB Output is correct
10 Correct 7 ms 13908 KB Output is correct
11 Correct 9 ms 14080 KB Output is correct
12 Correct 8 ms 14036 KB Output is correct
13 Correct 9 ms 14036 KB Output is correct
14 Correct 8 ms 13904 KB Output is correct
15 Correct 11 ms 13908 KB Output is correct
16 Correct 11 ms 13908 KB Output is correct
17 Correct 11 ms 13848 KB Output is correct
18 Correct 11 ms 13940 KB Output is correct
19 Correct 8 ms 13908 KB Output is correct
20 Correct 8 ms 13964 KB Output is correct
21 Correct 8 ms 13908 KB Output is correct
22 Correct 9 ms 13780 KB Output is correct
23 Correct 9 ms 13864 KB Output is correct
24 Correct 10 ms 13864 KB Output is correct
25 Correct 9 ms 13860 KB Output is correct
26 Correct 7 ms 13864 KB Output is correct
27 Correct 8 ms 13988 KB Output is correct
28 Correct 8 ms 14036 KB Output is correct
29 Correct 8 ms 13860 KB Output is correct
30 Correct 11 ms 13908 KB Output is correct
31 Correct 11 ms 13868 KB Output is correct
32 Correct 9 ms 13908 KB Output is correct
33 Correct 8 ms 13864 KB Output is correct
34 Correct 9 ms 13880 KB Output is correct
35 Correct 9 ms 13780 KB Output is correct
36 Correct 8 ms 13780 KB Output is correct
37 Correct 10 ms 13780 KB Output is correct
38 Correct 8 ms 13780 KB Output is correct
39 Correct 9 ms 13908 KB Output is correct
40 Correct 8 ms 13780 KB Output is correct
41 Correct 8 ms 13908 KB Output is correct
42 Correct 8 ms 13780 KB Output is correct
43 Correct 8 ms 13780 KB Output is correct
44 Correct 8 ms 13780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 13652 KB Output is correct
2 Correct 6 ms 13676 KB Output is correct
3 Correct 7 ms 13652 KB Output is correct
4 Correct 8 ms 13732 KB Output is correct
5 Correct 11 ms 13860 KB Output is correct
6 Correct 10 ms 13852 KB Output is correct
7 Correct 10 ms 13884 KB Output is correct
8 Correct 7 ms 13908 KB Output is correct
9 Correct 8 ms 13864 KB Output is correct
10 Correct 7 ms 13908 KB Output is correct
11 Correct 9 ms 14080 KB Output is correct
12 Correct 8 ms 14036 KB Output is correct
13 Correct 9 ms 14036 KB Output is correct
14 Correct 8 ms 13904 KB Output is correct
15 Correct 11 ms 13908 KB Output is correct
16 Correct 11 ms 13908 KB Output is correct
17 Correct 11 ms 13848 KB Output is correct
18 Correct 11 ms 13940 KB Output is correct
19 Correct 8 ms 13908 KB Output is correct
20 Correct 8 ms 13964 KB Output is correct
21 Correct 8 ms 13908 KB Output is correct
22 Correct 9 ms 13780 KB Output is correct
23 Correct 9 ms 13864 KB Output is correct
24 Correct 10 ms 13864 KB Output is correct
25 Correct 9 ms 13860 KB Output is correct
26 Correct 7 ms 13864 KB Output is correct
27 Correct 8 ms 13988 KB Output is correct
28 Correct 8 ms 14036 KB Output is correct
29 Correct 8 ms 13860 KB Output is correct
30 Correct 11 ms 13908 KB Output is correct
31 Correct 11 ms 13868 KB Output is correct
32 Correct 9 ms 13908 KB Output is correct
33 Correct 8 ms 13864 KB Output is correct
34 Correct 9 ms 13880 KB Output is correct
35 Correct 9 ms 13780 KB Output is correct
36 Correct 8 ms 13780 KB Output is correct
37 Correct 10 ms 13780 KB Output is correct
38 Correct 8 ms 13780 KB Output is correct
39 Correct 9 ms 13908 KB Output is correct
40 Correct 8 ms 13780 KB Output is correct
41 Correct 8 ms 13908 KB Output is correct
42 Correct 8 ms 13780 KB Output is correct
43 Correct 8 ms 13780 KB Output is correct
44 Correct 8 ms 13780 KB Output is correct
45 Correct 15 ms 14024 KB Output is correct
46 Correct 88 ms 15076 KB Output is correct
47 Correct 92 ms 15120 KB Output is correct
48 Correct 92 ms 15192 KB Output is correct
49 Correct 16 ms 15700 KB Output is correct
50 Correct 17 ms 15676 KB Output is correct
51 Correct 16 ms 15700 KB Output is correct
52 Correct 37 ms 15716 KB Output is correct
53 Correct 38 ms 15788 KB Output is correct
54 Correct 39 ms 15780 KB Output is correct
55 Correct 38 ms 15788 KB Output is correct
56 Correct 33 ms 15512 KB Output is correct
57 Correct 137 ms 15288 KB Output is correct
58 Correct 132 ms 15256 KB Output is correct
59 Correct 134 ms 15256 KB Output is correct
60 Correct 132 ms 15264 KB Output is correct
61 Correct 33 ms 15564 KB Output is correct
62 Correct 38 ms 15716 KB Output is correct
63 Correct 34 ms 15604 KB Output is correct
64 Correct 61 ms 14436 KB Output is correct
65 Correct 84 ms 14900 KB Output is correct
66 Correct 84 ms 14956 KB Output is correct
67 Correct 90 ms 14968 KB Output is correct
68 Correct 14 ms 15416 KB Output is correct
69 Correct 43 ms 15884 KB Output is correct
70 Correct 36 ms 15532 KB Output is correct
71 Correct 31 ms 15192 KB Output is correct
72 Correct 131 ms 15196 KB Output is correct
73 Correct 133 ms 15092 KB Output is correct
74 Correct 31 ms 15344 KB Output is correct
75 Correct 37 ms 15140 KB Output is correct
76 Correct 39 ms 14928 KB Output is correct
77 Correct 38 ms 14924 KB Output is correct
78 Correct 38 ms 14860 KB Output is correct
79 Correct 37 ms 14872 KB Output is correct
80 Correct 37 ms 14712 KB Output is correct
81 Correct 41 ms 15332 KB Output is correct
82 Correct 49 ms 15092 KB Output is correct
83 Correct 42 ms 15152 KB Output is correct
84 Correct 37 ms 15156 KB Output is correct
85 Correct 42 ms 14860 KB Output is correct
86 Correct 40 ms 14992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 13652 KB Output is correct
2 Correct 6 ms 13676 KB Output is correct
3 Correct 7 ms 13652 KB Output is correct
4 Correct 8 ms 13732 KB Output is correct
5 Correct 11 ms 13860 KB Output is correct
6 Correct 10 ms 13852 KB Output is correct
7 Correct 10 ms 13884 KB Output is correct
8 Correct 7 ms 13908 KB Output is correct
9 Correct 8 ms 13864 KB Output is correct
10 Correct 7 ms 13908 KB Output is correct
11 Correct 9 ms 14080 KB Output is correct
12 Correct 8 ms 14036 KB Output is correct
13 Correct 9 ms 14036 KB Output is correct
14 Correct 8 ms 13904 KB Output is correct
15 Correct 11 ms 13908 KB Output is correct
16 Correct 11 ms 13908 KB Output is correct
17 Correct 11 ms 13848 KB Output is correct
18 Correct 11 ms 13940 KB Output is correct
19 Correct 8 ms 13908 KB Output is correct
20 Correct 8 ms 13964 KB Output is correct
21 Correct 8 ms 13908 KB Output is correct
22 Correct 9 ms 13780 KB Output is correct
23 Correct 9 ms 13864 KB Output is correct
24 Correct 10 ms 13864 KB Output is correct
25 Correct 9 ms 13860 KB Output is correct
26 Correct 7 ms 13864 KB Output is correct
27 Correct 8 ms 13988 KB Output is correct
28 Correct 8 ms 14036 KB Output is correct
29 Correct 8 ms 13860 KB Output is correct
30 Correct 11 ms 13908 KB Output is correct
31 Correct 11 ms 13868 KB Output is correct
32 Correct 9 ms 13908 KB Output is correct
33 Correct 8 ms 13864 KB Output is correct
34 Correct 9 ms 13880 KB Output is correct
35 Correct 9 ms 13780 KB Output is correct
36 Correct 8 ms 13780 KB Output is correct
37 Correct 10 ms 13780 KB Output is correct
38 Correct 8 ms 13780 KB Output is correct
39 Correct 9 ms 13908 KB Output is correct
40 Correct 8 ms 13780 KB Output is correct
41 Correct 8 ms 13908 KB Output is correct
42 Correct 8 ms 13780 KB Output is correct
43 Correct 8 ms 13780 KB Output is correct
44 Correct 8 ms 13780 KB Output is correct
45 Correct 15 ms 14024 KB Output is correct
46 Correct 88 ms 15076 KB Output is correct
47 Correct 92 ms 15120 KB Output is correct
48 Correct 92 ms 15192 KB Output is correct
49 Correct 16 ms 15700 KB Output is correct
50 Correct 17 ms 15676 KB Output is correct
51 Correct 16 ms 15700 KB Output is correct
52 Correct 37 ms 15716 KB Output is correct
53 Correct 38 ms 15788 KB Output is correct
54 Correct 39 ms 15780 KB Output is correct
55 Correct 38 ms 15788 KB Output is correct
56 Correct 33 ms 15512 KB Output is correct
57 Correct 137 ms 15288 KB Output is correct
58 Correct 132 ms 15256 KB Output is correct
59 Correct 134 ms 15256 KB Output is correct
60 Correct 132 ms 15264 KB Output is correct
61 Correct 33 ms 15564 KB Output is correct
62 Correct 38 ms 15716 KB Output is correct
63 Correct 34 ms 15604 KB Output is correct
64 Correct 61 ms 14436 KB Output is correct
65 Correct 84 ms 14900 KB Output is correct
66 Correct 84 ms 14956 KB Output is correct
67 Correct 90 ms 14968 KB Output is correct
68 Correct 14 ms 15416 KB Output is correct
69 Correct 43 ms 15884 KB Output is correct
70 Correct 36 ms 15532 KB Output is correct
71 Correct 31 ms 15192 KB Output is correct
72 Correct 131 ms 15196 KB Output is correct
73 Correct 133 ms 15092 KB Output is correct
74 Correct 31 ms 15344 KB Output is correct
75 Correct 37 ms 15140 KB Output is correct
76 Correct 39 ms 14928 KB Output is correct
77 Correct 38 ms 14924 KB Output is correct
78 Correct 38 ms 14860 KB Output is correct
79 Correct 37 ms 14872 KB Output is correct
80 Correct 37 ms 14712 KB Output is correct
81 Correct 41 ms 15332 KB Output is correct
82 Correct 49 ms 15092 KB Output is correct
83 Correct 42 ms 15152 KB Output is correct
84 Correct 37 ms 15156 KB Output is correct
85 Correct 42 ms 14860 KB Output is correct
86 Correct 40 ms 14992 KB Output is correct
87 Correct 526 ms 18020 KB Output is correct
88 Execution timed out 2079 ms 25352 KB Time limit exceeded
89 Halted 0 ms 0 KB -