Submission #865854

# Submission time Handle Problem Language Result Execution time Memory
865854 2023-10-24T23:51:57 Z AbdelmagedNour Inside information (BOI21_servers) C++17
100 / 100
2920 ms 54636 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
typedef long long ll;
const int N=250005,SQ=500;
vector<vector<pair<int,int>>>adj(N);
vector<int>cur_adj[N],order;
int in[N],out[N],dep[N],p[N][18],val[N],max_inc[N],max_dec[N],T;
void init_dfs(int v,int par){
	in[v]=++T;order.push_back(v);
	p[v][0]=par;
	for(int i=1;i<18;i++)p[v][i]=p[p[v][i-1]][i-1];
	max_inc[v]=max_dec[v]=par;
	if(val[v]<val[par])max_inc[v]=max_inc[par];
	else max_dec[v]=max_dec[par];
	for(auto&e:adj[v]){
		int u=e.first,w=e.second;
		if(u==par)continue;
		dep[u]=dep[v]+1;
		val[u]=w;
		init_dfs(u,v);
	}
	out[v]=T;
}
bool anc(int v,int u){
	return in[v]<=in[u]&&out[u]<=out[v];
}
int max_in_path(int u,int v){
	if(anc(u,v))return val[v];
	int pu=u,pv=v;
	for(int i=17;i>=0;i--){
		if(!anc(p[pu][i],v))pu=p[pu][i];
		if(!anc(p[pv][i],u))pv=p[pv][i];
	}
	if(p[pu][0]==v)return val[pu];
	return val[pu]<val[pv]?val[v]:INT_MAX;
}
bool can_reach(int u,int v,int Time){
	if(u==v)return 1;
	if((!anc(max_inc[u],v))||(!anc(max_dec[v],u)))return 0;
	if(!anc(v,u)&&val[v]>Time)return 0;
	return max_in_path(u,v)<=Time;
}
int dp_down[N],dp_up[N],dp[N],vis[N];
void dfs_down(int v,int par){
	if(vis[v])return;
	vis[v]=1;
	dp_down[v]=dp[v]=1;
	for(auto&u:cur_adj[v]){
		if(u==par)continue;
		dfs_down(u,v);
		if(val[u]>val[v])dp_down[v]+=dp_down[u];
		dp[v]+=dp_down[u];
	}
}
void dfs_up(int v,int par,int sum=0){
	dp_up[v]=sum+(par!=v);
	if(val[par]>val[v])dp_up[v]+=dp_up[par];
	dp[v]+=dp_up[v];
	sum=0;
	for(int i=cur_adj[v].size()-1;i>=0;i--){
		int u=cur_adj[v][i];
		if(u==par)continue;
		dfs_up(u,v,sum);
		sum+=dp_down[u];
	}
}
void reclac(){
	memset(vis+1,0,int(order.size())*sizeof(vis[1]));
	for(auto&u:order){
		if(vis[u])continue;
		dfs_down(u,u);
		dfs_up(u,u);
	}
}
char op[N];
int A[N],B[N];
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n,q;
	cin>>n>>q;
	for(int i=1;i<n+q;i++){
		cin>>op[i];
		if(op[i]=='S'){
			cin>>A[i]>>B[i];
			adj[A[i]].push_back({B[i],i});
			adj[B[i]].push_back({A[i],i});
		}
		if(op[i]=='Q')cin>>A[i]>>B[i];
		if(op[i]=='C')cin>>A[i];
	}
	init_dfs(1,1);
	fill(dp+1,dp+n+1,1);
	int cnt=0;
	vector<int>edges;
	for(int i=1;i<n+q;i++){
		if(cnt==SQ)reclac(),cnt=0,edges.clear();
		if(op[i]=='S'){
			edges.push_back(i);
			cur_adj[A[i]].push_back(B[i]);
			cur_adj[B[i]].push_back(A[i]);
			cnt++;
		}else if(op[i]=='Q')cout<<(can_reach(B[i],A[i],i)?"yes\n":"no\n");
		else{
			int res=dp[A[i]];
			for(auto&e:edges){
				int u=A[e],v=B[e];
				if(val[u]!=e)swap(u,v);
				if(!anc(u,A[i])&&can_reach(A[i],u,i))res++;
				else if(anc(u,A[i])&&can_reach(A[i],v,i))res++;
			}
			cout<<res<<"\n";
		}
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 23 ms 24152 KB Output is correct
2 Correct 25 ms 24924 KB Output is correct
3 Correct 33 ms 25104 KB Output is correct
4 Correct 28 ms 25172 KB Output is correct
5 Correct 25 ms 25168 KB Output is correct
6 Correct 49 ms 25140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 24152 KB Output is correct
2 Correct 25 ms 24924 KB Output is correct
3 Correct 33 ms 25104 KB Output is correct
4 Correct 28 ms 25172 KB Output is correct
5 Correct 25 ms 25168 KB Output is correct
6 Correct 49 ms 25140 KB Output is correct
7 Correct 29 ms 24404 KB Output is correct
8 Correct 137 ms 24892 KB Output is correct
9 Correct 490 ms 25208 KB Output is correct
10 Correct 152 ms 24988 KB Output is correct
11 Correct 217 ms 25180 KB Output is correct
12 Correct 2649 ms 25348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 24392 KB Output is correct
2 Correct 835 ms 44412 KB Output is correct
3 Correct 780 ms 44396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 24392 KB Output is correct
2 Correct 835 ms 44412 KB Output is correct
3 Correct 780 ms 44396 KB Output is correct
4 Correct 56 ms 24352 KB Output is correct
5 Correct 1669 ms 44364 KB Output is correct
6 Correct 1116 ms 44172 KB Output is correct
7 Correct 2920 ms 44184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 24400 KB Output is correct
2 Correct 769 ms 53016 KB Output is correct
3 Correct 749 ms 52940 KB Output is correct
4 Correct 656 ms 54432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 24400 KB Output is correct
2 Correct 769 ms 53016 KB Output is correct
3 Correct 749 ms 52940 KB Output is correct
4 Correct 656 ms 54432 KB Output is correct
5 Correct 21 ms 24412 KB Output is correct
6 Correct 886 ms 52572 KB Output is correct
7 Correct 1324 ms 52940 KB Output is correct
8 Correct 982 ms 52256 KB Output is correct
9 Correct 993 ms 52736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 24400 KB Output is correct
2 Correct 410 ms 44120 KB Output is correct
3 Correct 853 ms 44248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 24400 KB Output is correct
2 Correct 410 ms 44120 KB Output is correct
3 Correct 853 ms 44248 KB Output is correct
4 Correct 24 ms 24656 KB Output is correct
5 Correct 1495 ms 44220 KB Output is correct
6 Correct 935 ms 43868 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 24408 KB Output is correct
2 Correct 733 ms 52684 KB Output is correct
3 Correct 743 ms 52712 KB Output is correct
4 Correct 676 ms 54636 KB Output is correct
5 Correct 23 ms 24412 KB Output is correct
6 Correct 415 ms 44196 KB Output is correct
7 Correct 833 ms 44252 KB Output is correct
8 Correct 808 ms 45780 KB Output is correct
9 Correct 851 ms 45524 KB Output is correct
10 Correct 1294 ms 47572 KB Output is correct
11 Correct 1350 ms 47588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 24408 KB Output is correct
2 Correct 733 ms 52684 KB Output is correct
3 Correct 743 ms 52712 KB Output is correct
4 Correct 676 ms 54636 KB Output is correct
5 Correct 23 ms 24412 KB Output is correct
6 Correct 415 ms 44196 KB Output is correct
7 Correct 833 ms 44252 KB Output is correct
8 Correct 808 ms 45780 KB Output is correct
9 Correct 851 ms 45524 KB Output is correct
10 Correct 1294 ms 47572 KB Output is correct
11 Correct 1350 ms 47588 KB Output is correct
12 Correct 21 ms 24408 KB Output is correct
13 Correct 893 ms 52664 KB Output is correct
14 Correct 1332 ms 52632 KB Output is correct
15 Correct 1000 ms 52560 KB Output is correct
16 Correct 990 ms 52404 KB Output is correct
17 Correct 24 ms 24400 KB Output is correct
18 Correct 1496 ms 43920 KB Output is correct
19 Correct 945 ms 44064 KB Output is correct
20 Correct 1024 ms 45264 KB Output is correct
21 Correct 991 ms 45112 KB Output is correct
22 Correct 2845 ms 46644 KB Output is correct
23 Correct 2097 ms 48776 KB Output is correct
24 Correct 1563 ms 48632 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24400 KB Output is correct
2 Correct 24 ms 24976 KB Output is correct
3 Correct 32 ms 25180 KB Output is correct
4 Correct 25 ms 25056 KB Output is correct
5 Correct 31 ms 25340 KB Output is correct
6 Correct 36 ms 24924 KB Output is correct
7 Correct 25 ms 24284 KB Output is correct
8 Correct 573 ms 44400 KB Output is correct
9 Correct 565 ms 44508 KB Output is correct
10 Correct 19 ms 24272 KB Output is correct
11 Correct 742 ms 52984 KB Output is correct
12 Correct 742 ms 53192 KB Output is correct
13 Correct 656 ms 54476 KB Output is correct
14 Correct 23 ms 24400 KB Output is correct
15 Correct 408 ms 44300 KB Output is correct
16 Correct 754 ms 44244 KB Output is correct
17 Correct 832 ms 45432 KB Output is correct
18 Correct 917 ms 45428 KB Output is correct
19 Correct 1390 ms 47852 KB Output is correct
20 Correct 1220 ms 47392 KB Output is correct
21 Correct 567 ms 44624 KB Output is correct
22 Correct 577 ms 44612 KB Output is correct
23 Correct 617 ms 45100 KB Output is correct
24 Correct 631 ms 45216 KB Output is correct
25 Correct 839 ms 46576 KB Output is correct
26 Correct 881 ms 44064 KB Output is correct
27 Correct 846 ms 44224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 24400 KB Output is correct
2 Correct 24 ms 24976 KB Output is correct
3 Correct 32 ms 25180 KB Output is correct
4 Correct 25 ms 25056 KB Output is correct
5 Correct 31 ms 25340 KB Output is correct
6 Correct 36 ms 24924 KB Output is correct
7 Correct 25 ms 24284 KB Output is correct
8 Correct 573 ms 44400 KB Output is correct
9 Correct 565 ms 44508 KB Output is correct
10 Correct 19 ms 24272 KB Output is correct
11 Correct 742 ms 52984 KB Output is correct
12 Correct 742 ms 53192 KB Output is correct
13 Correct 656 ms 54476 KB Output is correct
14 Correct 23 ms 24400 KB Output is correct
15 Correct 408 ms 44300 KB Output is correct
16 Correct 754 ms 44244 KB Output is correct
17 Correct 832 ms 45432 KB Output is correct
18 Correct 917 ms 45428 KB Output is correct
19 Correct 1390 ms 47852 KB Output is correct
20 Correct 1220 ms 47392 KB Output is correct
21 Correct 567 ms 44624 KB Output is correct
22 Correct 577 ms 44612 KB Output is correct
23 Correct 617 ms 45100 KB Output is correct
24 Correct 631 ms 45216 KB Output is correct
25 Correct 839 ms 46576 KB Output is correct
26 Correct 881 ms 44064 KB Output is correct
27 Correct 846 ms 44224 KB Output is correct
28 Correct 29 ms 24360 KB Output is correct
29 Correct 134 ms 24528 KB Output is correct
30 Correct 487 ms 24944 KB Output is correct
31 Correct 152 ms 24772 KB Output is correct
32 Correct 206 ms 24916 KB Output is correct
33 Correct 2617 ms 25184 KB Output is correct
34 Correct 54 ms 24412 KB Output is correct
35 Correct 1195 ms 44488 KB Output is correct
36 Correct 827 ms 44212 KB Output is correct
37 Correct 2408 ms 43724 KB Output is correct
38 Correct 27 ms 24776 KB Output is correct
39 Correct 876 ms 52300 KB Output is correct
40 Correct 1343 ms 52712 KB Output is correct
41 Correct 973 ms 50544 KB Output is correct
42 Correct 981 ms 50284 KB Output is correct
43 Correct 24 ms 23644 KB Output is correct
44 Correct 1499 ms 42332 KB Output is correct
45 Correct 911 ms 42076 KB Output is correct
46 Correct 1020 ms 43468 KB Output is correct
47 Correct 978 ms 43436 KB Output is correct
48 Correct 2639 ms 44548 KB Output is correct
49 Correct 2074 ms 48584 KB Output is correct
50 Correct 1512 ms 48768 KB Output is correct
51 Correct 1595 ms 44620 KB Output is correct
52 Correct 2915 ms 44468 KB Output is correct
53 Correct 2418 ms 44060 KB Output is correct
54 Correct 808 ms 44484 KB Output is correct
55 Correct 2462 ms 44368 KB Output is correct
56 Correct 747 ms 45252 KB Output is correct
57 Correct 968 ms 45816 KB Output is correct
58 Correct 1037 ms 43720 KB Output is correct
59 Correct 848 ms 44084 KB Output is correct