P3391 文艺平衡树(Splay)

题目背景

这是一道经典的Splay模板题——文艺平衡树。

题目描述

您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1

输入格式:

第一行为n,m n表示初始序列有n个数,这个序列依次是 (1,2, \cdots n-1,n)(1,2,⋯n−1,n) m表示翻转操作次数

接下来m行每行两个数 [l,r][l,r] 数据保证 1 \leq l \leq r \leq n 1≤l≤r≤n

输出格式:

输出一行n个数字,表示原始序列经过m次变换后的结果

输入样例#1:

5 3
1 3
1 3
1 4

输出样例#1:

4 3 2 1 5

说明

n,m≤100000





我就想水一篇,你要咋地QAQ?


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5, L = 0, R = 1;
struct lpl{
    int data, size, tag, fa, son[2];
}node[maxn];
int n, m, cnt, root;

namespace Splay{
    inline void pushdown(int t)
    {
        if(!node[t].tag) return; node[t].tag = 0;
        swap(node[t].son[L], node[t].son[R]);
        node[node[t].son[L]].tag ^= 1; node[node[t].son[R]].tag ^= 1;
    }

    inline void update(int t){node[t].size = node[node[t].son[L]].size + node[node[t].son[R]].size + 1;}

    inline void rotate(int t)
    {
        int fa = node[t].fa, grdfa = node[fa].fa, which = (node[fa].son[R] == t);
        node[grdfa].son[node[grdfa].son[R] == fa] = t; node[t].fa = grdfa;
        node[node[t].son[which ^ 1]].fa = fa; 
        node[fa].son[which] = node[t].son[which ^ 1];
        node[t].son[which ^ 1] = fa;    node[fa].fa = t;
        update(fa); update(t);
    }


    inline void splay(int t, int k)
    {
        while(node[t].fa != k){
            int fa = node[t].fa, grdfa = node[fa].fa;
            if(grdfa != k){
                if((node[fa].son[R] == t) ^ (node[grdfa].son[R] == fa)) rotate(t);
                else rotate(fa);
            }
            rotate(t);
        }
        if(!k) root = t;
    }

    inline void insert(int t)
    {
        int now = root, fa = 0;
        while(now){
            fa = now;
            if(node[now].data < t) now = node[now].son[R];
            else now = node[now].son[L];
        }
        node[fa].son[t > node[fa].data] = ++cnt; node[cnt].data = t;
        node[cnt].fa = fa; node[cnt].size = 1;
        splay(cnt, 0);
    }

    int Find(int t, int k)
    {
        pushdown(t);
        if(k == node[node[t].son[L]].size + 1) return t;
        if(k <= node[node[t].son[L]].size) return Find(node[t].son[L], k);
        return Find(node[t].son[R], k - (node[node[t].son[L]].size + 1));
    }
    
    void print(int t)
    {
        pushdown(t);
        if(node[t].son[L]) print(node[t].son[L]);
        if(2 <= node[t].data && node[t].data <= n + 1) printf("%d ", node[t].data - 1);
        if(node[t].son[R]) print(node[t].son[R]);
    }
}

inline void workk(int l, int r)
{
    int LL, RR;
    LL = Splay::Find(root, l - 1), RR = Splay::Find(root, r + 1);
    Splay::splay(LL, 0); 
    Splay::splay(RR, LL);
    node[node[node[root].son[R]].son[L]].tag ^= 1;
}

int main()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n + 2; ++i) Splay::insert(i);
     while(m--){
         int l, r;
         scanf("%d%d", &l, &r); l++; r++;
         workk(l, r);
     }
     Splay::print(root);
     return 0;
}

转载于:https://www.cnblogs.com/LLppdd/p/9578268.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值