The solution to this problem is a doubly linked list, the data structure itself is not complex, but for four cases of processing is not detailed, mainly reflected in the following points:
- Classification discussion is not comprehensive, without considering special circumstances (itself does not need to operate, need to interchange two elements adjacent)
- No consideration of the effect on other operations after the change of State 4
- No flexible use of mathematical knowledge (courtship only needs to subtract all the odd numbers)
The following post the AC code
#include <cstdio>
#include <algorithm>
const int MAXN = 100000 + 10;
int LEFT[MAXN];
int RIGHT[MAXN];
int S[MAXN];
using namespace Std;
void link (int x,int y) {
Right[x] = y;
Left[y] = x;
}
int main () {
#ifdef DEBUG
Freopen ("6.5.in", "R", stdin);
#endif
int n, m, num = 0;
while (scanf ("%d%d", &n, &m) ==2) {
for (int i = 1; I <= n; i++) {
Right[i]= (i+1);
Left[i]=i-1;
}
Right[0]=1;
Left[0]=n;
int op,x,y;
int INV = 0;
while (m--) {
scanf ("%d", &op);
if (op = = 4) Inv=!inv;
else {
scanf ("%d%d", &x, &y);
if (op = = 3 && right[y] = = X) swap (x, y);
if (OP! = 3 && Inv) op = 3-op;
if (op = = 1 && X = = Left[y]) continue;
if (op = = 2 && X = = Right[y]) continue;
int lx= Left[x],rx = right[x],ly = Left[y], RY = Right[y];
if (op = = 1) {
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) {
if (right[x] = = Y) {
Link (LX, y); Link (y, X); Link (X, RY);
}
else{
Link (LX, y); Link (y, RX); Link (LY, X); link (x,ry);
}
}
}
}
A long long ans = 0;
int j=0;
for (int i = 1;i<=n; i++) {
J=RIGHT[J];
if (i% 2 = = 1) ans+=j;
}
if (inv && n%2 = = 0) ans = (long Long) n * (n+1)/2-ans;
printf ("Case%d:%lld\n", ++num, ans);
}
return 0;
}
Example 6-5 Boxes in a line uVa12657