Submission #892281

#TimeUsernameProblemLanguageResultExecution timeMemory
892281vjudge1Sprinkler (JOI22_sprinkler)C++17
9 / 100
1133 ms64196 KiB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

const int N = 2e5 + 1;

int L, n;

struct SegTree{
    int T[N * 4], lazy[N * 4];
    SegTree(){
        for ( int i = 0; i < N * 4; i++ ){
            T[i] = lazy[i] = 1;
        }
    }
    void push(int v, int l, int r){
        if ( l != r ){
            lazy[v * 2] = lazy[v * 2] * lazy[v] % L;
            lazy[v * 2 + 1] = lazy[v * 2 + 1] * lazy[v] % L;
        }
        T[v] = T[v] * lazy[v] % L;
        lazy[v] = 1;
    }
    void upd(int v, int l, int r, int tl, int tr, int val){
        if ( tl > r or tr < l ){
            return;
        }
        push(v, l, r);
        if ( tl <= l && tr >= r ){
            lazy[v] = lazy[v] * val % L;
            push(v, l, r);
            return;
        }
        int md = (l + r) >> 1;
        upd(v * 2 + 1, md + 1, r, tl, tr, val);
        upd(v * 2, l, md, tl, tr, val);
        T[v] = (T[v * 2] * T[v * 2 + 1]) % L;
    };
    void upd(int l, int r, int val){
        upd(1, 0, n - 1, l, r, val);
    }
    int get(int v, int l, int r, int tl, int tr){
        push(v, l, r);
        if ( l > tr or r < tl ){
            return 1;
        }
        if ( tl <= l && tr >= r ){
            return T[v];
        }
        int md = (l + r) >> 1;
        return (get(v * 2, l, md, tl, tr) * get(v * 2 + 1, md + 1, r, tl, tr)) % L;
    }
    int get(int l, int r){
        return get(1, 0, n - 1, l, r);
    }
} seg;

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> L;
    vector <vector<int>> G(n);
    for ( int i = 0; i + 1 < n; i++ ){
        int u, v; cin >> u >> v;
        --u, --v;
        G[u].pb(v), G[v].pb(u);
    }
    vector <int> h(n);
    for ( auto &u: h ) cin >> u;
    int q; cin >> q;
    vector <ar<int,4>> Q(q);
    bool Qflag = true;
    for ( auto &[t, x, d, w]: Q ){
        cin >> t >> x;
        --x;
        if ( t == 1 ){
            cin >> d >> w;
            Qflag &= (d <= 2);
        }
    }
    if ( false && min(n, q) <= 1000 ){ // subtask #1
        for ( auto &[t, x, d, w]: Q ){
            if ( t == 1 ){
                auto dfs = [&](auto dfs, int u, int p, int D) -> void{
                    h[u] = h[u] * w % L;
                    if ( D != d ){
                        for ( auto &v: G[u] ){
                            if ( v != p ){
                                dfs(dfs, v, u, D + 1);
                            }
                        }
                    }
                };
                dfs(dfs, x, -1, 0);
            } else cout << h[x] << ln;
        }
        return 0;
    }
    if ( Qflag ){ // subtask #2, 3
        vector <int> fa(n), id(n);
        vector <ar<int,2>> ch(n, {-1, -1});
        int ct = 0;
        auto dfs = [&](auto dfs, int u, int p) -> void{
            fa[u] = p;
            for ( auto &v: G[u] ){
                if ( v != p ){
                    id[v] = ++ct;
                    if ( ch[u][0] == -1 ){
                        ch[u][0] = v;
                    } ch[u][1] = v;
                }
            }
            for ( auto &v: G[u] ){
                if ( v != p ){
                    dfs(dfs, v, u);
                }
            }
        };
        dfs(dfs, 0, -1);
        for ( int i = 0; i < n; i++ ){
            seg.upd(id[i], id[i], h[i]);
        }
        for ( auto &[t, x, d, w]: Q ){
            if ( t == 1 ){
                seg.upd(id[x], id[x], w);
                if ( d > 0 ){
                    if ( fa[x] != -1 ){
                        seg.upd(id[fa[x]], id[fa[x]], w);
                    }
                    if ( ch[x][0] != -1 ){
                        seg.upd(id[ch[x][0]], id[ch[x][1]], w);
                    }
                }
            } else{
                cout << seg.get(id[x], id[x]) << ln;
            }
        }
    }

    cout << '\n';
}

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