#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 100000 + 10; int L[maxn], R[maxn]; void link(int X, int Y) { R[X] = Y; L[Y] = X; } void Swap(int& x, int& y) { int t = x; x = y; y = t; } int main() { int n, m, kase = 0; while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= n; i++) { L[i] = i - 1; R[i] = (i + 1) % (n + 1); } R[0] = 1; L[0] = n; // 给头尾节点赋好值 /*inv等于0表示链表和初始时一样的方向 inv等于1的话说明链表和初始时的方向相反,因为链表的方向由于4的关系只有这两种方向来回变 所以用个标记来记录下方向,就不用耗费大量时间真的转置链表了,其实感觉跟线段树的懒惰标记 有那么点像*/ int inv = 0, op, X, Y, LX, RX, LY, RY;; for (int k = 1; k <= m; k++) { scanf("%d", &op); if (op != 4) scanf("%d%d", &X, &Y); if (op == 4) { inv = !inv; //标记链表转置了 continue; } //因为如果inv跟初始的方向相反的话,那么左右颠倒 if (inv && op != 3) op = 3 - op; //debug op != 3 if (op == 1 && R[X] == Y) continue; if (op == 2 && L[X] == Y) continue; if (op == 3 && R[Y] == X) Swap(X, Y); LX = L[X]; RX = R[X]; LY = L[Y]; RY = R[Y]; if (op == 1) { //相当于执行链表操作 /* struct node { int n; node *pre, *next; }; node *X; node *Y; link(LX,RX) : X->pre->next = X->next; X->next->pre = X->pre; link(LY,X) : Y->pre->next = X; X->pre = Y->pre; link(X,Y) : X->next = Y; Y->pre = X; 下同 */ link(LX, RX); link(LY, X); link(X, Y); } else if (op == 2) { link(LX, RX); link(Y, X); link(X, RY); } else if (op == 3) { //debug link(LX, Y); link(Y, X); link(X, RY); //要特判一下相邻的情况 if (R[X] == Y) { link(LX, Y); link(Y, X); link(X, RY); } else { link(LX, Y); link(Y, RX); link(LY, X); link(X, RY); } } } int b = 0; long long ans = 0; //单向遍历链表 for (int i = 1; i <= n; i++) { b = R[b]; if (i % 2) ans += b; } if (inv && n % 2 == 0) ans = (long long)n * (n + 1) / 2 - ans; printf("Case %d: %lld\n", ++kase, ans); //不用%I64d,不然错。。。 } return 0; }