# 代码1

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 1e5+5;

struct Node {
int l, r;
Node(int l, int r) : l(l), r(r) {}
Node() {}
};

struct List {
int n;
Node a[N];
bool st;

void init(int n) {
this->n = n;
for (int i = 0; i <= n+1; i++) {
a[i] = Node(i-1, i+1);
}
st = false;
}

void reverseState() {
st = !st;
}

void handleInsert(int x, int y, bool left) {
if (st ^ left) {
insertLeft(x, y);
} else {
insertRight(x, y);
}
}

void insertLeft(int x, int y) {
removeNode(x);
insertNode(x, a[y].l, y);
}

void insertRight(int x, int y) {
removeNode(x);
insertNode(x, y, a[y].r);
}

void swapNode(int x, int y) {
int xl = a[x].l, xr = a[x].r;
int yl = a[y].l, yr = a[y].r;
if (a[x].r == y) {
a[xl].r = y;
a[yr].l = x;
a[x] = Node(y, yr);
a[y] = Node(xl, x);
} else if (a[x].l == y) {
swapNode(y, x);
} else {
removeNode(x);
removeNode(y);
insertNode(x, yl, yr);
insertNode(y, xl, xr);
}
}

void removeNode(int x) {
int l = a[x].l, r = a[x].r;
a[l].r = r;
a[r].l = l;
}

void insertNode(int x, int l, int r) {
a[x] = Node(l, r);
a[l].r = a[r].l = x;
}

LL getAns() {
LL ans = 0;
if (n & 1) st = false;
int cur = 0;
for (int i = 1; i <= n; i++) {
if ((i & 1) ^ st) {
ans += a[cur].r;
}
cur = a[cur].r;
}
return ans;
}
};

List list;

int main() {
int n, m, op, x, y, cas = 1;
while (scanf("%d%d", &n, &m) == 2) {
list.init(n);
for (int i = 0; i < m; i++) {
scanf("%d", &op);
if (op == 4) {
list.reverseState();
} else {
scanf("%d%d", &x, &y);
if (op == 1) {
list.handleInsert(x, y, true);
} else if (op == 2) {
list.handleInsert(x, y, false);
} else {
list.swapNode(x, y);
}
}
}
printf("Case %d: %lld\n", cas++, list.getAns());
}
return 0;
}


# 代码2

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N = 1e5+5;

struct Node {
int l, r, no;
Node(int l, int r, int no) : l(l), r(r), no(no) {}
Node() {}
};

struct List {
int n;
Node a[N];
int no[N];
bool st;

void init(int n) {
this->n = n;
st = false;
for (int i = 0; i <= n+1; i++) {
a[i] = Node(i-1, i+1, i);
no[i] = i;
}
}

void reverseState() {
st = !st;
}

void handleInsert(int x, int y, bool left) {
x = no[x], y = no[y];
if (st ^ left) {
insertLeft(x, y);
} else {
insertRight(x, y);
}
}

void insertLeft(int x, int y) {
removeNode(x);
insertNode(x, a[y].l, y);
}

void insertRight(int x, int y) {
removeNode(x);
insertNode(x, y, a[y].r);
}

void swapNode(int x, int y) {
swap(no[x], no[y]);
swap(a[no[x]].no, a[no[y]].no);
}

void removeNode(int x) {
int l = a[x].l, r = a[x].r;
a[l].r = r;
a[r].l = l;
}

void insertNode(int x, int l, int r) {
a[x] = Node(l, r, a[x].no);
a[l].r = a[r].l = x;
}

LL getAns() {
if (n & 1) st = false;
LL ans = 0;
int cur = a[0].r;
for (int i = 1; i <= n; i++) {
if ((i & 1) ^ st) {
ans += a[cur].no;
}
cur = a[cur].r;
}
return ans;
}
};

List list;

int main() {
int n, m, op, x, y, cas = 1;
while (scanf("%d%d", &n, &m) == 2) {
list.init(n);
for (int i = 0; i < m; i++) {
scanf("%d", &op);
if (op == 4) {
list.reverseState();
} else {
scanf("%d%d", &x, &y);
if (op == 1) {
list.handleInsert(x, y, true);
} else if (op == 2) {
list.handleInsert(x, y, false);
} else {
list.swapNode(x, y);
}
}
}
printf("Case %d: %lld\n", cas++, list.getAns());
}
return 0;
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

ned_chu

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-25

08-17 847
12-08 36
07-27 223
02-13 372
01-31 736
03-02 405