Submission #615166

# Submission time Handle Problem Language Result Execution time Memory
615166 2022-07-31T07:19:51 Z 이동현(#8495) Sprinkler (JOI22_sprinkler) C++17
41 / 100
4000 ms 579160 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;

const int NS = (int)2e5 + 4;

int n, mod;
vector<int> way[NS], cway[NS];
int dep[NS], sz[NS], megop[NS][44], ind[NS];
int lca[NS][20], cchk[NS], init[NS];
int cpr[NS];

struct Seg{
    int n;
    vector<int> tr;

    Seg(){}
    Seg(int n):n(n + 4){
        tr.resize(n * 4, 1);
    }

    void push(int x, int s, int e, int pos, int val){
        if(s == e){
            tr[x] = ((long long)tr[x] * val) % mod;
            return;
        }
        int m = s + e >> 1;
        if(pos <= m){
            push(x * 2, s, m, pos, val);
        }
        else{
            push(x * 2 + 1, m + 1, e, pos, val);
        }
        tr[x] = ((long long)tr[x * 2] * tr[x * 2 + 1]) % mod;
    }

    int get(int x, int s, int e, int fs, int fe){
        if(fe < s || fs > e || fs > fe) return 1;
        if(fs <= s && fe >= e){
            return tr[x];
        }

        int m = s + e >> 1;
        return (long long)get(x * 2, s, m, fs, fe) * get(x * 2 + 1, m + 1, e, fs, fe) % mod;
    }
}tree[NS][44];

void dfslca(int x, int pr){
    lca[x][0] = pr;

    for(auto&nxt:way[x]){
        if(nxt == pr){
            continue;
        }

        dep[nxt] = dep[x] + 1;
        dfslca(nxt, x);
    }
}

void dfssz(int x, int pr = -1){
    sz[x] = 1;

    for(auto&nxt:way[x]){
        if(nxt == pr || cchk[nxt]){
            continue;
        }

        dfssz(nxt, x);

        sz[x] += sz[nxt];
    }
}

int getcent(int x, int allsz, int pr = -1){
    for(auto&nxt:way[x]){
        if(nxt == pr || cchk[nxt]){
            continue;
        }

        if(sz[nxt] * 2 > allsz){
            return getcent(nxt, allsz, x);
        }
    }
    return x;
}

int docent(int x){
    dfssz(x);
    x = getcent(x, sz[x]);

    cchk[x] = 1;

    for(auto&nxt:way[x]){
        if(cchk[nxt]){
            continue;
        }

        int rv = docent(nxt);
        ind[rv] = (int)cway[x].size();
        cway[x].push_back(rv);
        cpr[rv] = x;
    }

    return x;
}

int getdist(int x, int y){
    int xs = x, ys = y;

    if(dep[x] < dep[y]){
        swap(x, y);
    }

    for(int i = 19; i >= 0; --i){
        if(dep[x] - (1 << i) >= dep[y]){
            x = lca[x][i];
        }
    }
    if(x != y){
        for(int i = 19; i >= 0; --i){
            if(lca[x][i] != lca[y][i]){
                x = lca[x][i];
                y = lca[y][i];
            }
        }
        x = lca[x][0];
    }

    return dep[xs] + dep[ys] - 2 * dep[x];
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n >> mod;
    for(int i = 1; i < n; ++i){
        int x, y; cin >> x >> y;

        way[x].push_back(y), way[y].push_back(x);
    }
    for(int i = 1; i <= n; ++i){
        cin >> init[i];
    }

    dfslca(1, 0);
    for(int j = 1; j < 20; ++j){
        for(int i = 1; i <= n; ++i){
            lca[i][j] = lca[lca[i][j - 1]][j - 1];
        }
    }

    int top = docent(1);
    for(int i = 1; i <= n; ++i){
        for(int j = 0; j <= 40; ++j){
            tree[i][j] = Seg((int)cway[i].size());
            megop[i][j] = 1;
        }
    }

    // for(int i = 1; i <= n; ++i){
    //     for(auto&nxt:cway[i]){
    //         cout << i << ' ' << nxt << endl;
    //     }
    // }

    int q; cin >> q;
    while(q--){
        int t; cin >> t;

        if(t == 1){
            int x, d, w; cin >> x >> d >> w;

            int now = x;
            for(int i = 0; i <= d; ++i){
                megop[now][i] = ((long long)megop[now][i] * w) % mod;
            }

            int cnt = 0;
            while(now != top){
                int up = cpr[now];
                int dist = getdist(x, up);

                if(d >= dist){
                    for(int i = 0; i <= d - dist; ++i){
                        tree[up][i].push(1, 0, (int)cway[up].size() - 1, ind[now], w);
                    }
                }
                now = up;
                ++cnt;
            }
            assert(cnt <= 20);
        }
        else{
            int x; cin >> x;

            int ans = init[x];
            int now = x;

            ans = ((long long)ans * tree[now][0].get(1, 0, (int)cway[now].size() - 1, 0, (int)cway[now].size() - 1)) % mod;

            while(true){
                int dist = getdist(x, now);

                if(dist <= 40){
                    ans = ((long long)ans * megop[now][dist]) % mod;
                }

                if(now == top) break;

                int up = cpr[now];
                dist = getdist(x, up);

                if(dist <= 40){
                    ans = ((long long)ans * tree[up][dist].get(1, 0, (int)cway[up].size() - 1, 0, ind[now] - 1)) % mod;
                    ans = ((long long)ans * tree[up][dist].get(1, 0, (int)cway[up].size() - 1, ind[now] + 1, (int)cway[up].size() - 1)) % mod;
                }

                now = up;
            }

            cout << ans << '\n';
        }
    }
    return 0;
}

Compilation message

sprinkler.cpp: In member function 'void Seg::push(int, int, int, int, int)':
sprinkler.cpp:29:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   29 |         int m = s + e >> 1;
      |                 ~~^~~
sprinkler.cpp: In member function 'int Seg::get(int, int, int, int, int)':
sprinkler.cpp:45:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   45 |         int m = s + e >> 1;
      |                 ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 140 ms 285192 KB Output is correct
2 Correct 149 ms 285292 KB Output is correct
3 Correct 142 ms 285192 KB Output is correct
4 Correct 141 ms 286540 KB Output is correct
5 Correct 134 ms 286556 KB Output is correct
6 Correct 151 ms 286568 KB Output is correct
7 Correct 156 ms 286528 KB Output is correct
8 Correct 138 ms 286288 KB Output is correct
9 Correct 158 ms 285284 KB Output is correct
10 Correct 157 ms 285308 KB Output is correct
11 Correct 131 ms 285312 KB Output is correct
12 Correct 136 ms 285248 KB Output is correct
13 Correct 141 ms 285360 KB Output is correct
14 Correct 130 ms 285312 KB Output is correct
15 Correct 137 ms 285320 KB Output is correct
16 Correct 134 ms 285260 KB Output is correct
17 Correct 145 ms 285224 KB Output is correct
18 Correct 133 ms 285260 KB Output is correct
19 Correct 132 ms 285392 KB Output is correct
20 Correct 141 ms 285332 KB Output is correct
21 Correct 140 ms 285304 KB Output is correct
22 Correct 133 ms 285304 KB Output is correct
23 Correct 137 ms 285240 KB Output is correct
24 Correct 147 ms 285236 KB Output is correct
25 Correct 169 ms 285232 KB Output is correct
26 Correct 132 ms 285312 KB Output is correct
27 Correct 141 ms 285268 KB Output is correct
28 Correct 162 ms 285260 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 285244 KB Output is correct
2 Correct 2532 ms 544480 KB Output is correct
3 Correct 2068 ms 545140 KB Output is correct
4 Correct 3452 ms 575328 KB Output is correct
5 Correct 2342 ms 546468 KB Output is correct
6 Correct 1535 ms 540668 KB Output is correct
7 Correct 1397 ms 533932 KB Output is correct
8 Correct 799 ms 484632 KB Output is correct
9 Correct 3316 ms 579160 KB Output is correct
10 Correct 2910 ms 575240 KB Output is correct
11 Correct 2516 ms 548180 KB Output is correct
12 Correct 1986 ms 544688 KB Output is correct
13 Correct 621 ms 493084 KB Output is correct
14 Correct 659 ms 492520 KB Output is correct
15 Correct 744 ms 493124 KB Output is correct
16 Correct 815 ms 492316 KB Output is correct
17 Correct 824 ms 493300 KB Output is correct
18 Correct 139 ms 285188 KB Output is correct
19 Correct 132 ms 285296 KB Output is correct
20 Correct 136 ms 285260 KB Output is correct
21 Correct 133 ms 285336 KB Output is correct
22 Correct 147 ms 285316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 285244 KB Output is correct
2 Correct 2532 ms 544480 KB Output is correct
3 Correct 2068 ms 545140 KB Output is correct
4 Correct 3452 ms 575328 KB Output is correct
5 Correct 2342 ms 546468 KB Output is correct
6 Correct 1535 ms 540668 KB Output is correct
7 Correct 1397 ms 533932 KB Output is correct
8 Correct 799 ms 484632 KB Output is correct
9 Correct 3316 ms 579160 KB Output is correct
10 Correct 2910 ms 575240 KB Output is correct
11 Correct 2516 ms 548180 KB Output is correct
12 Correct 1986 ms 544688 KB Output is correct
13 Correct 621 ms 493084 KB Output is correct
14 Correct 659 ms 492520 KB Output is correct
15 Correct 744 ms 493124 KB Output is correct
16 Correct 815 ms 492316 KB Output is correct
17 Correct 824 ms 493300 KB Output is correct
18 Correct 139 ms 285188 KB Output is correct
19 Correct 132 ms 285296 KB Output is correct
20 Correct 136 ms 285260 KB Output is correct
21 Correct 133 ms 285336 KB Output is correct
22 Correct 147 ms 285316 KB Output is correct
23 Correct 155 ms 285184 KB Output is correct
24 Correct 2725 ms 546116 KB Output is correct
25 Correct 2392 ms 544136 KB Output is correct
26 Correct 3510 ms 576676 KB Output is correct
27 Correct 2206 ms 546040 KB Output is correct
28 Correct 1563 ms 540320 KB Output is correct
29 Correct 1502 ms 533320 KB Output is correct
30 Correct 855 ms 484056 KB Output is correct
31 Correct 3787 ms 575152 KB Output is correct
32 Correct 2996 ms 574868 KB Output is correct
33 Correct 2580 ms 546156 KB Output is correct
34 Correct 2190 ms 544144 KB Output is correct
35 Correct 149 ms 285308 KB Output is correct
36 Correct 162 ms 285216 KB Output is correct
37 Correct 163 ms 285252 KB Output is correct
38 Correct 160 ms 285248 KB Output is correct
39 Correct 185 ms 285316 KB Output is correct
40 Correct 168 ms 285300 KB Output is correct
41 Correct 188 ms 285252 KB Output is correct
42 Correct 175 ms 285216 KB Output is correct
43 Correct 163 ms 285292 KB Output is correct
44 Correct 171 ms 285268 KB Output is correct
45 Correct 153 ms 285300 KB Output is correct
46 Correct 155 ms 285308 KB Output is correct
47 Correct 150 ms 285288 KB Output is correct
48 Correct 161 ms 285232 KB Output is correct
49 Correct 172 ms 285276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 285200 KB Output is correct
2 Execution timed out 4072 ms 571172 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 165 ms 285300 KB Output is correct
2 Correct 3882 ms 573472 KB Output is correct
3 Execution timed out 4122 ms 569272 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 140 ms 285192 KB Output is correct
2 Correct 149 ms 285292 KB Output is correct
3 Correct 142 ms 285192 KB Output is correct
4 Correct 141 ms 286540 KB Output is correct
5 Correct 134 ms 286556 KB Output is correct
6 Correct 151 ms 286568 KB Output is correct
7 Correct 156 ms 286528 KB Output is correct
8 Correct 138 ms 286288 KB Output is correct
9 Correct 158 ms 285284 KB Output is correct
10 Correct 157 ms 285308 KB Output is correct
11 Correct 131 ms 285312 KB Output is correct
12 Correct 136 ms 285248 KB Output is correct
13 Correct 141 ms 285360 KB Output is correct
14 Correct 130 ms 285312 KB Output is correct
15 Correct 137 ms 285320 KB Output is correct
16 Correct 134 ms 285260 KB Output is correct
17 Correct 145 ms 285224 KB Output is correct
18 Correct 133 ms 285260 KB Output is correct
19 Correct 132 ms 285392 KB Output is correct
20 Correct 141 ms 285332 KB Output is correct
21 Correct 140 ms 285304 KB Output is correct
22 Correct 133 ms 285304 KB Output is correct
23 Correct 137 ms 285240 KB Output is correct
24 Correct 147 ms 285236 KB Output is correct
25 Correct 169 ms 285232 KB Output is correct
26 Correct 132 ms 285312 KB Output is correct
27 Correct 141 ms 285268 KB Output is correct
28 Correct 162 ms 285260 KB Output is correct
29 Correct 147 ms 285244 KB Output is correct
30 Correct 2532 ms 544480 KB Output is correct
31 Correct 2068 ms 545140 KB Output is correct
32 Correct 3452 ms 575328 KB Output is correct
33 Correct 2342 ms 546468 KB Output is correct
34 Correct 1535 ms 540668 KB Output is correct
35 Correct 1397 ms 533932 KB Output is correct
36 Correct 799 ms 484632 KB Output is correct
37 Correct 3316 ms 579160 KB Output is correct
38 Correct 2910 ms 575240 KB Output is correct
39 Correct 2516 ms 548180 KB Output is correct
40 Correct 1986 ms 544688 KB Output is correct
41 Correct 621 ms 493084 KB Output is correct
42 Correct 659 ms 492520 KB Output is correct
43 Correct 744 ms 493124 KB Output is correct
44 Correct 815 ms 492316 KB Output is correct
45 Correct 824 ms 493300 KB Output is correct
46 Correct 139 ms 285188 KB Output is correct
47 Correct 132 ms 285296 KB Output is correct
48 Correct 136 ms 285260 KB Output is correct
49 Correct 133 ms 285336 KB Output is correct
50 Correct 147 ms 285316 KB Output is correct
51 Correct 155 ms 285184 KB Output is correct
52 Correct 2725 ms 546116 KB Output is correct
53 Correct 2392 ms 544136 KB Output is correct
54 Correct 3510 ms 576676 KB Output is correct
55 Correct 2206 ms 546040 KB Output is correct
56 Correct 1563 ms 540320 KB Output is correct
57 Correct 1502 ms 533320 KB Output is correct
58 Correct 855 ms 484056 KB Output is correct
59 Correct 3787 ms 575152 KB Output is correct
60 Correct 2996 ms 574868 KB Output is correct
61 Correct 2580 ms 546156 KB Output is correct
62 Correct 2190 ms 544144 KB Output is correct
63 Correct 149 ms 285308 KB Output is correct
64 Correct 162 ms 285216 KB Output is correct
65 Correct 163 ms 285252 KB Output is correct
66 Correct 160 ms 285248 KB Output is correct
67 Correct 185 ms 285316 KB Output is correct
68 Correct 168 ms 285300 KB Output is correct
69 Correct 188 ms 285252 KB Output is correct
70 Correct 175 ms 285216 KB Output is correct
71 Correct 163 ms 285292 KB Output is correct
72 Correct 171 ms 285268 KB Output is correct
73 Correct 153 ms 285300 KB Output is correct
74 Correct 155 ms 285308 KB Output is correct
75 Correct 150 ms 285288 KB Output is correct
76 Correct 161 ms 285232 KB Output is correct
77 Correct 172 ms 285276 KB Output is correct
78 Correct 159 ms 285200 KB Output is correct
79 Execution timed out 4072 ms 571172 KB Time limit exceeded
80 Halted 0 ms 0 KB -