写题半小时,调试一万年系列
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;
}