题目大意:
这道题目是考察双向链表的使用,下面是我用c语言实现的代码:
#include <stdio.h>
#define maxn 100000+10
int m,n;
int pre[maxn],next[maxn];
void init(){
int i;
for(i=1;i<=n;i++){pre[i] = i-1;next[i] = i+1; }
pre[n+1] = n;
next[0] = 1;
}
void move(int x,int y,int dir){
if(dir == 1){
next[pre[x]] = next[x];
pre[next[x]] = pre[x];
pre[x] = pre[y];
next[x] = y;
next[pre[y]] = x;
pre[y] = x;
}
else if(dir == 2){
next[pre[x]] = next[x];
pre[next[x]] = pre[x];
pre[x] = y;
next[x] = next[y];
pre[next[y]] = x;
next[y] = x;
}
}
int main(){
int kase = 0;
while(scanf("%d%d",&n,&m)){
init();
int i,x,y,op,dir = 1;
for(i=0;i<m;i++){
scanf("%d",&op);
if(op == 4)dir = !dir;
else{
scanf("%d%d",&x,&y);
if(op < 3){
if(dir)move(x,y,op);
else move(x,y,3-op);
}else{
int lx = pre[x];
move(x,y,1);
move(y,lx,2);
}
}
}
int b = 0;
long long ans = 0;
for(i = 1;i <= n;i++){
b = next[b];
if(i % 2 == 1)ans += b;
}
if(!dir && n % 2 == 0) ans = (long long)(1+n)*n/2 - ans;
printf("Case %d: %lld\n",++kase,ans);
}
return 0;
}