牛客多校0726C

C、Shuffle Cards | 时间限制:1秒 | 内存限制:256M
Eddy likes to play cards game since there are always lots of randomness in the game. For most of the cards game, the very first step in the game is shuffling the cards. And, mostly the randomness in the game is from this step. However, Eddy doubts that if the shuffling is not done well, the order of the cards is predictable!

To prove that, Eddy wants to shuffle cards and tries to predict the final order of the cards. Actually, Eddy knows only one way to shuffle cards that is taking some middle consecutive cards and put them on the top of rest. When shuffling cards, Eddy just keeps repeating this procedure. After several rounds, Eddy has lost the track of the order of cards and believes that the assumption he made is wrong. As Eddy’s friend, you are watching him doing such foolish thing and easily memorizes all the moves he done. Now, you are going to tell Eddy the final order of cards as a magic to surprise him.

Eddy has showed you at first that the cards are number from 1 to N from top to bottom.

For example, there are 5 cards and Eddy has done 1 shuffling. He takes out 2-nd card from top to 4-th card from top(indexed from 1) and put them on the top of rest cards. Then, the final order of cards from top will be [2,3,4,1,5].
输入描述:
The first line contains two space-separated integer N, M indicating the number of cards and the number of shuffling Eddy has done.
Each of following M lines contains two space-separated integer pi, si indicating that Eddy takes pi-th card from top to (pi+si-1)-th card from top(indexed from 1) and put them on the top of rest cards.

1 ≤ N, M ≤ 105
1 ≤ pi ≤ N
1 ≤ si ≤ N-pi+1
输出描述:
Output one line contains N space-separated integers indicating the final order of the cards from top to bottom.
示例1
输入
5 1
2 3
输出
2 3 4 1 5
示例2
输入
5 2
2 3
2 3
输出
3 4 1 2 5
示例3
输入
5 3
2 3
1 4
2 4
输出
3 4 1 5 2
4

第一次使用伸展树.翻几翻就可以实现挪移了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>

#define Key_value ch[ch[root][1]][0]
using namespace std;
const int maxn = 100010;
const int INF = 1000000000;
int a[maxn], N, M;
int ch[maxn][2], pre[maxn], size[maxn], key[maxn], rev[maxn];
int sum[maxn], setv[maxn];
int root, tot1, tot2, s[maxn];
int lx[maxn], rx[maxn], mx[maxn];

void NewNode(int &r, int f, int val) {
    if (tot2)r = s[tot2--];
    else r = ++tot1;
    ch[r][0] = ch[r][1] = 0;
    pre[r] = f;
    key[r] = val;
    rev[r] = setv[r] = 0;
    sum[r] = val;
    size[r] = 1;
    lx[r] = rx[r] = mx[r] = val;
}

void update_setv(int r, int c) {
    if (!r)return;
    setv[r] = 1;
    key[r] = c;
    sum[r] = size[r] * c;
    lx[r] = rx[r] = mx[r] = max(c, c * size[r]);
}

void pushup(int r) {
    int lc = ch[r][0], rc = ch[r][1];
    size[r] = size[lc] + size[rc] + 1;
    sum[r] = sum[rc] + sum[lc] + key[r];
    lx[r] = max(lx[lc], sum[lc] + key[r] + max(0, lx[rc]));
    rx[r] = max(rx[rc], sum[rc] + key[r] + max(0, rx[lc]));
    mx[r] = max(mx[lc], mx[rc]);
    mx[r] = max(mx[r], max(0, rx[lc]) + key[r] + max(0, lx[rc]));
}

void update_rev(int r) {
    if (!r)return;
    swap(ch[r][0], ch[r][1]);
    swap(lx[r], rx[r]);
    rev[r] ^= 1;
}

void pushdown(int r) {
    if (setv[r]) {
        update_setv(ch[r][0], key[r]);
        update_setv(ch[r][1], key[r]);
        setv[r] = 0;
    }
    if (rev[r]) {
        update_rev(ch[r][0]);
        update_rev(ch[r][1]);
        rev[r] = 0;
    }

}

void build(int &x, int l, int r, int f) {
    if (l > r)return;
    int mid = (l + r) >> 1;
    NewNode(x, f, a[mid]);
    build(ch[x][0], l, mid - 1, x);
    build(ch[x][1], mid + 1, r, x);
    pushup(x);
}

void init() {
    root = tot1 = tot2 = 0;
    ch[root][0] = ch[root][1] = size[root] = key[root] = pre[root] = 0;
    sum[root] = rev[root] = 0;
    lx[root] = rx[root] = mx[root] = -INF;
    NewNode(root, 0, -1);
    NewNode(ch[root][1], root, -1);
    build(Key_value, 1, N, ch[root][1]);
    pushup(ch[root][1]);
    pushup(root);
}

void Rotate(int x, int kind) {
    int y = pre[x];
    pushdown(y);
    pushdown(x);
    ch[y][!kind] = ch[x][kind];
    pre[ch[x][kind]] = y;
    if (pre[y])ch[pre[y]][ch[pre[y]][1] == y] = x;
    pre[x] = pre[y];
    ch[x][kind] = y;
    pre[y] = x;
    pushup(y);
}

void Splay(int r, int goal) {
    pushdown(r);
    while (pre[r] != goal) {
        if (pre[pre[r]] == goal) {
            pushdown(pre[r]);
            pushdown(r);
            Rotate(r, ch[pre[r]][0] == r);
        } else {
            pushdown(pre[pre[r]]);
            pushdown(pre[r]);
            pushdown(r);
            int y = pre[r];
            int kind = (ch[pre[y]][0] == y);
            if (ch[y][kind] == r) {
                Rotate(r, !kind);
                Rotate(r, kind);
            } else {
                Rotate(y, kind);
                Rotate(r, kind);
            }
        }
    }
    pushup(r);
    if (goal == 0)root = r;
}

int get_kth(int r, int k) {
    pushdown(r);
    int t = size[ch[r][0]] + 1;
    if (t == k)return r;
    if (t > k)return get_kth(ch[r][0], k);
    else return get_kth(ch[r][1], k - t);
}

void Insert(int pos, int tot) {
    Splay(get_kth(root, pos + 1), 0);
    Splay(get_kth(root, pos + 2), root);
    build(Key_value, 1, tot, ch[root][1]);
    pushup(ch[root][1]);
    pushup(root);
}

void erase(int r) {
    if (r) {
        s[++tot2] = r;
        erase(ch[r][0]);
        erase(ch[r][1]);
    }
}

void Delete(int pos, int tot) {
    Splay(get_kth(root, pos), 0);
    Splay(get_kth(root, pos + 1 + tot), root);
    erase(Key_value);
    pre[Key_value] = 0;
    Key_value = 0;
    pushup(ch[root][1]);
    pushup(root);
}

void MakeSame(int pos, int tot, int c) {
    Splay(get_kth(root, pos), 0);
    Splay(get_kth(root, pos + 1 + tot), root);
    update_setv(Key_value, c);
    pushup(ch[root][1]);
    pushup(root);
}

void Reverse(int pos, int tot) {
    Splay(get_kth(root, pos), 0);
    Splay(get_kth(root, pos + 1 + tot), root);
    update_rev(Key_value);
    pushup(ch[root][1]);
    pushup(root);
}

int GetSum(int pos, int tot) {
    Splay(get_kth(root, pos), 0);
    Splay(get_kth(root, pos + 1 + tot), root);
    pushup(ch[root][1]);
    pushup(root);
    return sum[Key_value];
}

void MaxSum(int pos, int tot) {
    Splay(get_kth(root, pos), 0);
    Splay(get_kth(root, pos + 1 + tot), root);
    printf("%d\n", mx[Key_value]);
}

void print() {
    for (int i = 1; i < N; ++i)
        printf("%d ", GetSum(i, 1));
    printf("%d\n", GetSum(N, 1));
}

int pos, tot;

int main() {
    //freopen("../in", "r", stdin);
    scanf("%d%d", &N, &M);
    for (int i = 1; i <= N; ++i) a[i] = i;
    init();
    while (M--) {
        scanf("%d%d",&pos,&tot);
        if (pos == 1) continue;
        if (pos + tot - 1 == N) {
            Reverse(pos,tot);
            Reverse(1,pos-1);
            Reverse(1,N);
        } else {
            if (N - pos + 1 > 1) Reverse(pos, N - pos + 1);
            if (N - pos - tot + 1 > 1) Reverse(pos, N - pos - tot + 1);
            if (N - tot > 1) Reverse(1, N - tot);
            Reverse(1, N);
        }
    }

    print();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值