社区讨论

为什么TLE?

UVA12657移动盒子 Boxes in a Line参与者 5已保存回复 5

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
5 条
当前快照
1 份
快照标识符
@lob87dhn
此快照首次捕获于
2023/10/29 16:45
2 年前
此快照最后确认于
2023/11/03 22:54
2 年前
查看原帖
#1TLE了,其他点不知道
按理论上不会啊?
CPP
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
int L[100005],R[100005];
void del(int x){
	R[L[x]]=R[x];
	L[R[x]]=L[x];
}
void lplus(int x,int k){
	L[k]=L[x];
	R[k]=x;
	R[L[x]]=k;
	L[x]=k;
}
void rplus(int x,int k){
	R[k]=R[x];
	L[k]=x;
	L[R[x]]=k;
	R[x]=k;
}
int n,m,tot;
void init(){
	for(int i=0;i<=n+1;i++)
		L[i]=i-1;
	for(int i=0;i<=n+1;i++)
		R[i]=i+1;
}
void R1(){
	int x,y;
	scanf("%d%d",&x,&y);
	if(L[y]==x)
		return; 
	del(x);
	lplus(y,x);
}
void R2(){
	int x,y;
	scanf("%d%d",&x,&y);
	if(R[y]==x)
		return; 
	del(x);
	rplus(y,x);
}
void R3(){
	int x,y;
	scanf("%d%d",&x,&y);
	if(L[x]==y){
		swap(L[x],L[y]);
		swap(R[x],R[y]);
	}else{
		int z=L[x];
		del(x);
		lplus(y,x);
		del(y);
		rplus(z,y);
	}
}
void R4(){
	for(int i=0;i<=n+1;i++)
		swap(L[i],R[i]);
}
void print(int x){
	int cnt=0;
	ll ans=0;
	if(x==0)
		for(int i=R[0];i<=n;i=R[i]){
			cnt++;
			if(cnt%2)
				ans+=(ll)(i);
		}
	else
		for(int i=R[n+1];i;i=R[i]){
			cnt++;
			if(cnt%2)
				ans+=(ll)(i);
		}
	printf("Case %d: %lld\n",++tot,ans);
}
int main(){
	while(~scanf("%d%d",&n,&m)){
		init();
		int cnt=0;
		for(int i=1;i<=m;i++){
			int opt;
			scanf("%d",&opt);
			if(opt==1)
				R1();
			else if(opt==2)
				R2();
			else if(opt==3)
				R3();
			else
				R4(),cnt++;
		}
		print(cnt%2);
	}
	return 0;
}

回复

5 条回复,欢迎继续交流。

正在加载回复...