bnu 33967 Boxes in a Line 链表模拟

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include <cstdlib>
#include <queue>

#define REP(i, n) for(int i=0; i<n; i++)
#define FF(i, a, b) for(int i=a; i<b; i++)
#define FD(i, a, b) for(int i=a; i>=b; i--)
#define CLR(a, b) memset(a, b, sizeof(a))
#define PB push_back
using namespace std;
const int maxn = 100010;
const int INF = 100000007;
typedef long long LL;

int l[maxn], r[maxn];
int n, m;
int rev;

void init()
{
    REP(i, n + 2)///!!!0
        l[i] = i - 1;
    FD(i, n + 1, 0)
        r[i] = i + 1;
    r[n + 1] = -1;///!!!n + 1
}


void _remove(int x)///remove x
{
    int lx = l[x];
    int rx = r[x];

    r[lx] = rx;
    l[rx] = lx;
}

void _insert(int x, int y)///insert x to the left of y
{
    int z = l[y];
    r[z] = x;
    l[y] = x;

    l[x] = z;
    r[x] = y;
}

int nc;
void solve()
{
    int op, x, y, z;
    int lx, ly, rx, ry;
    rev = 0;
    while (m--)
    {
        scanf("%d", &op);
        if ((op == 1 && !rev) || (op == 2 && rev))
        {
            scanf("%d%d", &x, &y);
            if (l[y] != x)
            {
                _remove(x);
                _insert(x, y);
            }
        }
        else if ((op == 2 && !rev) || (op == 1 && rev))
        {
            scanf("%d%d", &x, &y);
            if (r[y] != x)
            {
                _remove(x);
                _insert(x, r[y]);
            }
        }
        else if (op == 3)
        {
            scanf("%d%d", &x, &y);
            if (r[x] == y)
            {
                _remove(x);
                _insert(x ,r[y]);
            }
            else if (l[x] == y)
            {
                _remove(x);
                _insert(x, y);
            }
            else
            {
                rx = r[x];
                ry = r[y];
                _remove(x); _insert(x, ry);
                _remove(y); _insert(y, rx);
            }
        }
        else rev ^= 1;
    }

    long long ans = 0;
    int isget, id;
    if (rev)
    {
        isget = 1;
        id = l[n + 1];
        while (id != 0)
        {
            ans += isget * id;
            id = l[id];
            isget ^= 1;
        }
    }
    else
    {
        isget = 1;
        id = r[0];
        while (id != n + 1)
        {
            ans += isget * id;
            id = r[id];
            isget ^= 1;
        }
    }
    printf("Case %d: ", nc++);
    printf("%lld\n", ans);
}

int main()
{
    int x, y;
    nc = 1;
    while (~scanf("%d%d", &n, &m))
    {
        init();
        solve();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值