bzoj3224(splay)

Description
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input
第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

Output
对于操作3,4,5,6每行输出一个数,表示对应答案
Sample Input
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
106465
84185
492737

HINT
1.n的数据范围:n<=100000

2.每个数的数据范围:[-2e9,2e9]


区间翻转模板

#include<bits/stdc++.h>
#define rep(i,j,k) for(int i = j;i <= k;++i)
#define repp(i,j,k) for(int i = j;i >= k;--i)
#define rept(i,x) for(int i = linkk[x];i;i = e[i].n)
#define ll long long 
using namespace std;
const int INF = 0x7fffffff;
int root , tot;
struct Splay{
    int ch[2],val,size,cnt,fa;
    bool rev;
}tr[101000];

namespace fastIO{ 
    #define BUF_SIZE 100000 
    #define OUT_SIZE 100000 
    bool IOerror=0; 
    inline char nc(){ 
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; 
        if (p1==pend){ 
            p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); 
            if (pend==p1){IOerror=1;return -1;} 
        } 
        return *p1++; 
    } 
    inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';} 
    inline void read(int &x){ 
        bool sign=0; char ch=nc(); x=0; 
        for (;blank(ch);ch=nc()); 
        if (IOerror)return; 
        if (ch=='-')sign=1,ch=nc(); 
        for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; 
        if (sign)x=-x; 
    } 
    inline void read(ll &x){ 
        bool sign=0; char ch=nc(); x=0; 
        for (;blank(ch);ch=nc()); 
        if (IOerror)return; 
        if (ch=='-')sign=1,ch=nc(); 
        for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0'; 
        if (sign)x=-x; 
    } 
    #undef OUT_SIZE 
    #undef BUF_SIZE 
}; 
using namespace fastIO;

void update(int x){tr[x].size = tr[tr[x].ch[1]].size + tr[tr[x].ch[0]].size + tr[x].cnt;}
void zig(int x)
{
    int y = tr[x].fa;int z = tr[y].fa;
    int k = tr[y].ch[1] == x;
    tr[z].ch[tr[z].ch[1] == y] = x; tr[x].fa = z;
    tr[y].ch[k] = tr[x].ch[k^1]; tr[tr[x].ch[k^1]].fa = y;
    tr[x].ch[k^1] = y; tr[y].fa = x;
    update(y);update(x);
}
void splay(int x,int R)
{
    while(tr[x].fa != R)
    {
        int y = tr[x].fa;
        int z = tr[y].fa;
        if(z != R)
        (tr[z].ch[1] == y)^(tr[y].ch[1] == x)?zig(x):zig(y);
        zig(x);
    }
    if(R == 0) root = x;
}
void pushdown(int k)
{
    if(tr[k].rev)
    {
        swap(tr[k].ch[0] , tr[k].ch[1]);
        tr[tr[k].ch[0]].rev ^= 1;
        tr[tr[k].ch[1]].rev ^= 1;
        tr[k].rev = 0;
    }
}
int K_th(int rank)
{
    int x = root;
    while(1)
    {
        pushdown(x);
        int ls = tr[x].ch[0];
        if(rank > tr[ls].size + tr[x].cnt) {rank -= tr[ls].size + tr[x].cnt;x = tr[x].ch[1];}
        else if(rank <= tr[ls].size) x = ls;
        else return x;
    }
}
void build(int l,int r,int f)
{
    if(l > r) return;;
    if(l == r)
    {
        tr[l].fa = f;tr[l].size = tr[l].cnt = 1;
        tr[f].ch[l>f] = l;
        return;
    }
    int mid = (l + r)>>1;
    build(l,mid-1,mid);build(mid+1,r,mid);
    tr[mid].fa = f;tr[mid].cnt = 1;update(mid);
    tr[f].ch[mid>f] = mid;
}
void rever(int l,int r)
{
    int x = K_th(l);int y = K_th(r+2);
    splay(x,0);splay(y,x);
    int z = tr[y].ch[0];
    tr[z].rev ^= 1;
}
int main()
{
    int n , m;
    read(n);read(m);
    build(1,n+2,0);root = (n+3)>>1;
    rep(i,1,m)
    {
        int l , r;
        read(l);read(r);
        rever(l,r);
    }
    return 0;
}   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值