/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 6ms 5.152 MiB
#2 Wrong Answer 6ms 5.25 MiB
#3 Wrong Answer 53ms 16.809 MiB

Code

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

struct node{
	int s, e, m, val, lz;
	node *l, *r;
	node(int _s, int _e){
		s = _s, e = _e, m = (s + e) >> 1;
		if(s != e)l = new node(s, m), r = new node(m+1, e);
		val = lz =0 ;
	}
	
	void prop(){
		if(lz){
			val += lz * (e - s + 1);
			if(s != e)l->lz += lz, r->lz += lz;
			lz = 0;
		}
	}
	
	void upd(int a, int b, int c){
		if(s == a && e == b)lz += c;
		else{
			if(b <= m)l->upd(a, b, c);
			else if(a > m)r->upd(a, b, c);
			else l->upd(a, m, c), r->upd(m+1, b, c);
			l->prop(), r->prop();
			val = l->val + r->val;
		}
	}
	
	int qry(int a, int b){
		prop();
		if(a == s && b == e)return val;
		if(b <= m)return l->qry(a, b);
		if(a > m)return r->qry(a, b);
		return l->qry(a, m) + r->qry(m+1, b);
	}
}*root;

int n, q, S[200005], E[200005], head[200005], sz[200005], par[20][200005], dep[200005], up[200005];
vector <int> adj[200005];
int A[200005];
void precmp(int x, int p, int d){
	sz[x] = 1;
	dep[x] = d;
	up[x] = par[0][x] = p;
	for(auto i : adj[x]){
		if(i == p)continue;
		precmp(i, x, d + 1);
		sz[x] += sz[i];
		A[x] += A[i];
	}
}

int cnt = 1, rt = 1;
void dfs(int x, int par, int h){
	S[x] = cnt++;
	head[x] = h;
	int mx = -1, in = -1;
	for(auto i : adj[x]){
		if(i == par)continue;
		if(sz[i] > mx)mx = sz[i], in = i;
	}
	if(in == -1){
		E[x] = cnt - 1;
		return;
	}
	dfs(in, x, h);
	for(auto i : adj[x]){
		if(i == par || i == in)continue;
		dfs(i, x, i);
	}
	E[x] = cnt - 1;
}

int jmp(int cur, int k){
	assert(dep[cur] > k);
	for(int i = 19; i >= 0; i--){
		if(k >> i & 1)cur = par[i][cur];
	}
	return cur;
}

void updpath(int x, int y, int k){
	for(;head[x] != head[y]; y= up[head[y]]){
		if(dep[head[x]] > dep[head[y]])swap(x, y);
		root->upd(S[head[y]], S[y], k);
	}
	if(S[x] > S[y])swap(x, y);
	root->upd(S[x], S[y], k);
}

int qrypath(int x, int y){
	int res = 0;
	for(;head[x] != head[y]; y = up[head[y]]){
		if(dep[head[x]] > dep[head[y]])swap(x, y);
		res += root->qry(S[head[y]], S[y]);
	}
	if(S[x] > S[y])swap(x, y);
	return root->qry(S[x], S[y]) + res;
}

void solve(){
	cin >> n;
	for(int i = 1; i <= n; i++)cin >> A[i];
	for(int i = 2; i <= n; i++){
		int a, b; cin >> a >> b;
		adj[a].push_back(b); adj[b].push_back(a);
	}
	precmp(1, -1, 1);
	dfs(1, -1, 1);
	root = new node(1, n);
	for(int i = 1; i <= n; i++)root->upd(S[i], S[i], A[i]);
	for(int i = 1; i <= 19; i++)for(int j = 1; j <= n; j++)par[i][j] = par[i - 1][par[i - 1][j]];
	/*
	cin >> q;
	while(q--){
		int t; cin >> t;
		if(t == 1){
			cin >> rt;
		}
		else if(t == 2){
			int x, y, k; cin >> x >> y >> k;
			updpath(x, y, k);
		}
		else if(t == 3){
			int u, k; cin >> u >> k;
			if(u == rt){
				root->upd(1, n, k);
				continue;
			}
			if(S[u] >= S[rt] && E[u] <= E[rt]){
				//cerr << 1 << '\n';
				root->upd(S[u], E[u], k);
			}
			else if(S[u] > E[rt] || E[u] < S[rt]){
				//cerr << 2 << '\n';
				//cerr << S[u] << ' ' << E[u] << '\n';
				root->upd(S[u], E[u], k);
			}
			else{
				assert(jmp(rt, dep[rt] - dep[u]) == u);
				int res = jmp(rt, dep[rt] - dep[u] - 1);
				assert(res >= 1 && res <= n);
				//cerr << "ok\n";
				if(S[res] > 1)root->upd(1, S[res] - 1, k);
				if(E[res] < n)root->upd(E[res] + 1, n, k);
			}
			
			
		}
		else if(t == 4){
			int x, y; cin >> x >> y;
			cout << qrypath(x, y) << '\n';
		}
		else{
			int u; cin >> u;
			if(u == rt){
				cout << root->val << '\n';
				continue;
			}
			if(S[u] >= S[rt] && E[u] <= E[rt])cout << root->qry(S[u], E[u]) << '\n';
			else if(S[u] > E[rt] || E[u] < S[rt])cout << root->qry(S[u], E[u]) << '\n';
			else{
				//assert(0);
				assert(S[u] <= S[rt] && E[u] >= E[rt]);
				assert(jmp(rt, dep[rt] - dep[u]) == u);
				int res = jmp(rt, dep[rt] - dep[u] - 1);
				//cout << res << ' ';
				assert(res >= 1 && res <= n);
				int r = 0;
				if(S[res] > 1)r += root->qry(1, S[res] - 1);
				if(E[res] < n)r += root->qry(E[res] + 1, n);
				cout << r << '\n';
			}
			
			//cout << 0 << '\n';
		}
	}
	*/
	cin >> q;
	while(q--){
		int x, y; cin >> x >> y;
		int cur = root->qry(S[y], S[y]);
		if(x == 1){
			updpath(1, y, sz[y] - 2 * cur);
		}
		else{
			cout << cur << '\n';
		}
	}
}

main(){
	ios::sync_with_stdio(0);cin.tie(0);
	int tc = 1;
	//cin >> tc;
	for(int tc1=1;tc1<=tc;tc1++){
		// cout << "Case #" << tc1 << ": ";
		solve();
	}
}

Information

Submit By
Type
Submission
Problem
P1101 Mr. Heart and the Enchanted Lights
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 16:02:54
Judged At
2024-11-11 02:49:24
Judged By
Score
5
Total Time
53ms
Peak Memory
16.809 MiB