Submission #653162

# Submission time Handle Problem Language Result Execution time Memory
653162 2022-10-25T22:14:20 Z inksamurai Inside information (BOI21_servers) C++17
50 / 100
261 ms 61132 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3D2ZDxo ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}

template<class E>
struct treelca{
	int n;
	int timer=0;
	vec(vec(E)) adj;
	vi tin,tout,tour,tourid;
	vi segtree;
	vi depth;
	treelca(int m,int root,vec(vec(E)) neadj){
		init(m,root,neadj);
	}
	void init(int m,int root,vec(vec(E)) neadj){
		n=m;
		tin.clear();
		tout.clear();
		tour.clear();
		tourid.clear();
		depth.clear();
		tin.resize(n,0);
		tout.resize(n,0);
		tourid.resize(n,0);
		depth.resize(n,0);
		adj=neadj;
		dfs(root,-1);
		segtree.resize(4*sz(tour));
		build(1,0,sz(tour)-1);
	}
	void dfs(int v,int par){
		tour.pb(v);
		tourid[v]=sz(tour)-1;
		tin[v]=timer++;
		for(auto e:adj[v]){
			int u=e.to;
			if(u==par) continue;
			depth[u]=depth[v]+1;
			dfs(u,v);
			tour.pb(v);
		}
		tout[v]=timer++;
	}
	void build(int node,int l,int r){
		if(l==r){
			segtree[node]=tour[l];
		}else{
			int m=(l+r)>>1;
			build(node*2,l,m);
			build(node*2+1,m+1,r);
			segtree[node]=(tin[segtree[node*2]]<tin[segtree[node*2+1]]?segtree[node*2]:segtree[node*2+1]);
		}
	}
	int query(int node,int l,int r,int _l,int _r){
		if(l>_r or r<_l) return -1;
		if(l>=_l and r<=_r) return segtree[node];
		int m=(l+r)>>1;
		int vl=query(node*2,l,m,_l,_r);
		int vr=query(node*2+1,m+1,r,_l,_r);
		if(min(vl,vr)==-1) return max(vl,vr);
		return (tin[vl]<tin[vr]?vl:vr);
	}
	int ancestor(int from,int to){
		int tinfrom=tin[from],tinto=tin[to];
		if(tinfrom>tinto) swap(tinfrom,tinto);
		return query(1,0,sz(tour)-1,tinfrom,tinto);
	}
	int dist(int u,int v){
		return depth[u]+depth[v]-depth[ancestor(u,v)]*2;
	}
};

struct E{
	int to,w;
	E(int _to=0,int _w=0):
	to(_to),w(_w){}
};

const int inf=1e9;

signed main(){
_3D2ZDxo;
	int n,q;
	cin>>n>>q;
	// assert(n<=4000);
	q+=n-1;
	vec(vec(E)) adj(n);
	vec(vec(pii)) qry(n);
	vi pns(q,-1);
	rep(i,q){
		char ch;
		cin>>ch;
		if(ch=='S'){
			int u,v;
			cin>>u>>v;
			u-=1,v-=1;
			adj[u].pb(E(v,i));
			adj[v].pb(E(u,i));
		}else if(ch=='Q'){
			int u,v;
			cin>>u>>v;
			u-=1,v-=1;
			qry[v].pb({u,i});
		}else{
			int v;
			cin>>v;
			v-=1;
			pns[i]=inf;
		}
	}
	vi leb(n); // last node where increasing seq broke
	vi bel(n); // last node where decreasing seq broke
	vi par(n);
	vi par_wit(n);
	auto dfs=[&](auto self,int v,int _lst)->void{
		for(auto [u,w]:adj[v]){
			if(u==par[v]) continue;
			if(_lst>=w){
				leb[u]=v;
			}else{
				leb[u]=leb[v];
			}
			if(_lst<=w){
				bel[u]=v;
			}else{
				bel[u]=bel[v];
			}
			par[u]=v;
			par_wit[u]=w;
			self(self,u,w);
		}
	};
	dfs(dfs,0,0);
	vec(vi) afup(20,vi(n)); // affine up
	afup[0]=par;
	rng(j,1,20){
		rep(v,n){
			int up=afup[j-1][v];
			afup[j][v]=afup[j-1][up];
		}
	}
	treelca<E> lca(n,0,adj);
	auto move_down=[&](int v,int up)->int{
		int dist=lca.dist(v,up)-1;
		per(j,20){
			if(dist>>j&1){
				v=afup[j][v];
			}
		}
		return v;
	};
	rep(v,n){
		for(auto p:qry[v]){
			int u=p.fi;
			int j=p.se;
			bool pok=0;
			if(u==v){
				pok=1;
			}else{
				int up=lca.ancestor(u,v);
				if(up==v){
					if(lca.depth[leb[u]]<=lca.depth[up]){
						pok=par_wit[u]<=j;
					}
				}else if(up==u){
					// print("ho",lca.depth[bel[v]]);
					// if(v==4) print(move_down(v,u),bel[u]);
					if(lca.depth[bel[v]]<=lca.depth[up]){
						pok=par_wit[move_down(v,u)]<=j;
					}
				}else{
					if(lca.depth[leb[u]]<=lca.depth[up] and lca.depth[bel[v]]<=lca.depth[up]){
						int lov=move_down(v,up);
						int lou=move_down(u,up);
						pok=par_wit[lov]<=par_wit[lou] and par_wit[u]<=j;
					}
				}
			}
			pns[j]=pok;
		}
	}
	// rep(s,n){
	// 	vi usd(n,inf);
	// 	usd[s]=-1;
	// 	queue<int> que;
	// 	que.push(s);
	// 	while(sz(que)){
	// 		int v=que.front();
	// 		que.pop();
	// 		for(auto [to,w]:adj[v]){
	// 			if(usd[to]==inf and w>usd[v]){
	// 				usd[to]=w;
	// 				que.push(to);
	// 			}
	// 		}
	// 	}
	// 	for(auto p:qry[s]){
	// 		pns[p.se]=(usd[p.fi]<=p.se);
	// 	}
	// }
	rep(i,q){
		if(pns[i]==-1) continue;
		if(pns[i]!=inf){
			cout<<(pns[i]?"yes":"no")<<"\n";
		}else{
			cout<<"0\n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 33 ms 2636 KB Output is correct
2 Correct 54 ms 4172 KB Output is correct
3 Correct 73 ms 4328 KB Output is correct
4 Correct 54 ms 4220 KB Output is correct
5 Correct 51 ms 4280 KB Output is correct
6 Correct 84 ms 4316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 2636 KB Output is correct
2 Correct 54 ms 4172 KB Output is correct
3 Correct 73 ms 4328 KB Output is correct
4 Correct 54 ms 4220 KB Output is correct
5 Correct 51 ms 4280 KB Output is correct
6 Correct 84 ms 4316 KB Output is correct
7 Incorrect 31 ms 2544 KB Extra information in the output file
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 45 ms 2616 KB Output is correct
2 Correct 244 ms 54388 KB Output is correct
3 Correct 237 ms 54360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 2616 KB Output is correct
2 Correct 244 ms 54388 KB Output is correct
3 Correct 237 ms 54360 KB Output is correct
4 Incorrect 44 ms 2580 KB Extra information in the output file
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 2604 KB Output is correct
2 Correct 170 ms 57748 KB Output is correct
3 Correct 178 ms 57776 KB Output is correct
4 Correct 156 ms 57644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 2604 KB Output is correct
2 Correct 170 ms 57748 KB Output is correct
3 Correct 178 ms 57776 KB Output is correct
4 Correct 156 ms 57644 KB Output is correct
5 Incorrect 27 ms 2528 KB Extra information in the output file
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 28 ms 2652 KB Output is correct
2 Correct 174 ms 52136 KB Output is correct
3 Correct 196 ms 55188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 2652 KB Output is correct
2 Correct 174 ms 52136 KB Output is correct
3 Correct 196 ms 55188 KB Output is correct
4 Incorrect 31 ms 3404 KB Extra information in the output file
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 2620 KB Output is correct
2 Correct 173 ms 57776 KB Output is correct
3 Correct 174 ms 57772 KB Output is correct
4 Correct 167 ms 57660 KB Output is correct
5 Correct 28 ms 2584 KB Output is correct
6 Correct 171 ms 55656 KB Output is correct
7 Correct 190 ms 55244 KB Output is correct
8 Correct 224 ms 57820 KB Output is correct
9 Correct 206 ms 57396 KB Output is correct
10 Correct 208 ms 58104 KB Output is correct
11 Correct 221 ms 57548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 2620 KB Output is correct
2 Correct 173 ms 57776 KB Output is correct
3 Correct 174 ms 57772 KB Output is correct
4 Correct 167 ms 57660 KB Output is correct
5 Correct 28 ms 2584 KB Output is correct
6 Correct 171 ms 55656 KB Output is correct
7 Correct 190 ms 55244 KB Output is correct
8 Correct 224 ms 57820 KB Output is correct
9 Correct 206 ms 57396 KB Output is correct
10 Correct 208 ms 58104 KB Output is correct
11 Correct 221 ms 57548 KB Output is correct
12 Incorrect 25 ms 3404 KB Extra information in the output file
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 32 ms 2588 KB Output is correct
2 Correct 55 ms 4192 KB Output is correct
3 Correct 78 ms 4328 KB Output is correct
4 Correct 59 ms 4176 KB Output is correct
5 Correct 50 ms 4356 KB Output is correct
6 Correct 83 ms 4352 KB Output is correct
7 Correct 46 ms 2780 KB Output is correct
8 Correct 252 ms 57152 KB Output is correct
9 Correct 238 ms 57156 KB Output is correct
10 Correct 27 ms 3536 KB Output is correct
11 Correct 179 ms 61068 KB Output is correct
12 Correct 189 ms 61132 KB Output is correct
13 Correct 153 ms 60804 KB Output is correct
14 Correct 32 ms 3524 KB Output is correct
15 Correct 176 ms 55500 KB Output is correct
16 Correct 187 ms 55280 KB Output is correct
17 Correct 256 ms 57796 KB Output is correct
18 Correct 213 ms 57384 KB Output is correct
19 Correct 220 ms 58096 KB Output is correct
20 Correct 246 ms 57584 KB Output is correct
21 Correct 259 ms 58208 KB Output is correct
22 Correct 259 ms 58200 KB Output is correct
23 Correct 245 ms 58480 KB Output is correct
24 Correct 227 ms 58608 KB Output is correct
25 Correct 261 ms 58200 KB Output is correct
26 Correct 190 ms 55712 KB Output is correct
27 Correct 174 ms 55308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 2588 KB Output is correct
2 Correct 55 ms 4192 KB Output is correct
3 Correct 78 ms 4328 KB Output is correct
4 Correct 59 ms 4176 KB Output is correct
5 Correct 50 ms 4356 KB Output is correct
6 Correct 83 ms 4352 KB Output is correct
7 Correct 46 ms 2780 KB Output is correct
8 Correct 252 ms 57152 KB Output is correct
9 Correct 238 ms 57156 KB Output is correct
10 Correct 27 ms 3536 KB Output is correct
11 Correct 179 ms 61068 KB Output is correct
12 Correct 189 ms 61132 KB Output is correct
13 Correct 153 ms 60804 KB Output is correct
14 Correct 32 ms 3524 KB Output is correct
15 Correct 176 ms 55500 KB Output is correct
16 Correct 187 ms 55280 KB Output is correct
17 Correct 256 ms 57796 KB Output is correct
18 Correct 213 ms 57384 KB Output is correct
19 Correct 220 ms 58096 KB Output is correct
20 Correct 246 ms 57584 KB Output is correct
21 Correct 259 ms 58208 KB Output is correct
22 Correct 259 ms 58200 KB Output is correct
23 Correct 245 ms 58480 KB Output is correct
24 Correct 227 ms 58608 KB Output is correct
25 Correct 261 ms 58200 KB Output is correct
26 Correct 190 ms 55712 KB Output is correct
27 Correct 174 ms 55308 KB Output is correct
28 Incorrect 31 ms 3408 KB Extra information in the output file
29 Halted 0 ms 0 KB -