注意操作3是通过数字寻找位置的,不会受是否翻转的影响
#include <cstdio>
#include <algorithm>
using namespace std;#define BG 100005int be[BG],ne[BG];
int w=1;
int main()
{int n,m;while(scanf("%d%d",&n,&m)!=EOF){int i;int x,a,b;int tn,tb;int inv=0;//use inv to show whether the link has been inverted for odd times.be[0]=n;ne[0]=1;for(i=1;i<n;i++)ne[i]=i+1;ne[n]=0;for(i=2;i<=n;i++)be[i]=i-1;be[1]=0;for(i=0;i<m;i++){scanf("%d",&x);if(x==4)inv=!inv;//fetch the opposite valueelse{if(x!=3&&inv==1) x=3-x;//if x is 1 or 2,change the direction of the order.if(x==1){scanf("%d%d",&a,&b);if(be[b]!=a){ne[be[a]]=ne[a];be[ne[a]]=be[a];be[a]=be[b];ne[a]=b;ne[be[b]]=a;be[b]=a;}}else if(x==2){scanf("%d%d",&a,&b);if(ne[b]!=a){ne[be[a]]=ne[a];be[ne[a]]=be[a];ne[a]=ne[b];be[a]=b;be[ne[b]]=a;ne[b]=a;}}else if(x==3)//operation 3 isn't influenced by the inv{scanf("%d%d",&a,&b);ne[be[a]]=b;be[ne[a]]=b;tn=ne[b];tb=be[b];ne[b]=ne[a];be[b]=be[a];ne[tb]=a;be[tn]=a;ne[a]=tn;be[a]=tb;}}
// else if(x==4)//waste too much time.
// inv=!inv;
// {
//
// for(i=0;i<=n;i++)
// {
// t=be[i];
// be[i]=ne[i];
// ne[i]=t;
// }
// }}i=ne[0];long long sum=0;int count=1;if(n%2==0&&inv==1)i=ne[ne[0]];while(i!=0){if(count%2!=0)sum+=i;i=ne[i];count++;}printf("Case %d: ",w);printf("%lld\n",sum);w++;}
}