uva 12657(双向链表)

一定要注意swap(x, y),x, y可能相邻!

#include <cstdio>
#define N 100005
#define ll long long
int n, m;
struct node{
int l, r;
node() : l(), r() {}
node(int l_, int r_) : l(l_), r(r_) {}
}num[N]; void init()
{
for(int i = ; i <= n + ; i++)
num[i].l = i - , num[i].r = i + ;
} void move2left(int x, int y)
{
if(num[y].l == x) return ;
int l, r;
l = num[x].l, r = num[x].r;
num[l].r = r, num[r].l = l;
l = num[y].l, r = y;
num[x].r = r, num[x].l = l;
num[l].r = x, num[r].l = x;
} void move2right(int x, int y)
{
if(num[y].r == x) return ;
int l, r;
l = num[x].l, r = num[x].r;
num[l].r = r, num[r].l = l;
l = y, r = num[y].r;
num[x].r = r, num[x].l = l;
num[l].r = x, num[r].l = x;
} void Swap(int x, int y)
{
int xl, xr, yl, yr;
if(num[x].r == y) {
xl = num[x].l, yr = num[y].r;
num[xl].r = y, num[yr].l = x;
num[y].l = xl, num[y].r = x;
num[x].l = y, num[x].r = yr;
}
else if(num[x].l == y) {
xr = num[x].r, yl = num[y].l;
num[xr].l = y, num[yl].r = x;
num[y].l = x, num[y].r = xr;
num[x].l = yl, num[x].r = y;
}
else {
xl = num[x].l, xr = num[x].r;
yl = num[y].l, yr = num[y].r;
num[xl].r = y, num[xr].l = y;
num[yl].r = x, num[yr].l = x;
num[x].l = yl, num[x].r = yr;
num[y].l = xl, num[y].r = xr;
} } int main()
{
//freopen("out.txt", "w", stdout);
int cases = ;
while(~scanf("%d%d", &n, &m))
{
cases++;
init();
int op, res = ;
for(int i = ; i <= m; i++)
{
scanf("%d", &op);
int x, y;
if(op < ) scanf("%d%d", &x, &y);
if((op == && res) || (op == && !res))
move2left(x, y);
else if((op == && res) || (op == && !res))
move2right(x, y);
else if(op == )
Swap(x, y);
else res = !res;
}
ll sum = ;
if(res) {
int t = num[].r, c = ;
while(c < n) {
c++;
if(c % == ) sum += (ll)t;
//printf("%d***\n", t);
t = num[t].r;
}
}
else {
int t = num[n + ].l, c = ;
//printf("%d***\n", t);
while(c < n) {
c++;
if(c % == ) sum += (ll)t;
//printf("%d***\n", t);
t = num[t].l;
}
}
printf("Case %d: %lld\n", cases, sum);
}
return ;
}
上一篇:MySQL 日期和时间戳互相转换


下一篇:CodeForces 1567C Carrying Conundrum