Submission #850838

# Submission time Handle Problem Language Result Execution time Memory
850838 2023-09-17T13:44:01 Z EJIC_B_KEDAX Sprinkler (JOI22_sprinkler) C++14
41 / 100
4000 ms 206172 KB
#include <bits/stdc++.h>
#include <random>

#ifndef LOCAL
    #pragma GCC optimize("O3")
    #pragma GCC optimize("Ofast")
    #pragma GCC optimize("unroll-loops")
    #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
typedef long long ll;
typedef double dd;
typedef long double ld;
typedef unsigned int uii;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()



void solve();

mt19937_64 mt(1);

int32_t main() {
#ifdef LOCAL
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
#else
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    //freopen("amusing.in", "r", stdin);
    //freopen("amusing.out", "w", stdout);
#endif
    cout << fixed << setprecision(30);
    int tests = 1;
    //cin >> tests;
    while (tests--) {
        solve();
    }
}

ll mod;

class SegmentTree {
public:
    void build(const vector<int>& b) {
        int sz = 1;
        while (sz < b.size()) {
            sz *= 2;
        }
        x.resize(2 * sz - 1);
        vn.resize(2 * sz - 1, 1);
        size = 2 * sz - 1;
        for (int i = sz - 1; i < 2 * sz - 1; i++) {
            if (i - sz + 1 < b.size()) {
                x[i] = b[i - sz + 1];
            } else {
                x[i] = 0;
            }
        }
        for (int i = sz - 2; i >= 0; i--) {
            //update(i);
            x[i] = 0;
        }
    }

    int get(int l, int r, int i = 0, int lx = 0, int rx = -1) {
        if (rx == -1) {
            rx = size / 2;
        }
        if (lx > r || rx < l) {
            return 0;
        }

        if (lx >= l && rx <= r) {
            return x[i];
        }

        updateVN(i);
        return get(l, r, 2 * i + 1, lx, (rx + lx) / 2) + get(l, r, 2 * i + 2, (rx + lx) / 2 + 1, rx);
    }

    void change(int ind, int c, int i = 0, int lx = 0, int rx = -1) {
        if (rx == -1) {
            rx = size / 2;
        }
        if (lx == rx) {
            x[i] = c;
            return;
        }
        updateVN(i);
        if ((rx + lx) / 2 >= ind) {
            change(ind, c, 2 * i + 1, lx, (rx + lx) / 2);
        } else {
            change(ind, c, 2 * i + 2, (rx + lx) / 2 + 1, rx);
        }
        //update(i);
    }
    void plusOtr(int l, int r, ll p, int i = 0, int lx = 0, int rx = -1) {
        if (r < l) {
            return;
        }
        if (rx == -1) {
            rx = size / 2;
        }
        if (lx >= l && rx <= r) {
            x[i] = (x[i] * p) % mod;
            vn[i] = (vn[i] * p) % mod;
            return;
        }
        if (lx > r || rx < l) {
            return;
        }
        updateVN(i);
        plusOtr(l, r, p, 2 * i + 1, lx, (rx + lx) / 2);
        plusOtr(l, r, p, 2 * i + 2, (rx + lx) / 2 + 1, rx);
        //update(i);
    }
private:
    vector<ll> x, vn;
    int size;
    void updateVN(int i) {
        if (2 * i + 2 < size) {
            if (vn[i] != 1) {
                if (x[2 * i + 1]) {
                    x[2 * i + 1] = (x[2 * i + 1] * vn[i]) % mod;
                }
                vn[2 * i + 1] = (vn[2 * i + 1] * vn[i]) % mod;
                if (x[2 * i + 2]) {
                    x[2 * i + 2] = (x[2 * i + 2] * vn[i]) % mod;
                }
                vn[2 * i + 2] = (vn[2 * i + 2] * vn[i]) % mod;
                vn[i] = 1;
            }
        }
    }
    void update(int i) {
        if (2 * i + 2 < size) {
            x[i] = x[2 * i + 1] + x[2 * i + 2];
        }
    }
};

pair<int, int> seg[200200][41];

void dfs(int s, int p, vector<vector<int>>& g, int dist[], vector<vector<int>>& d, int pr[]) {
    if (p != -1) {
        dist[s] = dist[p] + 1;
    } else {
        dist[s] = 0;
    }
    pr[s] = p;
    d[dist[s]].push_back(s);
    seg[s][0] = {d[dist[s]].size() - 1, d[dist[s]].size() - 1};
    for (int i = 1; i < 41; i++) {
        seg[s][i] = {INT32_MAX, INT32_MIN};
    }
    bool f = true;
    for (int i : g[s]) {
        if (i != p) {
            dfs(i, s, g, dist, d, pr);
            for (int j = 1; j < 41; j++) {
                seg[s][j].x = min(seg[s][j].x, seg[i][j - 1].x);
            }
            for (int j = 1; j < 41; j++) {
                seg[s][j].y = max(seg[s][j].y, seg[i][j - 1].y);
            }
        }
    }
}

void solve() {
    int n;
    cin >> n >> mod;
    vector<vector<int>> g(n), d(n);
    int deg[200200], dist[200200], h[200200], p[200200];
    for (int i = 0; i < n; i++) {
        deg[i] = 0;
    }
    int pr[200200][41], lev[200200][41];
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v; u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
        deg[u]++;
        deg[v]++;
    }
    int root = 0;
    for (int i = 0; i < n; i++) {
        if (deg[i] == 1) {
            root = i;
            break;
        }
    }
    dfs(root, -1, g, dist, d, p);
    for (int i = 0; i < n; i++) {
        int nw = i, lv = 0, nlev = 0;
        for (int c = 0; c < 41; c++) {
            while (c > nlev) {
                nlev++;
                if (p[nw] != -1) {
                    lv++;
                    nw = p[nw];
                }
            }
            pr[i][c] = nw;
            lev[i][c] = lv;
        }
    }
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < d[i].size(); j++) {
            d[i][j] = h[d[i][j]];
        }
    }
    SegmentTree dec[200200];
    for (int i = 0; i < n; i++) {
        dec[i].build(d[i]);
    }
    int q;
    cin >> q;
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            int x, dk, wk;
            cin >> x >> dk >> wk; x--;
            for (int l = max(-dk, dist[x] - n + 1); l <= dk && l <= dist[x]; l++) {
                int c = (dk + l) / 2;
                if (c - l >= 0 && c - l <= dk) {
                    int l1, r1;
                    l1 = seg[pr[x][c]][lev[x][c] - l].x;
                    r1 = seg[pr[x][c]][lev[x][c] - l].y;
                    dec[dist[x] - l].plusOtr(l1, r1, wk);
                }
            }
        } else {
            int x;
            cin >> x; x--;
            cout << (dec[dist[x]].get(seg[x][0].x, seg[x][0].y) % mod + mod) % mod << '\n';
        }
    }
}

Compilation message

sprinkler.cpp: In member function 'void SegmentTree::build(const std::vector<int>&)':
sprinkler.cpp:50:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   50 |         while (sz < b.size()) {
      |                ~~~^~~~~~~~~~
sprinkler.cpp:57:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |             if (i - sz + 1 < b.size()) {
      |                 ~~~~~~~~~~~^~~~~~~~~~
sprinkler.cpp: In function 'void dfs(int, int, std::vector<std::vector<int> >&, int*, std::vector<std::vector<int> >&, int*)':
sprinkler.cpp:160:10: warning: unused variable 'f' [-Wunused-variable]
  160 |     bool f = true;
      |          ^
sprinkler.cpp: In function 'void solve()':
sprinkler.cpp:217:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  217 |         for (int j = 0; j < d[i].size(); j++) {
      |                         ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 40 ms 78676 KB Output is correct
2 Correct 40 ms 78632 KB Output is correct
3 Correct 41 ms 78700 KB Output is correct
4 Correct 43 ms 81236 KB Output is correct
5 Correct 47 ms 81040 KB Output is correct
6 Correct 41 ms 80980 KB Output is correct
7 Correct 41 ms 80980 KB Output is correct
8 Correct 41 ms 81216 KB Output is correct
9 Correct 40 ms 78692 KB Output is correct
10 Correct 40 ms 78676 KB Output is correct
11 Correct 45 ms 78772 KB Output is correct
12 Correct 40 ms 78672 KB Output is correct
13 Correct 40 ms 78776 KB Output is correct
14 Correct 40 ms 78676 KB Output is correct
15 Correct 41 ms 78680 KB Output is correct
16 Correct 42 ms 78748 KB Output is correct
17 Correct 43 ms 78672 KB Output is correct
18 Correct 40 ms 78672 KB Output is correct
19 Correct 40 ms 78684 KB Output is correct
20 Correct 41 ms 78988 KB Output is correct
21 Correct 40 ms 78588 KB Output is correct
22 Correct 40 ms 78672 KB Output is correct
23 Correct 39 ms 78824 KB Output is correct
24 Correct 39 ms 78676 KB Output is correct
25 Correct 41 ms 78708 KB Output is correct
26 Correct 40 ms 78568 KB Output is correct
27 Correct 40 ms 78676 KB Output is correct
28 Correct 41 ms 78680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 78904 KB Output is correct
2 Correct 481 ms 184960 KB Output is correct
3 Correct 629 ms 182376 KB Output is correct
4 Correct 495 ms 204408 KB Output is correct
5 Correct 505 ms 183248 KB Output is correct
6 Correct 513 ms 184384 KB Output is correct
7 Correct 499 ms 183004 KB Output is correct
8 Correct 424 ms 182700 KB Output is correct
9 Correct 496 ms 205884 KB Output is correct
10 Correct 535 ms 202764 KB Output is correct
11 Correct 427 ms 184660 KB Output is correct
12 Correct 652 ms 181844 KB Output is correct
13 Correct 370 ms 182352 KB Output is correct
14 Correct 483 ms 183452 KB Output is correct
15 Correct 490 ms 182736 KB Output is correct
16 Correct 508 ms 182656 KB Output is correct
17 Correct 521 ms 182208 KB Output is correct
18 Correct 40 ms 78628 KB Output is correct
19 Correct 40 ms 78672 KB Output is correct
20 Correct 40 ms 78600 KB Output is correct
21 Correct 43 ms 78760 KB Output is correct
22 Correct 40 ms 78672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 78904 KB Output is correct
2 Correct 481 ms 184960 KB Output is correct
3 Correct 629 ms 182376 KB Output is correct
4 Correct 495 ms 204408 KB Output is correct
5 Correct 505 ms 183248 KB Output is correct
6 Correct 513 ms 184384 KB Output is correct
7 Correct 499 ms 183004 KB Output is correct
8 Correct 424 ms 182700 KB Output is correct
9 Correct 496 ms 205884 KB Output is correct
10 Correct 535 ms 202764 KB Output is correct
11 Correct 427 ms 184660 KB Output is correct
12 Correct 652 ms 181844 KB Output is correct
13 Correct 370 ms 182352 KB Output is correct
14 Correct 483 ms 183452 KB Output is correct
15 Correct 490 ms 182736 KB Output is correct
16 Correct 508 ms 182656 KB Output is correct
17 Correct 521 ms 182208 KB Output is correct
18 Correct 40 ms 78628 KB Output is correct
19 Correct 40 ms 78672 KB Output is correct
20 Correct 40 ms 78600 KB Output is correct
21 Correct 43 ms 78760 KB Output is correct
22 Correct 40 ms 78672 KB Output is correct
23 Correct 39 ms 78676 KB Output is correct
24 Correct 461 ms 185012 KB Output is correct
25 Correct 709 ms 182660 KB Output is correct
26 Correct 546 ms 204628 KB Output is correct
27 Correct 529 ms 183320 KB Output is correct
28 Correct 600 ms 183580 KB Output is correct
29 Correct 559 ms 184352 KB Output is correct
30 Correct 467 ms 182852 KB Output is correct
31 Correct 503 ms 206172 KB Output is correct
32 Correct 602 ms 202720 KB Output is correct
33 Correct 461 ms 184900 KB Output is correct
34 Correct 706 ms 181700 KB Output is correct
35 Correct 38 ms 78684 KB Output is correct
36 Correct 38 ms 78828 KB Output is correct
37 Correct 38 ms 78676 KB Output is correct
38 Correct 38 ms 78700 KB Output is correct
39 Correct 39 ms 78672 KB Output is correct
40 Correct 37 ms 78736 KB Output is correct
41 Correct 39 ms 78576 KB Output is correct
42 Correct 37 ms 78676 KB Output is correct
43 Correct 38 ms 78672 KB Output is correct
44 Correct 42 ms 78672 KB Output is correct
45 Correct 38 ms 78572 KB Output is correct
46 Correct 39 ms 78556 KB Output is correct
47 Correct 38 ms 78684 KB Output is correct
48 Correct 37 ms 78672 KB Output is correct
49 Correct 38 ms 78680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 78552 KB Output is correct
2 Correct 602 ms 203380 KB Output is correct
3 Correct 1728 ms 202748 KB Output is correct
4 Correct 840 ms 203204 KB Output is correct
5 Correct 1439 ms 181596 KB Output is correct
6 Correct 1339 ms 182088 KB Output is correct
7 Correct 962 ms 182632 KB Output is correct
8 Correct 451 ms 182260 KB Output is correct
9 Correct 600 ms 203332 KB Output is correct
10 Correct 1685 ms 202808 KB Output is correct
11 Correct 846 ms 182332 KB Output is correct
12 Execution timed out 4061 ms 181912 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 38 ms 78672 KB Output is correct
2 Correct 616 ms 205908 KB Output is correct
3 Correct 1791 ms 202832 KB Output is correct
4 Correct 883 ms 204368 KB Output is correct
5 Correct 1586 ms 183248 KB Output is correct
6 Correct 1507 ms 183540 KB Output is correct
7 Correct 1018 ms 182404 KB Output is correct
8 Correct 441 ms 182804 KB Output is correct
9 Correct 650 ms 205908 KB Output is correct
10 Correct 1796 ms 203252 KB Output is correct
11 Correct 859 ms 184948 KB Output is correct
12 Execution timed out 4057 ms 182012 KB Time limit exceeded
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 40 ms 78676 KB Output is correct
2 Correct 40 ms 78632 KB Output is correct
3 Correct 41 ms 78700 KB Output is correct
4 Correct 43 ms 81236 KB Output is correct
5 Correct 47 ms 81040 KB Output is correct
6 Correct 41 ms 80980 KB Output is correct
7 Correct 41 ms 80980 KB Output is correct
8 Correct 41 ms 81216 KB Output is correct
9 Correct 40 ms 78692 KB Output is correct
10 Correct 40 ms 78676 KB Output is correct
11 Correct 45 ms 78772 KB Output is correct
12 Correct 40 ms 78672 KB Output is correct
13 Correct 40 ms 78776 KB Output is correct
14 Correct 40 ms 78676 KB Output is correct
15 Correct 41 ms 78680 KB Output is correct
16 Correct 42 ms 78748 KB Output is correct
17 Correct 43 ms 78672 KB Output is correct
18 Correct 40 ms 78672 KB Output is correct
19 Correct 40 ms 78684 KB Output is correct
20 Correct 41 ms 78988 KB Output is correct
21 Correct 40 ms 78588 KB Output is correct
22 Correct 40 ms 78672 KB Output is correct
23 Correct 39 ms 78824 KB Output is correct
24 Correct 39 ms 78676 KB Output is correct
25 Correct 41 ms 78708 KB Output is correct
26 Correct 40 ms 78568 KB Output is correct
27 Correct 40 ms 78676 KB Output is correct
28 Correct 41 ms 78680 KB Output is correct
29 Correct 40 ms 78904 KB Output is correct
30 Correct 481 ms 184960 KB Output is correct
31 Correct 629 ms 182376 KB Output is correct
32 Correct 495 ms 204408 KB Output is correct
33 Correct 505 ms 183248 KB Output is correct
34 Correct 513 ms 184384 KB Output is correct
35 Correct 499 ms 183004 KB Output is correct
36 Correct 424 ms 182700 KB Output is correct
37 Correct 496 ms 205884 KB Output is correct
38 Correct 535 ms 202764 KB Output is correct
39 Correct 427 ms 184660 KB Output is correct
40 Correct 652 ms 181844 KB Output is correct
41 Correct 370 ms 182352 KB Output is correct
42 Correct 483 ms 183452 KB Output is correct
43 Correct 490 ms 182736 KB Output is correct
44 Correct 508 ms 182656 KB Output is correct
45 Correct 521 ms 182208 KB Output is correct
46 Correct 40 ms 78628 KB Output is correct
47 Correct 40 ms 78672 KB Output is correct
48 Correct 40 ms 78600 KB Output is correct
49 Correct 43 ms 78760 KB Output is correct
50 Correct 40 ms 78672 KB Output is correct
51 Correct 39 ms 78676 KB Output is correct
52 Correct 461 ms 185012 KB Output is correct
53 Correct 709 ms 182660 KB Output is correct
54 Correct 546 ms 204628 KB Output is correct
55 Correct 529 ms 183320 KB Output is correct
56 Correct 600 ms 183580 KB Output is correct
57 Correct 559 ms 184352 KB Output is correct
58 Correct 467 ms 182852 KB Output is correct
59 Correct 503 ms 206172 KB Output is correct
60 Correct 602 ms 202720 KB Output is correct
61 Correct 461 ms 184900 KB Output is correct
62 Correct 706 ms 181700 KB Output is correct
63 Correct 38 ms 78684 KB Output is correct
64 Correct 38 ms 78828 KB Output is correct
65 Correct 38 ms 78676 KB Output is correct
66 Correct 38 ms 78700 KB Output is correct
67 Correct 39 ms 78672 KB Output is correct
68 Correct 37 ms 78736 KB Output is correct
69 Correct 39 ms 78576 KB Output is correct
70 Correct 37 ms 78676 KB Output is correct
71 Correct 38 ms 78672 KB Output is correct
72 Correct 42 ms 78672 KB Output is correct
73 Correct 38 ms 78572 KB Output is correct
74 Correct 39 ms 78556 KB Output is correct
75 Correct 38 ms 78684 KB Output is correct
76 Correct 37 ms 78672 KB Output is correct
77 Correct 38 ms 78680 KB Output is correct
78 Correct 37 ms 78552 KB Output is correct
79 Correct 602 ms 203380 KB Output is correct
80 Correct 1728 ms 202748 KB Output is correct
81 Correct 840 ms 203204 KB Output is correct
82 Correct 1439 ms 181596 KB Output is correct
83 Correct 1339 ms 182088 KB Output is correct
84 Correct 962 ms 182632 KB Output is correct
85 Correct 451 ms 182260 KB Output is correct
86 Correct 600 ms 203332 KB Output is correct
87 Correct 1685 ms 202808 KB Output is correct
88 Correct 846 ms 182332 KB Output is correct
89 Execution timed out 4061 ms 181912 KB Time limit exceeded
90 Halted 0 ms 0 KB -