Submission #884363

# Submission time Handle Problem Language Result Execution time Memory
884363 2023-12-07T08:48:01 Z LucaIlie Sprinkler (JOI22_sprinkler) C++17
100 / 100
3316 ms 209832 KB
#include <bits/stdc++.h>

using namespace std;

struct update {
    int v, g, d, x, t;
};

struct query {
    int v, g, t;
};

const int MAX_N = 2e5;
const int MAX_AIB = MAX_N + 1;
const int MAX_Q = 4e5;
int n, l;
bool vis[MAX_N + 1];
int h[MAX_N + 1], ans[MAX_Q], sz[MAX_N + 1], depth[MAX_N + 1];
vector<int> edges[MAX_N + 1];
vector<update> updatesV[MAX_N + 1], updates;
vector<query> queriesV[MAX_N + 1], queries;

struct AIB {
    unordered_map<int, int> aib[MAX_AIB + 1];
    vector<int> changes;
    int size1, size2;

    void init( int _size1, int _size2 ) {
        size1 = _size1, size2 = _size2;
        for ( int p: changes )
            aib[p].clear();
        changes.clear();
    }

    void update( int i, int j, int x ) {
        while ( i <= size1 ) {
            int p = j;
            while ( p <= size2 ) {
                if ( aib[i].find( p ) == aib[i].end() )
                    aib[i][p] = 1;
                aib[i][p] = (long long)aib[i][p] * x % l;
                p += (p & -p);
            }
            changes.push_back( i );
            i += (i & -i);
        }
    }

    int query( int i, int j ) {
        int ans = 1;
        while ( i > 0 ) {
            int p = j;
            while ( p > 0 ) {
                if ( aib[i].find( p ) != aib[i].end() )
                    ans = (long long)ans * aib[i][p] % l;
                p -= (p & -p);
            }
            i -= (i & -i);
        }
        return ans;
    }
} aibNI, aibII;

int lgPut( int x, int n ) {
    if ( n == 0 )
        return 1;

    int p = lgPut( x, n / 2 );
    p = (long long)p * p % l;
    if ( n % 2 == 1 )
        p = (long long)p * x % l;

    return p;
}

int invmod( int x ) {
    return lgPut( x, l - 2 );
}

void calcSizes( int u, int p ) {
    sz[u] = 1;
    for ( int v: edges[u] ) {
        if ( v == p || vis[v] )
            continue;
        calcSizes( v, u );
        sz[u] += sz[v];
    }
}

int cen, totSz;
void findCentroid( int u, int p ) {
    int maxSize = 0;
    for ( int v: edges[u] ) {
        if ( v == p || vis[v] )
            continue;
        findCentroid( v, u );
        maxSize = max( maxSize, sz[v] );
    }
    maxSize = max( maxSize, totSz - sz[u] );

    if ( maxSize <= totSz / 2 )
        cen = u;
}

void dfs( int u, int p, int g ) {
    depth[u] = depth[p] + 1;
    for ( update upd: updatesV[u] ) {
        upd.g = g;
        updates.push_back( upd );
    }
    for ( query qry: queriesV[u] ) {
        qry.g = g;
        queries.push_back( qry );
    }
    for ( int v: edges[u] ) {
        if ( v == p || vis[v] )
            continue;
        dfs( v, u, g );
    }
}

void decomp( int r ) {
    calcSizes( r, 0 );
    totSz = sz[r];
    findCentroid( r, 0 );
    int c = cen;

    int g = 1;
    updates.clear();
    queries.clear();
    depth[c] = 0;
    for ( update upd: updatesV[c] ) {
        upd.g = g;
        updates.push_back( upd );
    }
    for ( query qry: queriesV[c] ) {
        qry.g = g;
        queries.push_back( qry );
    }
    for ( int v: edges[c] ) {
        if ( vis[v] )
            continue;
        ++g;
        dfs( v, c, g );
    }
    sort( updates.begin(), updates.end(), []( update x, update y ) {
        return x.t < y.t;
    } );
    sort( queries.begin(), queries.end(), []( query x, query y ) {
        return x.t < y.t;
    } );

    aibNI.init( g, n + 1 );
    aibII.init( g, n + 1 );

    int pu = 0;
    for ( query q: queries ) {
        while ( pu < updates.size() && updates[pu].t < q.t ) {
            if ( updates[pu].d >= depth[updates[pu].v] ) {
                aibNI.update( updates[pu].g, (n + 1) - min( n, (updates[pu].d - depth[updates[pu].v]) ), updates[pu].x );
                aibII.update( (g + 1) - updates[pu].g, (n + 1) - min( n, (updates[pu].d - depth[updates[pu].v]) ), updates[pu].x );
            }
            pu++;
        }
        if ( q.g == 1 )
            ans[q.t] = (long long)ans[q.t] * aibNI.query( g, (n + 1) - depth[q.v] ) % l;
        else
            ans[q.t] = (long long)ans[q.t] * aibNI.query( q.g - 1, (n + 1) - depth[q.v] ) % l * aibII.query( (g + 1) - (q.g + 1), (n + 1) - depth[q.v] ) % l;
    }

    vis[c] = true;
    for ( int v: edges[c] ) {
        if ( !vis[v] )
            decomp( v );
    }
}


int main() {
    cin >> n >> l;
    for ( int i = 0; i < n - 1; i++ ) {
        int u, v;
        cin >> u >> v;
        edges[u].push_back( v );
        edges[v].push_back( u );
    }
    for ( int v = 1; v <= n; v++ )
        cin >> h[v];
    int q;
    cin >> q;
    for ( int i = 0; i < q; i++ ) {
        int t;
        cin >> t;
        if ( t == 1 ) {
            int v, d, x;
            cin >> v >> d >> x;
            updatesV[v].push_back( { v, 0, d, x, i } );
            ans[i] = -1;
        } else {
            int v;
            cin >> v;
            queriesV[v].push_back( { v, 0, i } );
            ans[i] = h[v];
        }
    }

    decomp( 1 );

    for ( int i = 0; i < q; i++ ) {
        if ( ans[i] != -1 )
            cout << ans[i] << "\n";
    }

    return 0;
}

Compilation message

sprinkler.cpp: In function 'void decomp(int)':
sprinkler.cpp:158:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<update>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  158 |         while ( pu < updates.size() && updates[pu].t < q.t ) {
      |                 ~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 39520 KB Output is correct
2 Correct 10 ms 39516 KB Output is correct
3 Correct 9 ms 39512 KB Output is correct
4 Correct 14 ms 39604 KB Output is correct
5 Correct 13 ms 39516 KB Output is correct
6 Correct 15 ms 39516 KB Output is correct
7 Correct 14 ms 39912 KB Output is correct
8 Correct 13 ms 40112 KB Output is correct
9 Correct 11 ms 39516 KB Output is correct
10 Correct 10 ms 39516 KB Output is correct
11 Correct 12 ms 39516 KB Output is correct
12 Correct 11 ms 39516 KB Output is correct
13 Correct 10 ms 39516 KB Output is correct
14 Correct 10 ms 39516 KB Output is correct
15 Correct 10 ms 39680 KB Output is correct
16 Correct 10 ms 39516 KB Output is correct
17 Correct 10 ms 39516 KB Output is correct
18 Correct 10 ms 39516 KB Output is correct
19 Correct 9 ms 39600 KB Output is correct
20 Correct 10 ms 39512 KB Output is correct
21 Correct 10 ms 39516 KB Output is correct
22 Correct 13 ms 39516 KB Output is correct
23 Correct 11 ms 39512 KB Output is correct
24 Correct 10 ms 39768 KB Output is correct
25 Correct 10 ms 39672 KB Output is correct
26 Correct 12 ms 39516 KB Output is correct
27 Correct 10 ms 39672 KB Output is correct
28 Correct 10 ms 39516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39516 KB Output is correct
2 Correct 1639 ms 72272 KB Output is correct
3 Correct 1295 ms 80060 KB Output is correct
4 Correct 1795 ms 84596 KB Output is correct
5 Correct 1475 ms 73716 KB Output is correct
6 Correct 1327 ms 73156 KB Output is correct
7 Correct 1214 ms 74632 KB Output is correct
8 Correct 1066 ms 115340 KB Output is correct
9 Correct 1901 ms 87500 KB Output is correct
10 Correct 1589 ms 92612 KB Output is correct
11 Correct 1663 ms 72912 KB Output is correct
12 Correct 1344 ms 78720 KB Output is correct
13 Correct 903 ms 123352 KB Output is correct
14 Correct 1046 ms 105256 KB Output is correct
15 Correct 1070 ms 89704 KB Output is correct
16 Correct 1093 ms 85496 KB Output is correct
17 Correct 1133 ms 77028 KB Output is correct
18 Correct 10 ms 39516 KB Output is correct
19 Correct 11 ms 39676 KB Output is correct
20 Correct 13 ms 39516 KB Output is correct
21 Correct 10 ms 39512 KB Output is correct
22 Correct 12 ms 39516 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39516 KB Output is correct
2 Correct 1639 ms 72272 KB Output is correct
3 Correct 1295 ms 80060 KB Output is correct
4 Correct 1795 ms 84596 KB Output is correct
5 Correct 1475 ms 73716 KB Output is correct
6 Correct 1327 ms 73156 KB Output is correct
7 Correct 1214 ms 74632 KB Output is correct
8 Correct 1066 ms 115340 KB Output is correct
9 Correct 1901 ms 87500 KB Output is correct
10 Correct 1589 ms 92612 KB Output is correct
11 Correct 1663 ms 72912 KB Output is correct
12 Correct 1344 ms 78720 KB Output is correct
13 Correct 903 ms 123352 KB Output is correct
14 Correct 1046 ms 105256 KB Output is correct
15 Correct 1070 ms 89704 KB Output is correct
16 Correct 1093 ms 85496 KB Output is correct
17 Correct 1133 ms 77028 KB Output is correct
18 Correct 10 ms 39516 KB Output is correct
19 Correct 11 ms 39676 KB Output is correct
20 Correct 13 ms 39516 KB Output is correct
21 Correct 10 ms 39512 KB Output is correct
22 Correct 12 ms 39516 KB Output is correct
23 Correct 12 ms 39592 KB Output is correct
24 Correct 1776 ms 71944 KB Output is correct
25 Correct 1752 ms 82628 KB Output is correct
26 Correct 1897 ms 91360 KB Output is correct
27 Correct 1575 ms 75856 KB Output is correct
28 Correct 1487 ms 76920 KB Output is correct
29 Correct 1201 ms 75960 KB Output is correct
30 Correct 1297 ms 131312 KB Output is correct
31 Correct 1973 ms 85584 KB Output is correct
32 Correct 1593 ms 95180 KB Output is correct
33 Correct 1684 ms 73572 KB Output is correct
34 Correct 1294 ms 82628 KB Output is correct
35 Correct 10 ms 39516 KB Output is correct
36 Correct 10 ms 39676 KB Output is correct
37 Correct 10 ms 39516 KB Output is correct
38 Correct 10 ms 39516 KB Output is correct
39 Correct 10 ms 39616 KB Output is correct
40 Correct 10 ms 39512 KB Output is correct
41 Correct 10 ms 39516 KB Output is correct
42 Correct 10 ms 39768 KB Output is correct
43 Correct 10 ms 39516 KB Output is correct
44 Correct 10 ms 39516 KB Output is correct
45 Correct 10 ms 39516 KB Output is correct
46 Correct 12 ms 39596 KB Output is correct
47 Correct 10 ms 39512 KB Output is correct
48 Correct 11 ms 39516 KB Output is correct
49 Correct 10 ms 39852 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39516 KB Output is correct
2 Correct 2222 ms 84896 KB Output is correct
3 Correct 2004 ms 89828 KB Output is correct
4 Correct 2067 ms 84740 KB Output is correct
5 Correct 1955 ms 71604 KB Output is correct
6 Correct 2130 ms 76480 KB Output is correct
7 Correct 2031 ms 76968 KB Output is correct
8 Correct 2864 ms 204668 KB Output is correct
9 Correct 2168 ms 81356 KB Output is correct
10 Correct 1963 ms 90868 KB Output is correct
11 Correct 1918 ms 70216 KB Output is correct
12 Correct 1998 ms 77912 KB Output is correct
13 Correct 2955 ms 77988 KB Output is correct
14 Correct 3065 ms 78268 KB Output is correct
15 Correct 11 ms 39516 KB Output is correct
16 Correct 12 ms 39772 KB Output is correct
17 Correct 10 ms 39516 KB Output is correct
18 Correct 10 ms 39516 KB Output is correct
19 Correct 9 ms 39620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 39512 KB Output is correct
2 Correct 2167 ms 82252 KB Output is correct
3 Correct 1938 ms 89420 KB Output is correct
4 Correct 2088 ms 85668 KB Output is correct
5 Correct 1962 ms 75520 KB Output is correct
6 Correct 2157 ms 79592 KB Output is correct
7 Correct 2137 ms 79680 KB Output is correct
8 Correct 2870 ms 208536 KB Output is correct
9 Correct 2133 ms 88032 KB Output is correct
10 Correct 1939 ms 93640 KB Output is correct
11 Correct 1902 ms 74180 KB Output is correct
12 Correct 1969 ms 78676 KB Output is correct
13 Correct 2918 ms 80676 KB Output is correct
14 Correct 3005 ms 80020 KB Output is correct
15 Correct 10 ms 39516 KB Output is correct
16 Correct 10 ms 39516 KB Output is correct
17 Correct 10 ms 39516 KB Output is correct
18 Correct 10 ms 39672 KB Output is correct
19 Correct 10 ms 39576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 39520 KB Output is correct
2 Correct 10 ms 39516 KB Output is correct
3 Correct 9 ms 39512 KB Output is correct
4 Correct 14 ms 39604 KB Output is correct
5 Correct 13 ms 39516 KB Output is correct
6 Correct 15 ms 39516 KB Output is correct
7 Correct 14 ms 39912 KB Output is correct
8 Correct 13 ms 40112 KB Output is correct
9 Correct 11 ms 39516 KB Output is correct
10 Correct 10 ms 39516 KB Output is correct
11 Correct 12 ms 39516 KB Output is correct
12 Correct 11 ms 39516 KB Output is correct
13 Correct 10 ms 39516 KB Output is correct
14 Correct 10 ms 39516 KB Output is correct
15 Correct 10 ms 39680 KB Output is correct
16 Correct 10 ms 39516 KB Output is correct
17 Correct 10 ms 39516 KB Output is correct
18 Correct 10 ms 39516 KB Output is correct
19 Correct 9 ms 39600 KB Output is correct
20 Correct 10 ms 39512 KB Output is correct
21 Correct 10 ms 39516 KB Output is correct
22 Correct 13 ms 39516 KB Output is correct
23 Correct 11 ms 39512 KB Output is correct
24 Correct 10 ms 39768 KB Output is correct
25 Correct 10 ms 39672 KB Output is correct
26 Correct 12 ms 39516 KB Output is correct
27 Correct 10 ms 39672 KB Output is correct
28 Correct 10 ms 39516 KB Output is correct
29 Correct 9 ms 39516 KB Output is correct
30 Correct 1639 ms 72272 KB Output is correct
31 Correct 1295 ms 80060 KB Output is correct
32 Correct 1795 ms 84596 KB Output is correct
33 Correct 1475 ms 73716 KB Output is correct
34 Correct 1327 ms 73156 KB Output is correct
35 Correct 1214 ms 74632 KB Output is correct
36 Correct 1066 ms 115340 KB Output is correct
37 Correct 1901 ms 87500 KB Output is correct
38 Correct 1589 ms 92612 KB Output is correct
39 Correct 1663 ms 72912 KB Output is correct
40 Correct 1344 ms 78720 KB Output is correct
41 Correct 903 ms 123352 KB Output is correct
42 Correct 1046 ms 105256 KB Output is correct
43 Correct 1070 ms 89704 KB Output is correct
44 Correct 1093 ms 85496 KB Output is correct
45 Correct 1133 ms 77028 KB Output is correct
46 Correct 10 ms 39516 KB Output is correct
47 Correct 11 ms 39676 KB Output is correct
48 Correct 13 ms 39516 KB Output is correct
49 Correct 10 ms 39512 KB Output is correct
50 Correct 12 ms 39516 KB Output is correct
51 Correct 12 ms 39592 KB Output is correct
52 Correct 1776 ms 71944 KB Output is correct
53 Correct 1752 ms 82628 KB Output is correct
54 Correct 1897 ms 91360 KB Output is correct
55 Correct 1575 ms 75856 KB Output is correct
56 Correct 1487 ms 76920 KB Output is correct
57 Correct 1201 ms 75960 KB Output is correct
58 Correct 1297 ms 131312 KB Output is correct
59 Correct 1973 ms 85584 KB Output is correct
60 Correct 1593 ms 95180 KB Output is correct
61 Correct 1684 ms 73572 KB Output is correct
62 Correct 1294 ms 82628 KB Output is correct
63 Correct 10 ms 39516 KB Output is correct
64 Correct 10 ms 39676 KB Output is correct
65 Correct 10 ms 39516 KB Output is correct
66 Correct 10 ms 39516 KB Output is correct
67 Correct 10 ms 39616 KB Output is correct
68 Correct 10 ms 39512 KB Output is correct
69 Correct 10 ms 39516 KB Output is correct
70 Correct 10 ms 39768 KB Output is correct
71 Correct 10 ms 39516 KB Output is correct
72 Correct 10 ms 39516 KB Output is correct
73 Correct 10 ms 39516 KB Output is correct
74 Correct 12 ms 39596 KB Output is correct
75 Correct 10 ms 39512 KB Output is correct
76 Correct 11 ms 39516 KB Output is correct
77 Correct 10 ms 39852 KB Output is correct
78 Correct 9 ms 39516 KB Output is correct
79 Correct 2222 ms 84896 KB Output is correct
80 Correct 2004 ms 89828 KB Output is correct
81 Correct 2067 ms 84740 KB Output is correct
82 Correct 1955 ms 71604 KB Output is correct
83 Correct 2130 ms 76480 KB Output is correct
84 Correct 2031 ms 76968 KB Output is correct
85 Correct 2864 ms 204668 KB Output is correct
86 Correct 2168 ms 81356 KB Output is correct
87 Correct 1963 ms 90868 KB Output is correct
88 Correct 1918 ms 70216 KB Output is correct
89 Correct 1998 ms 77912 KB Output is correct
90 Correct 2955 ms 77988 KB Output is correct
91 Correct 3065 ms 78268 KB Output is correct
92 Correct 11 ms 39516 KB Output is correct
93 Correct 12 ms 39772 KB Output is correct
94 Correct 10 ms 39516 KB Output is correct
95 Correct 10 ms 39516 KB Output is correct
96 Correct 9 ms 39620 KB Output is correct
97 Correct 9 ms 39512 KB Output is correct
98 Correct 2167 ms 82252 KB Output is correct
99 Correct 1938 ms 89420 KB Output is correct
100 Correct 2088 ms 85668 KB Output is correct
101 Correct 1962 ms 75520 KB Output is correct
102 Correct 2157 ms 79592 KB Output is correct
103 Correct 2137 ms 79680 KB Output is correct
104 Correct 2870 ms 208536 KB Output is correct
105 Correct 2133 ms 88032 KB Output is correct
106 Correct 1939 ms 93640 KB Output is correct
107 Correct 1902 ms 74180 KB Output is correct
108 Correct 1969 ms 78676 KB Output is correct
109 Correct 2918 ms 80676 KB Output is correct
110 Correct 3005 ms 80020 KB Output is correct
111 Correct 10 ms 39516 KB Output is correct
112 Correct 10 ms 39516 KB Output is correct
113 Correct 10 ms 39516 KB Output is correct
114 Correct 10 ms 39672 KB Output is correct
115 Correct 10 ms 39576 KB Output is correct
116 Correct 1972 ms 72452 KB Output is correct
117 Correct 2099 ms 81180 KB Output is correct
118 Correct 2100 ms 89868 KB Output is correct
119 Correct 2015 ms 76324 KB Output is correct
120 Correct 2126 ms 82252 KB Output is correct
121 Correct 2282 ms 83392 KB Output is correct
122 Correct 2854 ms 209832 KB Output is correct
123 Correct 2142 ms 85424 KB Output is correct
124 Correct 2021 ms 92504 KB Output is correct
125 Correct 1881 ms 73440 KB Output is correct
126 Correct 2162 ms 82564 KB Output is correct
127 Correct 2108 ms 81604 KB Output is correct
128 Correct 3222 ms 82992 KB Output is correct
129 Correct 3316 ms 84276 KB Output is correct