This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
const int N = 2e5+5, D = 45;
ll dp[N + D][D], p[N];
vector<int> g[N];
void dfs(int s, int e = -1) {
    for (auto u : g[s]) {
        if (u == e) continue;
        p[u] = s;
        dfs(u, s);
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, L;
    cin >> n >> L;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].pb(v); g[v].pb(u);
    }
    p[1] = n+1;
    for (int i = n+1; i < n + D; ++i)  p[i] = i+1;
    dfs(1);
    for (int i = 1; i <= n; ++i)    cin >> dp[i][0];
    for (int i = 1; i <= n+D; ++i) {
        for (int j = 1; j < D; ++j) dp[i][j] = 1;
    }
    int q;
    cin >> q;
    while (q--) {
        int t;
        cin >> t;
        if (t == 1) {
            int u, d, w;
            cin >> u >> d >> w;
            while (u < n + D && d > -1) {
                dp[u][d] = (dp[u][d] * w) % L;
                if (d)  dp[u][d-1] = (dp[u][d-1] * w) % L;
                --d; u = p[u];
            }
        }
        if (t == 2) {
            ll ans = 1, u;
            cin >> u;
            for (int i = 0; i < D && u; ++i) {
                ans *= dp[u][i];
                ans %= L;
                u = p[u];
            }
            cout << ans << '\n';
        }
        
    }
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |