对拍

rand.cpp

#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
int main()
{
	freopen("q.in","w",stdout);
	srand(time(0));
	int n=rand()%200+1,m=rand()%200+1;
	cout<<n<<endl;
	for(int i=2;i<=n;i++){
		int u=rand()%i+1;
		if(u==i)u--;
		cout<<i<<' '<<u<<endl;
	}
	cout<<m<<endl;
	for(int i=1;i<=m;i++){
		int p=rand()%2;
		if(p){
			cout<<'C'<<' '<<rand()%n+1<<endl;
		}
		else cout<<'G'<<endl;
	}
}

dp.cpp

#include<bits/stdc++.h>
using namespace std;
int cnt,n;
int main()
{
	// cin>>n;
	n=100;
	for(int i=1;i<=n;i++){
		cout<<i<<" ";
		system("./rand; ./a; ./q");
		if(system("diff q.out std.out")){
			puts("WA");
			return 0;
		}
		printf("AC\n");
	}
}

q.cpp

#include<bits/stdc++.h>
using namespace std;
const int N=5e5+100;
int n,m,a,b,cnt,head[N],maxn[N],siz[N],sum,root,rt;
char ch;
int num,fa[N],d[N],id[N],c,depth[N];
bool black[N],vis[N];
int _siz[N],_son[N],dad[N],top[N];
int t[N*20],tot,rrt[N],chi[N*20][2],rd[N*20],sz[N*20],xr[N*20],fir[N],sec[N];
struct edge{
	int to,nxt;
}e[N<<1];
vector<int>v[N];
vector<int>son[N];
struct Treap{
	int add(int x){
		tot++;
		t[tot]=x;
		sz[tot]=xr[tot]=1;
		rd[tot]=rand();
		return tot;
	}
	void up(int r){
		sz[r]=sz[chi[r][0]]+sz[chi[r][1]]+xr[r];
	}
	void rotate(int &r,int d){
		int x=chi[r][d];
		chi[r][d]=chi[x][d^1];
		chi[x][d^1]=r;
		up(r);
		r=x;
		up(r);
	}
	void insert(int &r,int x){
		if(!r){
			r=add(x);
			return ;
		}
		sz[r]++;
		if(t[r]==x){
			xr[r]++;
			return ;
		}
		int d=(x>t[r]);
		insert(chi[r][d],x);
		if(rd[chi[r][d]]>rd[r])rotate(r,d);
	}
	void erase(int &r,int x){
		if(!r)return ;
		if(t[r]==x){
			if(xr[r]>1){
				xr[r]--;
				sz[r]--;
				return ;
			}
			if(!chi[r][0]||!chi[r][1])r=chi[r][0]+chi[r][1];
			else {
				int d=(rd[chi[r][0]]<rd[chi[r][1]]);
				rotate(r,d);
				erase(r,x);
			}
		}
		else {
			sz[r]--;
			erase(chi[r][t[r]<x],x);
		}
	}
	int find(int r,int x){
		if(!r)return 0;
		if(sz[chi[r][0]]>=x)return find(chi[r][0],x);
		else if(sz[chi[r][0]]+xr[r]>=x)return t[r];
		else return find(chi[r][1],x-sz[chi[r][0]]-xr[r]);
	}
}s[N],et[N];
void add_edge(int u,int v){
	cnt++;
	e[cnt].to=v;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
void dfs1(int u,int f){
	_siz[u]=1;
	dad[u]=f;
	depth[u]=depth[f]+1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==f)continue;
		dfs1(v,u);
		if(_siz[v]>_siz[_son[u]])_son[u]=v;
		_siz[u]+=_siz[v];
	}
}
void dfs2(int u,int t){
	top[u]=t;
	if(_son[u])dfs2(_son[u],t);
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==dad[u]||v==_son[u])continue;
		dfs2(v,v);
	}
}
int LCA(int x,int y){
	int fx=top[x],fy=top[y];
	while(fx!=fy){
		if(depth[fx]<depth[fy]){
			swap(x,y);
			swap(fx,fy);
		}
		x=dad[fx];
		fx=top[x];
	}
	if(depth[x]>depth[y])return y;
	else return x;
}
void getroot(int u,int f){
	maxn[u]=0;
	siz[u]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==f||vis[v])continue;
		getroot(v,u);
		siz[u]+=siz[v];
		maxn[u]=max(maxn[u],siz[v]);
	}
	maxn[u]=max(maxn[u],sum-siz[u]);
	if(maxn[u]<maxn[root])root=u;
}
void init(int u,int f,int an){
	s[an].insert(rrt[an],depth[fa[an]]+depth[u]-2*depth[LCA(fa[an],u)]);
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(vis[v]||v==f)continue;
		init(v,u,an);
	}
}
void dfs(int u,int f){
	vis[u]=1;
	fa[u]=f;
	init(u,f,u);
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(vis[v])continue;
		root=0;sum=siz[v];
		getroot(v,u);
		son[u].push_back(root);
		dfs(root,u);
	}
	et[u].insert(rrt[u+n],0);
	for(auto v:son[u]){
		et[u].insert(rrt[u+n],s[v].find(rrt[v],sz[rrt[v]]));
	}
}
void updata(int u,int v,bool pd){
	if(pd==0){
		while(u){
			int lca=LCA(fa[u],v),ddd=depth[fa[u]]+depth[v]-2*depth[lca];
			s[u].erase(rrt[u],ddd);
			if(sz[rrt[u]]){
				int _=s[u].find(rrt[u],sz[rrt[u]]);
				if(ddd>_){
					et[fa[u]].erase(rrt[fa[u]+n],ddd);
					et[fa[u]].insert(rrt[fa[u]+n],_);
				}
			}
			else et[fa[u]].erase(rrt[fa[u]+n],ddd);
			u=fa[u];
			if(fa[u]==u)return ;
		}
	}
	else{
		while(u){
			int lca=LCA(fa[u],v),ddd=depth[fa[u]]+depth[v]-2*depth[lca];
			if(sz[rrt[u]]){
				int _=s[u].find(rrt[u],sz[rrt[u]]);
				if(ddd>_){
					et[fa[u]].erase(rrt[fa[u]+n],_);
					et[fa[u]].insert(rrt[fa[u]+n],ddd);
				}
			}
			else et[fa[u]].insert(rrt[fa[u]+n],ddd);
			s[u].insert(rrt[u],ddd);
			if(fa[u]==u)return ;
			u=fa[u];
		}
	}
}
int main()
{
	// freopen("q.in","r",stdin);
	// freopen("q.out","w",stdout);
	srand(time(0));
	scanf("%d",&n);
	for(int i=1;i<n;i++){
		scanf("%d%d",&a,&b);
		add_edge(a,b);
		add_edge(b,a);
	}
	num=n;
	for(int i=1;i<=n;i++){
		black[i]=1;
	}
	root=0;c=n;
	maxn[0]=n+1;
	sum=n;
	getroot(1,0);rt=root;
	dfs1(root,0);
	dfs2(root,root);
	dfs(root,root);
	// for(int i=1;i<=n;i++){
	// 	cout<<i<<':';
	// 	for(int j=1;j<=sz[rrt[i+n]];j++){
	// 		cout<<et[i].find(rrt[i+n],j)<<' ';
	// 	}
	// 	puts("");
	// }
	scanf("%d",&m);
	while(m--){
		cin>>ch;
		if(ch=='G'){
			if(num==0)puts("-1");
			else if(num==1)puts("0");
			else {
				printf("%d\n",et[rt].find(rrt[rt+n],sz[rrt[rt+n]])+et[rt].find(rrt[rt+n],sz[rrt[rt+n]]-1));
			}
		}
		else{
			scanf("%d",&a);
			if(black[a]){
				num--;
				updata(a,a,0);
			}
			else{
				num++;
				updata(a,a,1);
			}
			black[a]^=1;
		}
	}
}

std.cpp

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int N=200100;
struct que{
	priority_queue<int> x,y;
	inline void push(int a){x.push(a);}
	inline void del(int a){y.push(a);}
	inline int top(){while(y.size()&&x.top()==y.top())x.pop(),y.pop();return x.top();}
	inline int size(){return x.size()-y.size();}
	inline void pop(){while(y.size()&&x.top()==y.top())x.pop(),y.pop();x.pop();}
	inline int sectop(){int a=top();pop();int b=top();push(a);return b;}
}A,B[N],C[N];
int cnt,head[N];
int light[N],tot,n,m;
int root,size[N],g[N],vis[N],all,f[N];
int dep[N],mn[N*2][24],num,dfn[N];
int Log[N];
struct edge{
	int to,nxt;
}e[N*2];
inline void add_edge(int u,int v){
	cnt++;
	e[cnt].nxt=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}
inline void dfs(int u,int f){
	dfn[u]=++num;
	dep[u]=dep[f]+1;
	mn[num][0]=dep[u];
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==f)continue;
		dfs(v,u);
		mn[++num][0]=dep[u];
	}
}
inline void getroot(int u,int f){
	g[u]=0;size[u]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==f||vis[v])continue;
		getroot(v,u);
		g[u]=max(g[u],size[v]);
		size[u]+=size[v];
	}
	g[u]=max(g[u],all-size[u]);
	if(g[u]<g[root])root=u;
}
inline int getdep(int x,int y){
	int a=dfn[x];
	int b=dfn[y];
	if(a>b)swap(a,b);
	int len=Log[b-a+1];
	return min(mn[a][len],mn[b-(1<<len)+1][len]);
}
inline int dis(int x,int y){
	return dep[x]+dep[y]-2*getdep(x,y);
}
inline void work(int u,int ff){
	C[root].push(dis(u,f[root]));
	size[u]=1;
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(v==ff||vis[v])continue;
		work(v,u);
		size[u]+=size[v];
	}
}
inline void pusha(int x){
	if(B[x].size()>=2)A.push(B[x].top()+B[x].sectop());
}
inline void dela(int x){
	if(B[x].size()>=2)A.del(B[x].top()+B[x].sectop());
}
inline void build(int u,int ff){
	f[u]=ff;vis[u]=1;
	B[u].push(0);
	work(u,0);
	for(int i=head[u];i;i=e[i].nxt){
		int v=e[i].to;
		if(vis[v])continue;
		root=0,all=size[v];
		getroot(v,0);
		v=root;
		build(root,u);
		B[u].push(C[v].top());
	}
	pusha(u);
}
inline void on(int x){
	dela(x);
	B[x].del(0);
	pusha(x);
	for(int i=x;f[i];i=f[i]){
		dela(f[i]);
		B[f[i]].del(C[i].top());
		C[i].del(dis(x,f[i]));
		if(C[i].size())B[f[i]].push(C[i].top());
		pusha(f[i]);
	}
}
inline void off(int x){
	dela(x);
	B[x].push(0);
	pusha(x);
	for(int i=x;f[i];i=f[i]){
		dela(f[i]);
		if(C[i].size())B[f[i]].del(C[i].top());
		C[i].push(dis(x,f[i]));
		B[f[i]].push(C[i].top());
		pusha(f[i]);
	}
}
void prework(){
	for(int j=1;j<=Log[num];j++)
		for(int i=1;i+(1<<j)-1<=num;i++)
			mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
}
inline int read(){
	int sum=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){sum=sum*10+ch-'0';ch=getchar();}
	return sum*f;
}
int main(){
	freopen("q.in","r",stdin);
	freopen("std.out","w",stdout);
	n=read();
	Log[0]=-1;for(int i=1;i<=200000;i++)Log[i]=Log[i>>1]+1;
	for(int i=1;i<n;i++){
		int u=read(),v=read();
		add_edge(u,v);add_edge(v,u);
	}
	dfs(1,0);
	prework();
	g[0]=n;root=0;all=n;
	getroot(1,0);
	build(root,0);
	tot=n;
	m=read();
	char s;
	while(m--){
		cin>>s;
		if(s=='G'){
			if(tot<=1)printf("%d\n",tot-1);
			else printf("%d\n",A.top());
		}
		else{
			int x=read();
			if(light[x]==0)on(x),tot--;
			else off(x),tot++;
			light[x]^=1;
		}
	}
	return 0;
}
posted @ 2024-07-24 07:16  Abnormal123  阅读(13)  评论(0编辑  收藏  举报