水吧,慢慢调试吧。
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN (100000+5)
struct DoubleLinked{
int l,r;
};
DoubleLinked a[MAXN];
int n,m;
void build(int x,int y){
a[x].r=y;a[y].l=x;
}
int main(){
int K=1;
while(scanf("%d%d",&n,&m)==2){
for(int i=1;i<=n;i++)build(i-1,i);build(n,0);
bool isltor=true;
for(int i=0;i<m;i++){
int op,x,y;
scanf("%d",&op);
if(op==4){
isltor=!isltor;
}else{
scanf("%d%d",&x,&y);
if(!isltor&&op!=3)op=op%2+1;
if(op==3&&a[x].l==y)swap(x,y);
int XL=a[x].l,XR=a[x].r;
int YL=a[y].l,YR=a[y].r;
if(op==1&&XR==y)continue;
if(op==2&&XL==y)continue;
if(op==1){build(XL,XR);build(YL,x);build(x,y);}
else if(op==2){build(XL,XR);build(y,x);build(x,YR);}
else if(op==3){
if(XR==y){build(XL,y);build(y,x);build(x,YR);}
else{build(XL,y);build(y,XR);build(YL,x);build(x,YR);}
}
}
}
int b=0;
long long int ans=0;
for(int i=1;i<=n;i++){
b=a[b].r;
//printf("%d ",b);
if(i%2==1)ans+=b;
}
if(!isltor&&n%2==0)ans=(long long)n*(n+1)/2-ans;
printf("Case %d: %lld\n",K++,ans);
}
return 0;
}