Submission #722133

#TimeUsernameProblemLanguageResultExecution timeMemory
722133fatemetmhrSprinkler (JOI22_sprinkler)C++17
100 / 100
844 ms65612 KiB
// Heiy ... key mn na omid shodam azat ke in beshe dovomin bar?

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define all(x) x.begin(), x.end()
#define pb     push_back
#define se     second
#define fi     first

const int maxn5 = 2e5 + 10;

int mod, av[maxn5][43], par[maxn5], a[maxn5];
vector <int> adj[maxn5];

void dfs(int v){
    fill(av[v], av[v] + 43, 1);
    for(auto u : adj[v]) if(u != par[v]){
        par[u] = v;
        dfs(u);
    }
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int n; cin >> n >> mod;
    for(int i = 0; i < n - 1; i++){
        int a, b; cin >> a >> b;
        a--; b--;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    for(int i = 0; i < n; i++)
        cin >> a[i];
    par[0] = -1;
    dfs(0);

    int q; cin >> q;
    while(q--){
        int ty, v; cin >> ty >> v;
        v--;
        if(ty == 1){
            int d;
            ll w; 
            cin >> d >> w;
            while(d >= 0){
                av[v][d] = av[v][d] * w % mod;
                if(d)
                    av[v][d - 1] = av[v][d - 1] * w % mod;
                if(!v){
                    for(int i = 0; i < d - 1; i++)
                        av[v][i] = av[v][i] * w % mod;
                    break;
                }
                v = par[v];
                d--;
            }
            //cout << "here " << av[0][0] << '\n';
        }
        else{
            ll ans = a[v];
            int done = 0;
            while(v != -1 && done <= 40){
                ans = ans * av[v][done] % mod;
                //cout << "aha " << v << ' ' << done << ' ' << av[v][done] << '\n';
                v = par[v];
                done++;
            }
            cout << ans << '\n';
        }
    }

}

/*
4 7
1 2
2 3
3 4
1
1
1
1
2
1 2 1 2
2 1
1 1 0 2
*/
#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...