답안 #401408

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
401408 2021-05-10T07:03:38 Z errorgorn Inside information (BOI21_servers) C++17
100 / 100
2345 ms 102992 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
 
#define ll long long
#define ii pair<int,int>
#define fi first
#define se second
#define endl '\n'
 
#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back
 
#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);(s<e)?x++:x--)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
 
struct DSU{
	int p[120005];
	
	DSU(){
		rep(x,0,120005) p[x]=x;
	}
	
	int par(int i){
		if (p[i]==i) return i;
		else return p[i]=par(p[i]);
	}
	
	void unions(int i,int j){
		i=par(i),j=par(j);
		p[i]=j;
	}
} dsu=DSU();
 
int n,k;
vector<ii> al[120005];
 
int d[120005];
int tkd[120005][20];
 
int asc[120005];
int dsc[120005];
int w[120005];
 
void dfs(int i,int p){
	for (auto &it:al[i]){
		if (it.fi==p) continue;
		w[it.fi]=it.se;
		
		asc[it.fi]=dsc[it.fi]=i;
		if (w[it.fi]>w[i]) asc[it.fi]=asc[i];
		else dsc[it.fi]=dsc[i];
		
		d[it.fi]=d[i]+1;
		
		int curr=tkd[it.fi][0]=i;
		rep(x,0,20){
			if (curr==-1) break;
			curr=tkd[it.fi][x+1]=tkd[curr][x];
		}
		
		dfs(it.fi,i);
	}
}
 
int mov(int i,int j){
	rep(bit,0,20) if (j&(1<<bit)){
		i=tkd[i][bit];
	}
	return i;
}
 
int lca(int i,int j){
	if (d[i]<d[j]) swap(i,j);
	i=mov(i,d[i]-d[j]);
	if (i==j) return i;
	
	rep(x,20,0){
		if (tkd[i][x]!=tkd[j][x]) i=tkd[i][x],j=tkd[j][x];
	}
	
	return tkd[i][0];
}
 
struct dat{
	char c;
	int a,b;
};
 
vector<dat> Q;
 
bool reach(int a,int b){ //is a->b decreasing
	int g=lca(a,b);
			
	if (g==a){
		if (d[dsc[b]]<=d[g]) return true;
		else return false;
	}
	else if (g==b){
		if (d[asc[a]]<=d[g]) return true;
		else return false;
	}
	else{
		bool can=true;
		if (d[dsc[b]]>d[g]) can=false;
		if (d[asc[a]]>d[g]) can=false;
		
		b=mov(b,d[b]-d[g]-1);
		a=mov(a,d[a]-d[g]-1);
		if (w[a]<w[b]) can=false;
		
		return can;
	}
}

int last(int a,int b){ 
	int g=lca(a,b);
	
	if (g==a) return w[mov(b,d[b]-d[a]-1)];
	else return w[a];
}

bool used[120005];
int ss[120005];
int centp[120005];

void dfs_sz(int i,int p){
	ss[i]=1;
	for (auto &it:al[i]){
		if (used[it.fi] || it.fi==p) continue;
		dfs_sz(it.fi,i);
		ss[i]+=ss[it.fi];
	}
}

void dfs_c(int i,int p){
	dfs_sz(i,-1);
	
	int currp=i;
	int curr=i;
	int sz=ss[i];
	while (true){
		for (auto &it:al[curr]){
			if (used[it.fi] || it.fi==currp) continue;
			
			if (ss[it.fi]>sz/2){
				currp=curr;
				curr=it.fi;
				goto reloop;
			}
		}
		break;
		reloop:;
	}
	
	centp[curr]=p;
	used[curr]=true;
	
	for (auto &it:al[curr]) if (!used[it.fi]) dfs_c(it.fi,curr);
}

#define indexed_set tree<ll,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug

indexed_set s[120005];

void upd_c(int i,int j){
	int curr=centp[i];
	
	while (curr!=-1){
		if (reach(i,curr) && last(i,curr)==j){
			s[curr].insert(last(curr,i));
			//cout<<"add: "<<curr<<" "<<last(curr,i)<<endl;
		}
		
		curr=centp[curr];
	}
}

int que_c(int i){
	int res=0;
	res+=sz(s[i])+1;
	int curr=centp[i];
	
	while (curr!=-1){
		if (dsu.par(i)==dsu.par(curr) && reach(curr,i)){
			res+=sz(s[curr])-s[curr].order_of_key(last(curr,i)+1)+1;
		}
		
		curr=centp[curr];
	}
	
	return res;
}
 
int main(){
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(false);
	
	cin>>n>>k;
	
	char c;
	int a,b;
	
	int IDX=0;
	rep(x,0,n+k-1){
		cin>>c;
		
		if (c=='S'){
			cin>>a>>b;
			Q.pub({c,a,b});
			al[a].pub(ii(b,IDX));
			al[b].pub(ii(a,IDX));
			IDX++;
		}
		else if (c=='Q'){
			cin>>a>>b;
			Q.pub({c,a,b});
		}
		else{
			cin>>a;
			Q.pub({c,a,0});
		}
	}
	
	memset(tkd,-1,sizeof(tkd));
	asc[1]=dsc[1]=1;
	dfs(1,-1);
	dfs_c(1,-1);
	
	
	//rep(x,1,n+1) cout<<w[x]<<" "; cout<<endl;
	//rep(x,1,n+1) cout<<asc[x]<<" "; cout<<endl;
	//rep(x,1,n+1) cout<<dsc[x]<<" "; cout<<endl;
	//rep(x,1,n+1) cout<<d[x]<<" "; cout<<endl;
	//rep(x,1,n+1) cout<<centp[x]<<" "; cout<<endl;
	
	IDX=0;
	for (auto &it:Q){
		if (it.c=='S'){
			dsu.unions(it.a,it.b);
			
			upd_c(it.a,IDX);
			upd_c(it.b,IDX);
			IDX++;
			//cout<<endl;
		}
		else if (it.c=='Q'){
			if (dsu.par(it.a)==dsu.par(it.b) && 
				reach(it.a,it.b)) cout<<"yes"<<endl;
			else cout<<"no"<<endl;
		}
		else{
			cout<<que_c(it.a)<<endl;
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 26172 KB Output is correct
2 Correct 65 ms 26664 KB Output is correct
3 Correct 76 ms 26724 KB Output is correct
4 Correct 64 ms 26824 KB Output is correct
5 Correct 75 ms 26980 KB Output is correct
6 Correct 82 ms 26604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 26172 KB Output is correct
2 Correct 65 ms 26664 KB Output is correct
3 Correct 76 ms 26724 KB Output is correct
4 Correct 64 ms 26824 KB Output is correct
5 Correct 75 ms 26980 KB Output is correct
6 Correct 82 ms 26604 KB Output is correct
7 Correct 51 ms 26084 KB Output is correct
8 Correct 79 ms 27692 KB Output is correct
9 Correct 88 ms 27860 KB Output is correct
10 Correct 80 ms 27844 KB Output is correct
11 Correct 124 ms 27992 KB Output is correct
12 Correct 73 ms 27708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 26112 KB Output is correct
2 Correct 328 ms 42668 KB Output is correct
3 Correct 346 ms 42548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 26112 KB Output is correct
2 Correct 328 ms 42668 KB Output is correct
3 Correct 346 ms 42548 KB Output is correct
4 Correct 52 ms 26120 KB Output is correct
5 Correct 350 ms 45376 KB Output is correct
6 Correct 245 ms 44508 KB Output is correct
7 Correct 271 ms 44696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 26172 KB Output is correct
2 Correct 959 ms 53804 KB Output is correct
3 Correct 981 ms 53768 KB Output is correct
4 Correct 1080 ms 102788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 47 ms 26172 KB Output is correct
2 Correct 959 ms 53804 KB Output is correct
3 Correct 981 ms 53768 KB Output is correct
4 Correct 1080 ms 102788 KB Output is correct
5 Correct 49 ms 26144 KB Output is correct
6 Correct 1029 ms 56664 KB Output is correct
7 Correct 1343 ms 102920 KB Output is correct
8 Correct 1065 ms 56260 KB Output is correct
9 Correct 1022 ms 56252 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 26156 KB Output is correct
2 Correct 949 ms 86440 KB Output is correct
3 Correct 637 ms 48520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 26156 KB Output is correct
2 Correct 949 ms 86440 KB Output is correct
3 Correct 637 ms 48520 KB Output is correct
4 Correct 50 ms 26104 KB Output is correct
5 Correct 1016 ms 89544 KB Output is correct
6 Correct 705 ms 51312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 26124 KB Output is correct
2 Correct 987 ms 53796 KB Output is correct
3 Correct 955 ms 53864 KB Output is correct
4 Correct 1061 ms 102772 KB Output is correct
5 Correct 49 ms 26156 KB Output is correct
6 Correct 885 ms 86520 KB Output is correct
7 Correct 630 ms 48524 KB Output is correct
8 Correct 792 ms 47728 KB Output is correct
9 Correct 834 ms 47984 KB Output is correct
10 Correct 1216 ms 70008 KB Output is correct
11 Correct 1331 ms 69208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 26124 KB Output is correct
2 Correct 987 ms 53796 KB Output is correct
3 Correct 955 ms 53864 KB Output is correct
4 Correct 1061 ms 102772 KB Output is correct
5 Correct 49 ms 26156 KB Output is correct
6 Correct 885 ms 86520 KB Output is correct
7 Correct 630 ms 48524 KB Output is correct
8 Correct 792 ms 47728 KB Output is correct
9 Correct 834 ms 47984 KB Output is correct
10 Correct 1216 ms 70008 KB Output is correct
11 Correct 1331 ms 69208 KB Output is correct
12 Correct 48 ms 26156 KB Output is correct
13 Correct 1040 ms 56604 KB Output is correct
14 Correct 1357 ms 102992 KB Output is correct
15 Correct 1047 ms 56392 KB Output is correct
16 Correct 1047 ms 56344 KB Output is correct
17 Correct 50 ms 26940 KB Output is correct
18 Correct 1040 ms 89540 KB Output is correct
19 Correct 686 ms 51228 KB Output is correct
20 Correct 999 ms 50644 KB Output is correct
21 Correct 867 ms 50740 KB Output is correct
22 Correct 1516 ms 71544 KB Output is correct
23 Correct 2313 ms 92624 KB Output is correct
24 Correct 1764 ms 86284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 26192 KB Output is correct
2 Correct 70 ms 26644 KB Output is correct
3 Correct 78 ms 26704 KB Output is correct
4 Correct 66 ms 26824 KB Output is correct
5 Correct 74 ms 26872 KB Output is correct
6 Correct 77 ms 26688 KB Output is correct
7 Correct 51 ms 26140 KB Output is correct
8 Correct 360 ms 42592 KB Output is correct
9 Correct 317 ms 42592 KB Output is correct
10 Correct 47 ms 26200 KB Output is correct
11 Correct 1004 ms 53960 KB Output is correct
12 Correct 980 ms 53812 KB Output is correct
13 Correct 1070 ms 102768 KB Output is correct
14 Correct 48 ms 26172 KB Output is correct
15 Correct 877 ms 86672 KB Output is correct
16 Correct 624 ms 48524 KB Output is correct
17 Correct 819 ms 47776 KB Output is correct
18 Correct 851 ms 47896 KB Output is correct
19 Correct 1229 ms 69920 KB Output is correct
20 Correct 1327 ms 69236 KB Output is correct
21 Correct 317 ms 45092 KB Output is correct
22 Correct 330 ms 44116 KB Output is correct
23 Correct 546 ms 47628 KB Output is correct
24 Correct 552 ms 47852 KB Output is correct
25 Correct 872 ms 48796 KB Output is correct
26 Correct 537 ms 43372 KB Output is correct
27 Correct 460 ms 42980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 48 ms 26192 KB Output is correct
2 Correct 70 ms 26644 KB Output is correct
3 Correct 78 ms 26704 KB Output is correct
4 Correct 66 ms 26824 KB Output is correct
5 Correct 74 ms 26872 KB Output is correct
6 Correct 77 ms 26688 KB Output is correct
7 Correct 51 ms 26140 KB Output is correct
8 Correct 360 ms 42592 KB Output is correct
9 Correct 317 ms 42592 KB Output is correct
10 Correct 47 ms 26200 KB Output is correct
11 Correct 1004 ms 53960 KB Output is correct
12 Correct 980 ms 53812 KB Output is correct
13 Correct 1070 ms 102768 KB Output is correct
14 Correct 48 ms 26172 KB Output is correct
15 Correct 877 ms 86672 KB Output is correct
16 Correct 624 ms 48524 KB Output is correct
17 Correct 819 ms 47776 KB Output is correct
18 Correct 851 ms 47896 KB Output is correct
19 Correct 1229 ms 69920 KB Output is correct
20 Correct 1327 ms 69236 KB Output is correct
21 Correct 317 ms 45092 KB Output is correct
22 Correct 330 ms 44116 KB Output is correct
23 Correct 546 ms 47628 KB Output is correct
24 Correct 552 ms 47852 KB Output is correct
25 Correct 872 ms 48796 KB Output is correct
26 Correct 537 ms 43372 KB Output is correct
27 Correct 460 ms 42980 KB Output is correct
28 Correct 52 ms 26108 KB Output is correct
29 Correct 73 ms 27724 KB Output is correct
30 Correct 83 ms 27876 KB Output is correct
31 Correct 82 ms 27844 KB Output is correct
32 Correct 126 ms 28008 KB Output is correct
33 Correct 71 ms 27764 KB Output is correct
34 Correct 53 ms 27092 KB Output is correct
35 Correct 321 ms 45352 KB Output is correct
36 Correct 248 ms 44524 KB Output is correct
37 Correct 251 ms 44716 KB Output is correct
38 Correct 51 ms 27064 KB Output is correct
39 Correct 989 ms 56816 KB Output is correct
40 Correct 1373 ms 102972 KB Output is correct
41 Correct 1088 ms 56252 KB Output is correct
42 Correct 1010 ms 56304 KB Output is correct
43 Correct 51 ms 26936 KB Output is correct
44 Correct 994 ms 89468 KB Output is correct
45 Correct 681 ms 51308 KB Output is correct
46 Correct 964 ms 50732 KB Output is correct
47 Correct 853 ms 50708 KB Output is correct
48 Correct 1454 ms 71692 KB Output is correct
49 Correct 2345 ms 92428 KB Output is correct
50 Correct 1787 ms 86360 KB Output is correct
51 Correct 289 ms 49204 KB Output is correct
52 Correct 226 ms 45512 KB Output is correct
53 Correct 228 ms 45212 KB Output is correct
54 Correct 221 ms 45804 KB Output is correct
55 Correct 220 ms 45796 KB Output is correct
56 Correct 595 ms 50868 KB Output is correct
57 Correct 845 ms 50712 KB Output is correct
58 Correct 825 ms 46716 KB Output is correct
59 Correct 550 ms 46184 KB Output is correct