You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4kinds of commands:
• 1 X Y : move box X to the left to Y (ignore this if X is already the left of Y )
• 2 X Y : move box X to the right to Y (ignore this if X is already the right of Y )
• 3 X Y : swap box X and Y
• 4: reverse the whole line.
Commands are guaranteed to be valid, i.e. X will be not equal to Y .For example, if n = 6, after executing 1 1 4, the line becomes 2 3 1 4 5 6. Then after executing2 3 5, the line becomes 2 1 4 5 3 6. Then after executing 3 1 6, the line becomes 2 6 4 5 3 1.Then after executing 4, then line becomes 1 3 5 4 6 2
Input
There will be at most 10 test cases. Each test case begins with a line containing 2 integers n, m(1 ≤ n, m ≤ 100, 000). Each of the following m lines contain a command.
Output
For each test case, print the sum of numbers at odd-indexed positions. Positions are numbered 1 to nfrom left to right.
Sample Input
6 4
1 1 4
2 3 5
3 1 6
4
6 3
1 1 4
2 3 5
3 1 6
100000 1
4
Sample Output
Case 1: 12
Case 2: 9
Case 3: 2500050000
使用双向链表解决,静态链表,挺简单的
#include<iostream>
using namespace std;
const int size = 100000 + 5;
void Link(int L, int R, int* right, int*left)
{
right[L] = R;
left[R] = L;
}
void op1(int X, int Y, int* right, int*left) //操作一
{
int lx = left[X];
int rx = right[X];
int ly = left[Y];
Link(X, Y, right, left);
Link(ly, X, right, left);
Link(lx, rx, right, left);
}
void op2(int X, int Y, int* right, int*left) //操作二
{
int lx = left[X];
int rx = right[X];
int ry = right[Y];
Link(Y, X, right, left);
Link(X, ry, right, left);
Link(lx, rx, right, left);
}
void op3(int X, int Y, int* right, int*left) //操作三
{
int lx = left[X];
int rx = right[X];
int ly = left[Y];
int ry = right[Y];
Link(X, ry, right, left);
Link(ly, X, right, left);
Link(Y, rx, right, left);
Link(lx, Y, right, left);
}
int main()
{
int right[size] = {0};
int left[size] = {0};
int n, m, kcase = 0;
while (cin >> n >> m)
{
//初始化
for (int i = 1; i <= n; i++)
{
left[i] = i - 1;
right[i] = i + 1;
}
left[0] = n;
right[0] = 0;
int op, X, Y, inv = 0; //inv是一个操作,如果进行了操作就变为1
while (m--)
{
cin >> op;
if (op == 4)inv = 1;
else
{
cin >> X >> Y;
if (op == 3 && right[Y] == X)
{
int rx = right[X];
int ly = left[Y];
Link(ly, X, right, left);
Link(Y, rx, right, left);
Link(X, Y, right, left);
}
else if (op == 3 && right[X] == Y)
{
int lx = left[X];
int ry = right[Y];
Link(X, ry, right, left);
Link(lx, Y, right, left);
Link(Y, X, right, left);
}
else if (op == 3 && right[X] != Y&&right[Y] != X)
{
op3(X, Y, right, left);
}
else if (op == 1 && inv)op2(X, Y, right, left);
else if (op == 2 && inv)op1(X, Y, right, left);
else if (op == 1 && X == left[Y])continue;
else if (op == 2 && Y == right[X])continue;
else if (op == 1 && !inv)op1(X, Y, right, left);
else if (op == 2 && !inv)op2(X, Y, right, left);
}
}
int b = 0;
long long result = 0;
for (int i = 1; i <= n; i++)
{
b = right[b];
if (i % 2 != 0)result += b;
}
if (inv&&n % 2 == 0)result = (long long)n*(n + 1) / 2 - result;
cout << "Case " << ++kcase << ": " << result << endl;
}
return 0;
}
**如果数据结构上的某个操作很耗时,有时可以用加标记的方式处理,而不需真的执行那个操作,但同时,该数据结构的所有其他操作都要考虑这个标记。