이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define sz(x) int(x.size())
const int mx = 200'000;
const int lg = 19;
const int maxD = 40;
int N;
ll L;
vll H(1+mx);
vi edge[1+mx];
vi depth(1+mx, 1);
vi par(1+mx, 1);
ll mul(ll a, ll b)
{
	return (a*b)%L;
}
void dfs(int u, int p)
{
	par[u] = p;
	for(int v : edge[u])
	{
		if(v == p) continue;
		depth[v] = depth[u] + 1;
		dfs(v, u);
	}
}
vvll prop(1+mx, vll(1+maxD, 1)); //propagate this value in prop[u][d] at depth = depth[u]+d
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N >> L;
	for(int e = 1; e <= N-1; e++)
	{
		int a, b;
		cin >> a >> b;
		edge[a].push_back(b);
		edge[b].push_back(a);
	}
	for(int i = 1; i <= N; i++) cin >> H[i];
	dfs(1, 0);
	
	int Q;
	cin >> Q;
	for(int q = 1; q <= Q; q++)
	{
		int t;
		cin >> t;
		if(t == 1)
		{
			int x, d;
			ll w;
			cin >> x >> d >> w;
			int y = x;
			for(int z = 0; z <= d; z++) //assign every level to the HIGHEST ancestor that can handle it.
			{
				if(y == 0) break;
				if(z >= 1)
					y = par[y];
				int dlo = depth[y];
				int dhi = depth[y] + (d - (depth[x] - depth[y]));
				if(z < d && y != 1)
					dlo = max(dlo, (depth[y]-1) + d - (depth[x] - (depth[y]-1)) + 1);
				dlo -= depth[y];
				dhi -= depth[y];
				for(int v = dlo; v <= dhi; v++)
					prop[y][v] = mul(prop[y][v], w);
			}
		}
		else
		{
			int x;
			cin >> x;
			ll res = H[x];
			for(int y = x; y != 0 && depth[x] - depth[y] <= 40; y = par[y])
				res = mul(res, prop[y][depth[x] - depth[y]]);
			cout << res << '\n';
		}
	}
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |