Submission #1247601

#TimeUsernameProblemLanguageResultExecution timeMemory
1247601lrnnzSprinkler (JOI22_sprinkler)C++20
100 / 100
708 ms92572 KiB
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <queue>
#include <random>
using namespace std;

#define all(a) (a).begin(), (a).end()
#define ll long long
#define ld long double
#define ui uint64_t
#define cont(set, element) ((set).find(element) != (set).end())
#define pb push_back

#define chmin(x, y) (x = min(x, y)) 
#define chmax(x, y) (x = max(x, y))

/********* DEBUG *********/

template <typename T>
void outvec(const vector<T>& Z){
    for (const T& x : Z)
    cout << x << ' ';
    cout << "\n";
}
void printVariable(const any& var) {
    if (!var.has_value()) {
        cout << "null";
        return;
    }

    if (var.type() == typeid(int)) {
        cout << any_cast<int>(var);
    } else if (var.type() == typeid(double)) {
        cout << any_cast<double>(var);
    } else if (var.type() == typeid(float)) {
        cout << any_cast<float>(var);
    } else if (var.type() == typeid(char)) {
        cout << any_cast<char>(var);
    } else if (var.type() == typeid(bool)) {
        cout << (any_cast<bool>(var) ? "true" : "false");
    } else if (var.type() == typeid(string)) {
        cout << any_cast<string>(var);
    } else if (var.type() == typeid(const char*)) {
        cout << any_cast<const char*>(var);
    } else if (var.type() == typeid(long long)) {
        cout << any_cast<long long>(var);
    } else {
        cout << "[unknown type]";
    }
}


template<typename... Args>
void outval(Args... args) {
    vector<any> variables = {args...};
    
    for (size_t i = 0; i < variables.size(); ++i) {
        printVariable(variables[i]);
        if (i != variables.size() - 1) {
            cout << " ";
        }
    }
    cout << "\n";
}

#define sp << " " <<
#define fi first
#define se second

/********* DEBUG *********/

const ll MOD2 = 1e9 + 7;
const ll MOD = 998244353;
const ll inf = 1e18;

// from, curr, val, time, decreasing
struct update{
    ll from, val, tm;
    bool decreasing;
};


void dfs(ll u, ll p, vector<vector<ll>> &adj, vector<ll> &par){
    par[u] = p;
    for (auto &v : adj[u]){
        if (v == p)
            continue;

        dfs(v, u, adj, par);
    }
}

void solve(){
    ll n, mod;
    cin >> n >> mod;

    vector<vector<ll>> adj(n);
    for (int i = 0; i < n-1; i++){
        ll a,b;
        cin >> a >> b;

        a--; b--;
        adj[a].pb(b);
        adj[b].pb(a);
    }

    vector<vector<ll>> mult(n, vector<ll>(41, 1));
    for (int i = 0; i < n; i++){
        cin >> mult[i][0];
    }

    vector<ll> par(n, -1);
    dfs(0, -1, adj, par);

    ll q;
    cin >> q;
    
    while (q--){
        ll t;
        cin >> t;

        if (t == 1){
            ll x,d,w;
            cin >> x >> d >> w;

            x--;
            while (d >= 0){
                if (par[x] == -1){
                    for (int i = 0; i <= d; i++)
                        mult[x][i] = (mult[x][i] * w) % mod;
                    break;
                }

                mult[x][d] = (mult[x][d] * w) % mod;
                if (d)
                    mult[x][d-1] = (mult[x][d-1] * w) % mod;

                x = par[x];
                d--;
            }
        }
        else{
            ll x, ans = 1;
            cin >> x;
            x--;

            for (int i = 0; i <= 40; i++){
                if (x == -1)
                    break;

                ans = (ans * mult[x][i]) % mod;
                x = par[x];
            }            

            outval(ans);
        }
    }
} 

int main() {    
    ios::sync_with_stdio(0);
    cin.tie(0);

    ll t = 1;
    //cin >> t;
    while (t--) {
        solve();
    }
}    
#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...