#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include <cstdlib>
#include <queue>
#define REP(i, n) for(int i=0; i<n; i++)
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define CLR(a, b) memset(a, b, sizeof(a))
#define PB push_back
using namespace std;
const int maxn = 100010;
const int INF = 100000007;
typedef long long LL;
int l[maxn], r[maxn];
int n, m;
int rev;
void init()
{
REP(i, n + 2)///!!!0
l[i] = i - 1;
FD(i, n + 1, 0)
r[i] = i + 1;
r[n + 1] = -1;///!!!n + 1
}
void _remove(int x)///remove x
{
int lx = l[x];
int rx = r[x];
r[lx] = rx;
l[rx] = lx;
}
void _insert(int x, int y)///insert x to the left of y
{
int z = l[y];
r[z] = x;
l[y] = x;
l[x] = z;
r[x] = y;
}
int nc;
void solve()
{
int op, x, y, z;
int lx, ly, rx, ry;
rev = 0;
while (m--)
{
scanf("%d", &op);
if ((op == 1 && !rev) || (op == 2 && rev))
{
scanf("%d%d", &x, &y);
if (l[y] != x)
{
_remove(x);
_insert(x, y);
}
}
else if ((op == 2 && !rev) || (op == 1 && rev))
{
scanf("%d%d", &x, &y);
if (r[y] != x)
{
_remove(x);
_insert(x, r[y]);
}
}
else if (op == 3)
{
scanf("%d%d", &x, &y);
if (r[x] == y)
{
_remove(x);
_insert(x ,r[y]);
}
else if (l[x] == y)
{
_remove(x);
_insert(x, y);
}
else
{
rx = r[x];
ry = r[y];
_remove(x); _insert(x, ry);
_remove(y); _insert(y, rx);
}
}
else rev ^= 1;
}
long long ans = 0;
int isget, id;
if (rev)
{
isget = 1;
id = l[n + 1];
while (id != 0)
{
ans += isget * id;
id = l[id];
isget ^= 1;
}
}
else
{
isget = 1;
id = r[0];
while (id != n + 1)
{
ans += isget * id;
id = r[id];
isget ^= 1;
}
}
printf("Case %d: ", nc++);
printf("%lld\n", ans);
}
int main()
{
int x, y;
nc = 1;
while (~scanf("%d%d", &n, &m))
{
init();
solve();
}
}
bnu 33967 Boxes in a Line 链表模拟
最新推荐文章于 2018-12-08 10:36:00 发布