You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simulate 4 kinds 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 executing 2 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 n from 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
题目类型:双重链表
算法分析:使用left[x]表示x的左边节点标号,right[x]表示x的右边节点标号,之后按照题目模拟即可。注意答案可能超出int的范围
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
/************************************************** filename :n.cpp author :maksyuki created time :2018/1/28 12:22:32 last modified :2018/1/28 13:00:56 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int Right[maxn], Left[maxn]; void Link(int a, int b) { Right[a] = b, Left[b] = a; } int main() { #ifdef LOCAL CFF; //CFO; #endif int n, m, id = 1; while(cin >> n >> m) { for(int i = 1; i <= n; i++) { Left[i] = i - 1; Right[i] = (i + 1) % (n + 1); } Left[0] = n, Right[0] = 1; int op, vx, vy; bool inv = false; for(int i = 1; i <= m; i++) { cin >> op; if(op == 4) { inv = !inv; continue; } cin >> vx >> vy; if(op == 3 && Right[vy] == vx) swap(vx, vy); if(op < 3 && inv) op = 3 - op; if(op == 1 && vx == Left[vy]) continue; if(op == 2 && vx == Right[vy]) continue; int lx = Left[vx], rx = Right[vx], ly = Left[vy], ry = Right[vy]; if(op == 1) Link(lx, rx), Link(ly, vx), Link(vx, vy); else if(op == 2) Link(lx, rx), Link(vy, vx), Link(vx, ry); else if(op == 3) { if(Right[vx] == vy) Link(lx, vy), Link(vy, vx), Link(vx, ry); else Link(lx, vy), Link(vy, rx), Link(ly, vx), Link(vx, ry); } } int p = 0; LL ans = 0; for(int i = 1; i <= n; i++) { p = Right[p]; if(i % 2) ans += p; } if(inv && !(n % 2)) ans = ((LL) n * (n + 1) / 2) - ans; cout << "Case " << id++ << ": " << ans << endl; } return 0; } |
- « 上一篇:uva442
- uva679:下一篇 »