[SHOI2009]Booking 会场预约 【Treap】

[SHOI2009]Booking 会场预约

写题半小时,调试一万年系列

Treap里面包含各种神奇的成分,然后在找重合区间的时候顺便删掉,要不会TLE

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
using namespace std;

struct node
{
    node* ch[2]; int low, high, s, r, maxn;
    node(int low, int high):low(low), high(high) {maxn=high, r=rand(), s=1, ch[0]=ch[1]=NULL;}
    void maintain(){ 
        maxn=max(high, max((ch[0] == NULL ? -0x7f7f7f : ch[0]->maxn), (ch[1] == NULL ? -0x7f7f7f : ch[1]->maxn)));
        s=1+(ch[0] == NULL ? 0 : ch[0]->s)+(ch[1] == NULL ? 0 : ch[1]->s);
    }
    int cmp(int l) { if(l == low) return -1; return l > high;}
};
node* rt;

void rotate(node* &o, int d)
{
    node* k=o->ch[d^1]; o->ch[d^1]=k->ch[d]; k->ch[d]=o;
    o->maintain(); k->maintain(); o=k;
}

void ins(node* &o, int l, int r)
{
    if(o == NULL) {o=new node(l, r); return ;}
    int d=(o->high < l); ins(o->ch[d], l, r);
    if(o->ch[d]->r > o->r) rotate(o, d^1);
    o->maintain();
}

void del(node* &o)
{
    if(o->ch[0] != NULL && o->ch[1] != NULL)
    {
        int d2=(o->ch[0]->r > o->ch[1]->r);
        rotate( o, d2); del( o->ch[d2]);
    }
    else { if(o->ch[0] == NULL) o=o->ch[1]; else o=o->ch[0];}
}

int find(node* &o, int l, int r)
{
    if(o == NULL || o->maxn < l) return 0;
    int flag=0, cnt=0; 
    if(l <= o->high && r >= o->low) cnt=1, flag=1;
    if(r >= o->low) cnt+=find(o->ch[1], l, r); cnt+=find(o->ch[0], l, r);
    if(flag) del(o);
    return cnt;
}

int read(){
    int out=0, f=1; char c=getchar(); while(c < '0' || c > '9') {if(c == '-') f=-1; c=getchar();}
    while(c >= '0' && c <= '9') {out=(out<<1)+(out<<3)+c-'0'; c=getchar();}
    return out*f;
}

void solve()
{
    int n=read();
    for(int i=1; i <= n; i++)
    {
        char cmd=getchar();
        while(cmd != 'A' && cmd != 'B') cmd=getchar();
        if(cmd == 'B') printf("%d\n", rt == NULL ? 0 : rt->s);
        else {int l=read(), r=read(), ans=find(rt, l, r); ins(rt, l, r); printf("%d\n", ans);}
    }
}

int main()
{
    solve();
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值