标签:
#include<cstdio>
using namespace std;
int left[100010], right[100010];
int main() {
int n, m, times(0);
while (scanf("%d%d", &n, &m) == 2) {
for (int i = 0; i != n + 3; ++i) { //初始化左右
left[i] = i - 1;
right[i] = i + 1;
}
int op, x, y, chan(0);
while (m--) {
scanf("%d", &op);
if (op == 4) chan = !chan;
else {
scanf("%d%d", &x, &y);
if (chan && (op == 1 || op == 2)) {
if (op == 1) op = 2;
else op = 1;
}
if (op == 1) {
left[right[x]] = left[x];
right[left[x]] = right[x];
left[x] = left[y];
right[x] = y;
right[left[y]] = x;
left[y] = x;
}else if (op == 2) {
left[right[x]] = left[x];
right[left[x]] = right[x];
right[x] = right[y];
left[x] = y;
left[right[y]] = x;
right[y] = x;
}else {
int lx = left[x], ly = left[y],
rx = right[x], ry = right[y];
if (rx == y) {
left[y] = lx;
right[x] = ry;
right[y] = left[ry] = x;
left[x] = right[lx] = y;
} else if (lx == y) {
left[x] = ly;
right[y] = rx;
right[x] = left[rx] = y;
left[y] = right[ly] = x;
} else {
right[lx] = left[rx] = y;
right[ly] = left[ry] = x;
left[x] = ly;
right[x] = ry;
left[y] = lx;
right[y] = rx;
}
}
}
}
long long sum(0);
int b(0);
if (chan) {
b = n + 1;
for (int i = 1; i != n + 1; ++i) {
b = left[b];
if (i % 2) sum += b;
// printf("%d ", b); //*
}
} else {
for (int i = 1; i != n + 1; ++i) {
b = right[b];
if (i % 2) sum += b;
// printf("%d ", b); //*
}
}
printf("Case %d: %lld\n", ++times, sum);
}
return 0;
}
UVa 12657 Boxes in a Line 双向链表
标签:
原文地址:http://www.cnblogs.com/liangyongrui/p/4643875.html