P3391 文艺平衡树Splay区间翻转模板

//#define LOCAL
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define sz(a) (int)a.size()
#define INF 0x3f3f3f3f
#define DNF 0x7f7f7f7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define PiL pair <ll , ll>
#define PLL pair <ll , ll>
#define Pii pair <int , int>
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define LF putchar('\n')
#define SP putchar(' ')
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)

template<typename T>
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template<typename T, typename... Args>
void read(T &first, Args& ... args) {read(first);read(args...);}
template<typename T>
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template<typename T, typename ... Ts>
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}
using namespace std;
const int N = 300005;
int n , m , val[N];
struct Splay_Tree
{
    int tag[N] , val[N] , siz[N], fa[N], ch[N][2], rt , tot;
    int get (int x)  { return x == ch[fa[x]][1]; }
    void push_up (int x) {siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1;}
    void push_down (int x)
    {
        if (tag[x])
        {
            swap (ch[x][0] , ch[x][1]);
            tag[ch[x][0]] ^= 1, tag[ch[x][1]] ^= 1, tag[x] ^= 1;
        }
    }
    void rotate (int x)
    {
        int y = fa[x] , z = fa[y] , chk = get(x);
        ch[y][chk] = ch[x][chk ^ 1];
        fa[ch[x][chk ^ 1]] = y;
        ch[x][chk ^ 1] = y;
        fa[y] = x;
        fa[x] = z;
        if (z) ch[z][y == ch[z][1]] = x;
        push_up(y);
        push_up(x);
    }
    void splay (int x, int to)
    {
        int end = fa[to];
        while (fa[x] != end)
        {
            push_down(x);
            if (fa[fa[x]] == end) rotate (x);
            else if (get(x) == get(fa[x])) rotate(fa[x]) , rotate(x);
            else rotate(x) , rotate(x);
        }
        if (to == rt) rt = x;
    }
    void insert (int k)
    {
        if (! rt)
        {
            val[++ tot] = k;
            rt = tot;
            push_up(rt);
            return ;
        }
        int cur = rt , f = 0;
        while (1)
        {
            f = cur;
            cur = ch[cur][val[cur] < k];
            if (! cur)
            {
                val[++ tot] = k;
                fa[tot] = f;
                ch[f][val[f] < k] = tot;
                push_up(tot);
                push_up(f);
                splay(tot, rt);
                break;
            }
        }
    }
    int Kth (int k)
    {
        int cur = rt;
        while (1)
        {
            push_down(cur);
            if (siz[ch[cur][0]] >= k) cur = ch[cur][0];
            else if (siz[ch[cur][0]] + 1 == k) return cur;
            else k -= siz[ch[cur][0]] + 1, cur = ch[cur][1];
        }
    }
    void reserve(int l , int r)
    {
        l = Kth(l); //l - 1
        r = Kth (r + 2); // r + 1
        splay(l,rt);
        splay(r,ch[l][1]);
        tag[ch[ch[rt][1]][0]] ^= 1;
    }
    void write(int x)
    {
        push_down(x);
        if(ch[x][0]) write(ch[x][0]);
        if(val[x] > 1 && val[x] < n + 2) printf("%d ",val[x]-1);
        if(ch[x][1])write(ch[x][1]);
    }
}tree;
int main()
{
    read (n , m);
    for (int i = 1 ; i <= n+2 ; i ++)
        tree.insert(i);
    while (m --)
    {
        int l , r;
        read (l , r);
        tree.reserve(l , r);
    }
    tree.write(tree.rt), LF;
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值