Submission #964288

#TimeUsernameProblemLanguageResultExecution timeMemory
964288rolandpetreanSprinkler (JOI22_sprinkler)C++17
38 / 100
264 ms50768 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define int ll

#define endl '\n'
#define pb push_back
using pi = array<int, 2>;

const int N = 2e5 + 5;
vector<int> adj[N];
int par[N];

int h[N], wch[2][N];

void dfs(int u) {
  for (int v : adj[u]) {
    if (v == par[u]) continue;
    par[v] = u;
    dfs(v);
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int n, l;
  cin >> n >> l;
  
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    adj[u].pb(v);
    adj[v].pb(u);
  }
  for (int i = 0; i <= n; ++i) wch[0][i] = wch[1][i] = 1;
  
  dfs(1);
  
  for (int i = 1; i <= n; ++i) cin >> h[i];
  
  int q;
  cin >> q;
  
  while (q--) {
    int t;
    cin >> t;
    
    if (t == 1) {
      int u, d, w;
      cin >> u >> d >> w;
      
      assert(d <= 2);
      
      auto mult = [&](int& var) {
        var = var * w % l;
      };
      
      int p = par[u];
      if (d == 0) {
        mult(h[u]);
      } else if (d == 1) {
        mult(h[u]);
        mult(h[p]);
        mult(wch[0][u]);
      } else {
        mult(h[p]);
        mult(wch[0][p]); // all children of p
        mult(h[par[p]]);
        mult(wch[0][u]);
        mult(wch[1][u]);
      }
    } else {
      int u;
      cin >> u;
      
      int ans = h[u];
      ans = ans * wch[0][par[u]] % l;
      ans = ans * wch[1][par[par[u]]] % l;
      cout << ans << endl;
    }
  }
}
#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...