Submission #1268432

#TimeUsernameProblemLanguageResultExecution timeMemory
1268432nguynSprinkler (JOI22_sprinkler)C++20
100 / 100
615 ms54644 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define F first
#define S second
#define pb push_back
#define pii pair<int, int>

const int N = 2e5 + 55;

vector<int> g[N];
int h[N];
int mul[N][44];
int n, mod, q;
int par[N];

void dfs(int u, int p) {
    for (int v : g[u]) {
        if (v == p) continue;
        par[v] = u;
        dfs(v, u);
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> mod;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    for (int i = 1; i <= n; i++) {
        cin >> h[i];
    }
    for (int i = 1; i <= 40; i++) {
        g[n + i].pb(n + i - 1);
        g[n + i - 1].pb(n + i);
    }
    dfs(n + 40, 0);
    cin >> q;
    for (int i = 1; i <= n + 40; i++) {
        for (int j = 0; j < 44; j++) mul[i][j] = 1;
    }
    for (int i = 1; i <= q; i++) {
        int type;
        cin >> type;
        if (type == 1) {
            int x, d, w;
            cin >> x >> d >> w;
            while(d >= 0 && x != 0) {
                mul[x][d] = 1ll * mul[x][d] * w % mod;
                d--;
                if (d >= 0) {
                    mul[x][d] = 1ll * mul[x][d] * w % mod;
                }
                x = par[x];
            }
        }
        if (type == 2) {
            int x;
            cin >> x;
            int res = h[x];
            int d = 0;
            while(x != 0 && d <= 40) {
                res = 1ll * res * mul[x][d] % mod;
                d++;
                x = par[x];
            }
            cout << res << '\n';
        }
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...