Submission #779219

# Submission time Handle Problem Language Result Execution time Memory
779219 2023-07-11T09:10:10 Z boris_mihov Sprinkler (JOI22_sprinkler) C++17
41 / 100
4000 ms 128952 KB
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <random>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>

typedef long long llong;
const int MAXN = 200000 + 10;
const int MAXD = 50;
const int INF = 1e9;

int n, MOD, q;
struct SegmentTree
{
    int tree[4*MAXN];
    void build(int l, int r, int node, int vals[])
    {
        if (l == r)
        {
            tree[node] = vals[l];
            return;
        }

        int mid = (l + r) / 2;
        build(l, mid, 2*node, vals);
        build(mid + 1, r, 2*node + 1, vals);
        tree[node] = 1;
    }

    void push(int node, int l, int r)
    {
        if (tree[node] == 1)
        {
            return;
        }

        if (l < r)
        {
            tree[2*node] = (1LL * tree[2*node] * tree[node]) % MOD;
            tree[2*node + 1] = (1LL * tree[2*node + 1] * tree[node]) % MOD;
            tree[node] = 1;
        }
    }

    void update(int l, int r, int node, int queryL, int queryR, int val)
    {
        // std::cout << "update: " << l << ' ' << r << ' ' << node  << '\n' << std::flush;
        push(node, l, r);
        if (queryL <= l && r <= queryR)
        {
            tree[node] = (1LL * tree[node] * val) % MOD;
            push(node, l, r);
            return;
        }

        int mid = (l + r) / 2;
        if (queryL <= mid) update(l, mid, 2*node, queryL, queryR, val);
        if (mid + 1 <= queryR) update(mid + 1, r, 2*node + 1, queryL, queryR, val);
    }

    int query(int l, int r, int node, int pos)
    {
        push(node, l, r);
        if (l == r)
        {
            return tree[node];
        }

        int mid = (l + r) / 2;
        if (pos <= mid) return query(l, mid, 2*node, pos);
        else return query(mid + 1, r, 2*node + 1, pos);
    }

    void build(int vals[])
    {
        build(0, n - 1, 1, vals);
    }

    void update(int l, int r, int val)
    {
        update(0, n - 1, 1, l, r, val);
    }

    int query(int pos)
    {
        return query(0, n - 1, 1, pos);
    }
};

int h[MAXN];
int p[MAXN];
int d[MAXN];
int maxD[MAXN];
int vals[MAXN];
SegmentTree tree;
std::queue <int> queue;
std::vector <int> tour;
std::vector <int> byLevel[MAXN];
std::vector <int> g[MAXN];
int in[MAXN], out[MAXN];
int tour2IN[MAXN];
int tourIN[MAXN];
int timer;

int leftIN[MAXN][MAXD];
int rightIN[MAXN][MAXD];

bool inSubtree(int x, int y)
{
    return in[y] <= in[x] && in[x] <= out[y];
}

void dfs(int node, int par)
{
    p[node] = par;
    d[node] = d[par] + 1;
    in[node] = ++timer;
    maxD[node] = d[node];

    for (const int &u : g[node])
    {
        if (par == u)
        {
            continue;
        }

        dfs(u, node);
        maxD[node] = std::max(maxD[node], maxD[u]);
    }

    out[node] = timer;
}

void dfsIN(int node)
{
    leftIN[node][0] = tour2IN[node];
    rightIN[node][0] = tour2IN[node];

    for (const int &u : g[node])
    {
        if (u == p[node])
        {
            continue;
        }

        dfsIN(u);
    }

    for (int i = 1 ; i < MAXD ; ++i)
    {
        leftIN[node][i] = INF;
        rightIN[node][i] = -INF;
        for (const int &u : g[node])
        {
            if (u == p[node])
            {
                continue;
            }

            leftIN[node][i] = std::min(leftIN[node][i], leftIN[u][i - 1]);
            rightIN[node][i] = std::max(rightIN[node][i], rightIN[u][i - 1]);
        }
    }
}

void updateLevel(int node, int depth, int val)
{
    assert(depth >= d[node]);
    int l = leftIN[node][depth - d[node]];
    int r = rightIN[node][depth - d[node]];
    if (l <= r)
    {
        tree.update(tourIN[byLevel[depth][l]], tourIN[byLevel[depth][r]], val);
    }
}

int recurse(int node, int depth, int w)
{
    if (node == 0)
    {
        return 0;
    }

    if (depth == 0)
    {
        tree.update(tourIN[node], tourIN[node], w);
        return d[node];
    }

    int res = std::max(recurse(p[node], depth - 1, w) + 1, d[node]);
    for (int i = res ; i <= std::min(maxD[node], d[node] + depth) ; ++i)
    {
        updateLevel(node, i, w);
    }

    return d[node] + depth;
}

void solve()
{
    dfs(1, 0);
    queue.push(1);

    while (queue.size())
    {
        int top = queue.front();
        queue.pop();

        byLevel[d[top]].push_back(top);
        tour.push_back(top);
        tourIN[top] = tour.size() - 1;
        tour2IN[top] = byLevel[d[top]].size() - 1;
        // std::cout << "HERE: " << top << ' ' << d[top] << ' ' << tourIN[top] << '\n';

        for (const int &u : g[top])
        {
            if (u == p[top])
            {
                continue;
            }

            queue.push(u);
        }
    }

    dfsIN(1);
    for (int i = 1 ; i <= n ; ++i)
    {
        vals[tourIN[i]] = h[i];
    }

    tree.build(vals);
    std::cin >> q;

    for (int i = 1 ; i <= q ; ++i)
    {
        int qType;
        std::cin >> qType;

        if (qType == 2)
        {
            int node;
            std::cin >> node;
            std::cout << tree.query(tourIN[node]) << '\n' << std::flush;
            continue;
        }

        assert(qType == 1);
        int node, depth, w;
        std::cin >> node >> depth >> w;
        recurse(node, depth, w);
    }
}

void input()
{
    std::cin >> n >> MOD;
    for (int i = 2 ; i <= n ; ++i)
    {
        int u, v;
        std::cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> h[i];
    }
}

void fastIOI()
{
    std::ios_base :: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}

/*
4 7
1 2
2 3
3 4
1
1
1
1
5
1 2 1 2
2 1
2 2
2 3
2 4
*/
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 4 ms 9756 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 8 ms 10228 KB Output is correct
5 Correct 8 ms 10196 KB Output is correct
6 Correct 6 ms 10248 KB Output is correct
7 Correct 7 ms 10280 KB Output is correct
8 Correct 6 ms 10196 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9668 KB Output is correct
16 Correct 5 ms 9796 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 5 ms 9684 KB Output is correct
26 Correct 6 ms 9684 KB Output is correct
27 Correct 6 ms 9684 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9776 KB Output is correct
2 Correct 763 ms 108996 KB Output is correct
3 Correct 709 ms 105828 KB Output is correct
4 Correct 742 ms 120124 KB Output is correct
5 Correct 774 ms 107372 KB Output is correct
6 Correct 766 ms 107120 KB Output is correct
7 Correct 661 ms 107496 KB Output is correct
8 Correct 901 ms 108664 KB Output is correct
9 Correct 880 ms 128952 KB Output is correct
10 Correct 813 ms 123976 KB Output is correct
11 Correct 873 ms 108856 KB Output is correct
12 Correct 676 ms 105840 KB Output is correct
13 Correct 784 ms 106724 KB Output is correct
14 Correct 745 ms 106920 KB Output is correct
15 Correct 769 ms 106376 KB Output is correct
16 Correct 697 ms 106416 KB Output is correct
17 Correct 676 ms 106440 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9776 KB Output is correct
2 Correct 763 ms 108996 KB Output is correct
3 Correct 709 ms 105828 KB Output is correct
4 Correct 742 ms 120124 KB Output is correct
5 Correct 774 ms 107372 KB Output is correct
6 Correct 766 ms 107120 KB Output is correct
7 Correct 661 ms 107496 KB Output is correct
8 Correct 901 ms 108664 KB Output is correct
9 Correct 880 ms 128952 KB Output is correct
10 Correct 813 ms 123976 KB Output is correct
11 Correct 873 ms 108856 KB Output is correct
12 Correct 676 ms 105840 KB Output is correct
13 Correct 784 ms 106724 KB Output is correct
14 Correct 745 ms 106920 KB Output is correct
15 Correct 769 ms 106376 KB Output is correct
16 Correct 697 ms 106416 KB Output is correct
17 Correct 676 ms 106440 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 4 ms 9684 KB Output is correct
24 Correct 788 ms 108912 KB Output is correct
25 Correct 806 ms 105792 KB Output is correct
26 Correct 758 ms 126448 KB Output is correct
27 Correct 703 ms 107404 KB Output is correct
28 Correct 657 ms 107200 KB Output is correct
29 Correct 710 ms 107324 KB Output is correct
30 Correct 811 ms 108580 KB Output is correct
31 Correct 834 ms 122488 KB Output is correct
32 Correct 934 ms 124064 KB Output is correct
33 Correct 826 ms 109012 KB Output is correct
34 Correct 754 ms 105700 KB Output is correct
35 Correct 5 ms 9684 KB Output is correct
36 Correct 5 ms 9792 KB Output is correct
37 Correct 5 ms 9684 KB Output is correct
38 Correct 6 ms 9684 KB Output is correct
39 Correct 5 ms 9684 KB Output is correct
40 Correct 5 ms 9716 KB Output is correct
41 Correct 5 ms 9684 KB Output is correct
42 Correct 5 ms 9684 KB Output is correct
43 Correct 5 ms 9684 KB Output is correct
44 Correct 5 ms 9684 KB Output is correct
45 Correct 5 ms 9684 KB Output is correct
46 Correct 5 ms 9684 KB Output is correct
47 Correct 5 ms 9684 KB Output is correct
48 Correct 5 ms 9684 KB Output is correct
49 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 1556 ms 125888 KB Output is correct
3 Execution timed out 4054 ms 119988 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9684 KB Output is correct
2 Correct 1469 ms 121576 KB Output is correct
3 Execution timed out 4061 ms 115280 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 4 ms 9756 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 8 ms 10228 KB Output is correct
5 Correct 8 ms 10196 KB Output is correct
6 Correct 6 ms 10248 KB Output is correct
7 Correct 7 ms 10280 KB Output is correct
8 Correct 6 ms 10196 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 5 ms 9684 KB Output is correct
13 Correct 5 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9668 KB Output is correct
16 Correct 5 ms 9796 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 5 ms 9684 KB Output is correct
19 Correct 5 ms 9684 KB Output is correct
20 Correct 5 ms 9684 KB Output is correct
21 Correct 5 ms 9684 KB Output is correct
22 Correct 5 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 5 ms 9684 KB Output is correct
26 Correct 6 ms 9684 KB Output is correct
27 Correct 6 ms 9684 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 5 ms 9776 KB Output is correct
30 Correct 763 ms 108996 KB Output is correct
31 Correct 709 ms 105828 KB Output is correct
32 Correct 742 ms 120124 KB Output is correct
33 Correct 774 ms 107372 KB Output is correct
34 Correct 766 ms 107120 KB Output is correct
35 Correct 661 ms 107496 KB Output is correct
36 Correct 901 ms 108664 KB Output is correct
37 Correct 880 ms 128952 KB Output is correct
38 Correct 813 ms 123976 KB Output is correct
39 Correct 873 ms 108856 KB Output is correct
40 Correct 676 ms 105840 KB Output is correct
41 Correct 784 ms 106724 KB Output is correct
42 Correct 745 ms 106920 KB Output is correct
43 Correct 769 ms 106376 KB Output is correct
44 Correct 697 ms 106416 KB Output is correct
45 Correct 676 ms 106440 KB Output is correct
46 Correct 5 ms 9684 KB Output is correct
47 Correct 6 ms 9684 KB Output is correct
48 Correct 5 ms 9684 KB Output is correct
49 Correct 5 ms 9684 KB Output is correct
50 Correct 5 ms 9684 KB Output is correct
51 Correct 4 ms 9684 KB Output is correct
52 Correct 788 ms 108912 KB Output is correct
53 Correct 806 ms 105792 KB Output is correct
54 Correct 758 ms 126448 KB Output is correct
55 Correct 703 ms 107404 KB Output is correct
56 Correct 657 ms 107200 KB Output is correct
57 Correct 710 ms 107324 KB Output is correct
58 Correct 811 ms 108580 KB Output is correct
59 Correct 834 ms 122488 KB Output is correct
60 Correct 934 ms 124064 KB Output is correct
61 Correct 826 ms 109012 KB Output is correct
62 Correct 754 ms 105700 KB Output is correct
63 Correct 5 ms 9684 KB Output is correct
64 Correct 5 ms 9792 KB Output is correct
65 Correct 5 ms 9684 KB Output is correct
66 Correct 6 ms 9684 KB Output is correct
67 Correct 5 ms 9684 KB Output is correct
68 Correct 5 ms 9716 KB Output is correct
69 Correct 5 ms 9684 KB Output is correct
70 Correct 5 ms 9684 KB Output is correct
71 Correct 5 ms 9684 KB Output is correct
72 Correct 5 ms 9684 KB Output is correct
73 Correct 5 ms 9684 KB Output is correct
74 Correct 5 ms 9684 KB Output is correct
75 Correct 5 ms 9684 KB Output is correct
76 Correct 5 ms 9684 KB Output is correct
77 Correct 5 ms 9684 KB Output is correct
78 Correct 4 ms 9684 KB Output is correct
79 Correct 1556 ms 125888 KB Output is correct
80 Execution timed out 4054 ms 119988 KB Time limit exceeded
81 Halted 0 ms 0 KB -