답안 #556702

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
556702 2022-05-03T17:37:41 Z Arvin Sprinkler (JOI22_sprinkler) C++11
3 / 100
4000 ms 155140 KB
#include <bits/stdc++.h>

using namespace std;

#define ll long long

const int maxN = 2e5 + 5;
const int maxD = 45;
const int logN = log2(maxN)+1;
const int maxQ = 4e5 + 5;

ll l;

struct Fenwick {
	ll tree[maxD];
	
	void reset(){
		fill(tree, tree+maxD, 1);
	}
	
	void update(int pos, int val){
		pos++;
		
		while(pos < maxD){
			tree[pos] *= val;
			tree[pos] %= l;
			pos += pos&(-pos);
		}
	}
	
	ll query(int pos){
		pos++;
		
		ll ans = 1;
		while(pos > 0){
			ans *= tree[pos];
			ans %= l;
			pos -= pos&(-pos);
		}
		return ans;
	}
};

vector<int> adj[maxN], new_adj[maxN];
Fenwick tree[maxN];
bool exist[maxN];
int parent[maxN], level[maxN], sz[maxN];
int ancestor[logN][maxN];

void dfs(int curNode, int prvNode){
	sz[curNode] = 1;
	
	for(auto nxt : adj[curNode]){
		if(nxt != prvNode && exist[nxt]){
			dfs(nxt, curNode);
			sz[curNode] += sz[nxt];	
		}
	}	
}

void dfs2(int curNode, int prvNode = 0){
	level[curNode] = level[prvNode]+1;
	ancestor[0][curNode] = prvNode;
	
	for(int x=1;x<logN;x++){
		ancestor[x][curNode] = ancestor[x-1][ancestor[x-1][curNode]];
	}
	
	for(auto nxt : adj[curNode]){
		if(nxt != prvNode){
			dfs2(nxt, curNode);
		}
	}	
}

int centroid(int curNode){
	dfs(curNode, curNode);
	int sum = sz[curNode];
	
	int root = curNode;
	bool found = false;
	while(!found){
		found = true;
		for(auto nxt : adj[root]){
			if(exist[nxt] && sz[nxt] <= sz[root] && sz[nxt] > sum/2){
				root = nxt;
				found = false;
				break;
			}
		}
	}
	return root;
}

int build(int curNode){
	int root = centroid(curNode);
	exist[root] = false;
	
	for(auto nxt : adj[root]){
		if(exist[nxt]){
			int nxtRoot = build(nxt);
			parent[nxtRoot] = root;
			new_adj[root].push_back(nxtRoot);
		}
	}
	return root;
}

int getLCA(int a, int b){
	if(level[a] > level[b]) swap(a, b);
	
	for(int x=logN-1;x>=0;x--){
		if(level[a] <= level[b]-(1 << x)){
			b = ancestor[x][b];
		}
	}
	
	if(a == b) return a;
	
	for(int x=logN-1;x>=0;x--){
		if(ancestor[x][a] != ancestor[x][b]){
			a = ancestor[x][a];
			b = ancestor[x][b];
		}
	}
	
	return ancestor[0][a];
}

int getDist(int a, int b){
	return level[a] + level[b] - 2*level[getLCA(a, b)];	
}

vector<pair<int, int>> v[maxN];

int main(){
	ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
	
	int n;
	cin >> n >> l;
	
	for(int x=0;x<n-1;x++){
		int a, b;
		cin >> a >> b;
		
		a--; b--;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	
	ll h[n];
	for(int x=0;x<n;x++){
		cin >> h[x];
		tree[x].reset();
		exist[x] = true;
	}
	
	parent[build(0)] = -1;
	
	level[0] = -1;
	dfs2(0);
	
//	for(int x=0;x<n;x++){
//		cout << parent[x] << " ";
//	}cout << "\n";
	
	int q;
	cin >> q;
	
	int idx = 0;
	ll val[q];
	
	while(q--){
		int t;
		cin >> t;
		
		if(t == 1){
			int x, d, w;
			cin >> x >> d >> w;
			
			x--;
			
			val[idx] = w;
			int node = x, prv = -1;
			while(node != -1){
				int rem = d-getDist(node, x);
				if(rem >= 0){
					v[node].push_back({rem, idx});
//					tree[node].update(41-rem, w);
				}
				
//				cout << x << " " << node << " -> " << rem << " " << w << "\n";
				
				prv = node;
				node = parent[node];
			}
			
			idx++;
		} else if(t == 2){
			int x;
			cin >> x;
			
			x--;
			
			ll ans = h[x];
			
			set<int> st;
			
			int node = x;
			while(node != -1){
				int d = getDist(node, x);
				
				if(d <= 40){
					for(auto p : v[node]){
						if(!st.count(p.second) && d <= p.first){
							st.insert(p.second);
							ans *= val[p.second];
							ans %= l;
						}
					}
				}
				
//				cout << level[node] << " " << node << " -> " << d << " " << ans << " " << tree[node].query(41-d) << "\n";
				node = parent[node];
			}
			
			cout << ans << "\n";
		} else {
			assert(false);
		}
	}
    return 0;
}

Compilation message

sprinkler.cpp: In function 'int main()':
sprinkler.cpp:186:18: warning: variable 'prv' set but not used [-Wunused-but-set-variable]
  186 |    int node = x, prv = -1;
      |                  ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14548 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 10 ms 15096 KB Output is correct
5 Correct 15 ms 15072 KB Output is correct
6 Correct 20 ms 15060 KB Output is correct
7 Correct 21 ms 15084 KB Output is correct
8 Correct 17 ms 15064 KB Output is correct
9 Correct 17 ms 14548 KB Output is correct
10 Correct 12 ms 14548 KB Output is correct
11 Correct 13 ms 14572 KB Output is correct
12 Correct 10 ms 14568 KB Output is correct
13 Correct 10 ms 14540 KB Output is correct
14 Correct 16 ms 14584 KB Output is correct
15 Correct 15 ms 14468 KB Output is correct
16 Correct 14 ms 14540 KB Output is correct
17 Correct 12 ms 14548 KB Output is correct
18 Correct 11 ms 14548 KB Output is correct
19 Correct 17 ms 14548 KB Output is correct
20 Correct 15 ms 14580 KB Output is correct
21 Correct 13 ms 14548 KB Output is correct
22 Correct 11 ms 14504 KB Output is correct
23 Correct 12 ms 14520 KB Output is correct
24 Correct 17 ms 14548 KB Output is correct
25 Correct 14 ms 14560 KB Output is correct
26 Correct 14 ms 14576 KB Output is correct
27 Correct 12 ms 14524 KB Output is correct
28 Correct 11 ms 14548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 1913 ms 121164 KB Output is correct
3 Correct 2089 ms 126772 KB Output is correct
4 Correct 2825 ms 130980 KB Output is correct
5 Correct 1959 ms 122852 KB Output is correct
6 Correct 1202 ms 122672 KB Output is correct
7 Correct 1052 ms 122852 KB Output is correct
8 Execution timed out 4066 ms 118068 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 1913 ms 121164 KB Output is correct
3 Correct 2089 ms 126772 KB Output is correct
4 Correct 2825 ms 130980 KB Output is correct
5 Correct 1959 ms 122852 KB Output is correct
6 Correct 1202 ms 122672 KB Output is correct
7 Correct 1052 ms 122852 KB Output is correct
8 Execution timed out 4066 ms 118068 KB Time limit exceeded
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 3173 ms 133548 KB Output is correct
3 Correct 3808 ms 155140 KB Output is correct
4 Correct 3973 ms 139884 KB Output is correct
5 Execution timed out 4078 ms 125328 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14420 KB Output is correct
2 Correct 3355 ms 131080 KB Output is correct
3 Correct 3675 ms 151924 KB Output is correct
4 Correct 3751 ms 138452 KB Output is correct
5 Execution timed out 4038 ms 133132 KB Time limit exceeded
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 14548 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 7 ms 14420 KB Output is correct
4 Correct 10 ms 15096 KB Output is correct
5 Correct 15 ms 15072 KB Output is correct
6 Correct 20 ms 15060 KB Output is correct
7 Correct 21 ms 15084 KB Output is correct
8 Correct 17 ms 15064 KB Output is correct
9 Correct 17 ms 14548 KB Output is correct
10 Correct 12 ms 14548 KB Output is correct
11 Correct 13 ms 14572 KB Output is correct
12 Correct 10 ms 14568 KB Output is correct
13 Correct 10 ms 14540 KB Output is correct
14 Correct 16 ms 14584 KB Output is correct
15 Correct 15 ms 14468 KB Output is correct
16 Correct 14 ms 14540 KB Output is correct
17 Correct 12 ms 14548 KB Output is correct
18 Correct 11 ms 14548 KB Output is correct
19 Correct 17 ms 14548 KB Output is correct
20 Correct 15 ms 14580 KB Output is correct
21 Correct 13 ms 14548 KB Output is correct
22 Correct 11 ms 14504 KB Output is correct
23 Correct 12 ms 14520 KB Output is correct
24 Correct 17 ms 14548 KB Output is correct
25 Correct 14 ms 14560 KB Output is correct
26 Correct 14 ms 14576 KB Output is correct
27 Correct 12 ms 14524 KB Output is correct
28 Correct 11 ms 14548 KB Output is correct
29 Correct 7 ms 14420 KB Output is correct
30 Correct 1913 ms 121164 KB Output is correct
31 Correct 2089 ms 126772 KB Output is correct
32 Correct 2825 ms 130980 KB Output is correct
33 Correct 1959 ms 122852 KB Output is correct
34 Correct 1202 ms 122672 KB Output is correct
35 Correct 1052 ms 122852 KB Output is correct
36 Execution timed out 4066 ms 118068 KB Time limit exceeded
37 Halted 0 ms 0 KB -