제출 #674605

#제출 시각아이디문제언어결과실행 시간메모리
674605someoneSprinkler (JOI22_sprinkler)C++14
100 / 100
861 ms99536 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int M = 2e5 + 42, N = 2 * M, T = 19, INF = 1e18 + 42, MOD = 1e9 + 7;

vector<int> adj[M];
int n, l, q, id = 0, h[M], pere[M], prof[M], ch[M][42];

void dfs(int i, int pre, int depth = 0) {
    pere[i] = pre;
    prof[i] = depth;
    for(int j : adj[i])
        if(j != pre)
            dfs(j, i, depth+1);
}


void update(int i, int d, int prod) {
    do {
        if(i == 0 || d < 2) {
            for(int j = 0; j <= d; j++)
                ch[i][j] = ch[i][j] * prod % l;
        } else {
            ch[i][d] = ch[i][d] * prod % l;
            ch[i][d-1] = ch[i][d-1] * prod % l;
        }
        d--;
        i = pere[i];
    } while(i != -1 && d >= 0);
}

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

    cin >> n >> l;
    for(int i = 0; i < M; i++)
        for(int j = 0; j < 42; j++)
            ch[i][j] = 1;
    for(int i = 0; i < n-1; i++) {
        int a, b; cin >> a >> b;
        a--, b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(0, -1);

    for(int i = 0; i < n; i++)
        cin >> h[i];

    cin >> q;
    for(int i = 0; i < q; i++) {
        int typ; cin >> typ;
        if(typ == 1) {
            int x, dist, prod;
            cin >> x >> dist >> prod;
            update(x-1, dist, prod);
        } else {
            int x;
            cin >> x;
            int j = x-1, d = 0, ans = h[j];
            while(j >= 0 && d < 41) {
                ans = ans * ch[j][d] % l;
                j = pere[j];
                d++;
            }
            cout << ans << '\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...