제출 #882882

#제출 시각아이디문제언어결과실행 시간메모리
882882NK_Sprinkler (JOI22_sprinkler)C++17
100 / 100
627 ms110420 KiB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back

using ll = long long;
template<class T> using V = vector<T>;
using vi = V<int>;

const int DEP = 45;
using AR = array<ll, 45>;


int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N, L; cin >> N >> L;

	V<vi> adj(N);
	for(int i = 0; i < N - 1; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(v);
		adj[v].pb(u);
	}	

	vi H(N); for(auto& x : H) cin >> x;

	V<AR> W(N); vi par(N, -1); 
	function<void(int)> gen = [&](int u) {
		W[u].fill(1);
		for(auto& v : adj[u]) if (v != par[u]) {
			par[v] = u; gen(v);
		}
	};

	gen(0);

	int Q; cin >> Q;
	for(int q = 0; q < Q; q++) {
		int t; cin >> t;
		if (t == 1) {
			int u, d; ll x; cin >> u >> d >> x; --u;
			while(u != -1 && d >= 0) {
				W[u][d] = (W[u][d] * x) % L;
				// cout << u << " " << d << " --> " << W[u][d] << endl;
				u = par[u], d--;
			}
		} else {
			int u, d = 0; cin >> u; --u;

			ll mul = H[u];
			while(u != -1 && d < DEP) {
				if (par[u] == -1) { // all ones that have pivot points above bounds
					for(int dx = d; dx < DEP; dx++) {
						mul = (mul * W[u][dx]) % L;
					}
				} else {
					mul = (mul * W[u][d]) % L;
					if (d + 1 < DEP) mul = (mul * W[u][d + 1]) % L;
				}

				// cout << "Q: " << u << " " << d << " => " << mul << endl;
				u = par[u]; d++;
			}

			cout << mul << nl;
		}
	}

	exit(0-0);
}
#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...