Submission #1208360

#TimeUsernameProblemLanguageResultExecution timeMemory
1208360PenguinsAreCuteSprinkler (JOI22_sprinkler)C++17
30 / 100
2078 ms125164 KiB
#include <bits/stdc++.h>
using namespace std;
struct fenwick {
	int n;
	vector<int> fw;
	fenwick(): n(42), fw(42,0) {}
	void up(int x, int u) {
		for(++x;x;x-=(x&(-x)))
			fw[x] += u;
	}
	int qry(int x) {
		int ans = 0;
		for(++x;x<n;x+=(x&(-x)))
			ans += fw[x];
		return ans;
	}
};
const int MAXN = 200'005;
vector<int> adj[MAXN];
int sub[MAXN], maxDist[MAXN], deg[MAXN];
pair<int,int> cent[MAXN];
vector<fenwick> fenw[MAXN];
int dfs0(int x, int p) {
	sub[x] = 1;
	for(auto i: adj[x])
		if(i != p && cent[i].first == -2)
			sub[x] += dfs0(i, x);
	return sub[x];
}
int dfs1(int x, int p, int n) {
	for(auto i: adj[x])
		if(i != p && cent[i].first == -2 && sub[i] > n / 2)
			return dfs1(i, x, n);
	return x;
}
void dfs2(int x, int p) {
	x = dfs1(x, p, dfs0(x, p));
	cent[x] = {p, deg[p]++};
	for(auto i: adj[x])
		if(cent[i].first == -2)
			dfs2(i, x);
}

int par[MAXN], h[MAXN], j[MAXN];
void dfs3(int x, int p) {
	par[x] = p;
	for(auto i: adj[x])
		if(i != p) {
			j[i] = (h[x]+h[j[j[x]]]==h[j[x]]*2?j[j[x]]:x);
			h[i] = h[x] + 1;
			dfs3(i, x);
		}
}
int t(int y, int x) {
	if(h[x] > h[y])
		swap(x, y);
	while(h[y] > h[x])
		y = (h[j[y]]<h[x]?par[y]:j[y]);
	while(x != y) {
		if(j[x] == j[y])
			x = par[x], y = par[y];
		else
			x = j[x], y = j[y];
	}
	return x;
}

int exp(int a, int b, int l) {return (b?1LL * exp(1LL * a * a % l, b / 2, l) * (b & 1 ? a : 1) % l:1);}
int main() {
	int n, l;
	cin >> n >> l;
	for(int i=1,a,b;i<n;i++) {
		cin >> a >> b;
		adj[--a].push_back(--b);
		adj[b].push_back(a);
	}
	int s[n];
	for(int i=0;i<n;i++)
		cin >> s[i];

	fill(cent,cent+n,make_pair(-2,0));
	memset(maxDist,-1,sizeof(maxDist));
	dfs2(0,-1);
	dfs3(0,-1);
	for(int i=0;i<n;i++)
			fenw[i].resize(deg[i] + 1);

	int q;
	cin >> q;
	while(q--) {
		int op;
		cin >> op;
		if(op == 1) {
			int x, d, w;
			cin >> x >> d >> w;
			for(int y=--x,z=-1;~y;) {
				int cur = h[y] + h[x] - 2 * h[t(y,x)];
				if(d - cur >= 0) {
						fenw[y][deg[y]].up(d - cur, 1);
						if(~z)
								fenw[y][z].up(d - cur, -1);
				}
				//maxDist[y] = max(maxDist[y], d - cur);
				z = cent[y].second;
				y = cent[y].first;
			}
		} else {
			int x;
			cin >> x;
			int ans = 0;
			for(int y=--x,z=-1;~y;) {
				int cur = h[y] + h[x] - 2 * h[t(y,x)];
				ans += fenw[y][deg[y]].qry(cur);
				if(~z)
						ans += fenw[y][z].qry(cur);
				/*
				if(maxDist[y] >= cur)
					ans = 0;
				*/
				z = cent[y].second;
				y = cent[y].first;
			}
			cout << 1LL * exp(2, ans, l) * s[x] % l << "\n";
		}
	}
}
#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...