namespace wyzds
{
template<typename _Tp>
class treap
{
public:
typedef _Tp type;
struct node
{
node *ch[2];
type val;
int siz,cnt;
int w;
node(type v):val(v),siz(1),cnt(1),w(rand())
{ch[0]=ch[1]=nullptr;}
node(node *x):val(x->val),siz(x->siz),cnt(x->cnt),w(x->w)
{ch[0]=ch[1]=nullptr;}
void push_up()
{
siz=cnt;
if(ch[0]!=nullptr) siz+=ch[0]->siz;
if(ch[1]!=nullptr) siz+=ch[1]->siz;
}
};
private:
node *rt;
public:
treap(){srand(time(nullptr)),rt=nullptr;}
private:
pair<node*,node*> split_val(type v,node *cur)
{
if(cur==nullptr) return {nullptr,nullptr};
if(v<cur->val)
{
auto tmp=split_val(v,cur->ch[0]);
cur->ch[0]=tmp.second,cur->push_up();
return {tmp.first,cur};
}
else
{
auto tmp=split_val(v,cur->ch[1]);
cur->ch[1]=tmp.first,cur->push_up();
return {cur,tmp.second};
}
}
pair<node*,node*> split_val_un(type v,node *cur)
{
if(cur==nullptr) return {nullptr,nullptr};
if(v<=cur->val)
{
auto tmp=split_val_un(v,cur->ch[0]);
cur->ch[0]=tmp.second,cur->push_up();
return {tmp.first,cur};
}
else
{
auto tmp=split_val_un(v,cur->ch[1]);
cur->ch[1]=tmp.first,cur->push_up();
return {cur,tmp.second};
}
}
tuple<node*,node*,node*> split_rnk(int k,node *cur)
{
if(cur==nullptr) return {nullptr,nullptr,nullptr};
int lsz=cur->ch[0]==nullptr?0:cur->ch[0]->siz;
if(k<=lsz)
{
node *l,*mid,*r;
tie(l,mid,r)=split_rnk(k,cur->ch[0]);
cur->ch[0]=r,cur->push_up();
return {l,mid,cur};
}
else if(k<=lsz+cur->cnt)
{
node *l=cur->ch[0],*r=cur->ch[1];
cur->ch[0]=cur->ch[1]=nullptr,cur->push_up();
return {l,cur,r};
}
else
{
node *l,*mid,*r;
tie(l,mid,r)=split_rnk(k-lsz-cur->cnt,cur->ch[1]);
cur->ch[1]=l,cur->push_up();
return {cur,mid,r};
}
}
node *merge(node *x,node *y)
{
if(x==nullptr) return y;
if(y==nullptr) return x;
if(x->w<y->w)
{
x->ch[1]=merge(x->ch[1],y),x->push_up();
return x;
}
else
{
y->ch[0]=merge(x,y->ch[0]),y->push_up();
return y;
}
}
public:
void insert(type v)
{
auto tmp=split_val(v,rt);
auto l=split_val_un(v,tmp.first);
node *nd;
if(l.second==nullptr) nd=new node(v);
else l.second->cnt++,l.second->push_up();
auto ll=merge(l.first,l.second==nullptr?nd:l.second);
rt=merge(ll,tmp.second);
}
void del(type v)
{
auto tmp=split_val(v,rt);
auto l=split_val_un(v,tmp.first);
if(l.second->cnt>1)
{
l.second->cnt--,l.second->push_up();
l.first=merge(l.first,l.second);
}
else
{
if(l.second==tmp.first)
tmp.first=nullptr;
delete l.second;
l.second=nullptr;
}
rt=merge(l.first,tmp.second);
}
int query_rnk(type v)
{
auto tmp=split_val_un(v,rt);
int res=(tmp.first==nullptr?0:tmp.first->siz)+1;
rt=merge(tmp.first,tmp.second);
return res;
}
private:
type query_val(int k,node *cur)
{
node *l,*mid,*r;
tie(l,mid,r)=split_rnk(k,cur);
type res=mid->val;
rt=merge(merge(l,mid),r);
return res;
}
public:
type query_val(int k){return query_val(k,rt);}
type query_pre(type v)
{
auto tmp=split_val_un(v,rt);
type res=query_val(tmp.first->siz,tmp.first);
rt=merge(tmp.first,tmp.second);
return res;
}
type query_nxt(type v)
{
auto tmp=split_val(v,rt);
type res=query_val(1,tmp.second);
rt=merge(tmp.first,tmp.second);
return res;
}
}; // class treap
template<typename _Tp>
class segtreap
{
public:
typedef _Tp type;
struct node
{
node *ch[2];
type val;
int siz,cnt;
int w;
bool tag;
node(type v):val(v),siz(1),cnt(1),w(rand()),tag(0)
{ch[0]=ch[1]=nullptr;}
node(node *x):val(x->val),siz(x->siz),cnt(x->cnt),w(x->w),tag(x->tag)
{}
inline void push_up()
{
siz=cnt;
if(ch[0]!=nullptr) siz+=ch[0]->siz;
if(ch[1]!=nullptr) siz+=ch[1]->siz;
}
inline void __upd(){tag^=1;}
inline void push_down()
{
if(!tag) return;
swap(ch[0],ch[1]);
if(ch[0]!=nullptr) ch[0]->__upd();
if(ch[1]!=nullptr) ch[1]->__upd();
tag=0;
}
};
private:
node *rt;
public:
segtreap(){rt=nullptr;}
private:
inline int siz(node *cur)
{return cur==nullptr?0:cur->siz;}
pair<node*,node*> split_siz(int k,node *cur)
{
if(cur==nullptr) return {nullptr,nullptr};
cur->push_down();
if(k<=siz(cur->ch[0]))
{
auto tmp=split_siz(k,cur->ch[0]);
cur->ch[0]=tmp.second,cur->push_up();
return {tmp.first,cur};
}
else
{
auto tmp=split_siz(k-siz(cur->ch[0])-1,cur->ch[1]);
cur->ch[1]=tmp.first,cur->push_up();
return {cur,tmp.second};
}
}
node* merge(node *x,node *y)
{
if(x==nullptr) return y;
if(y==nullptr) return x;
x->push_down(),y->push_down();
if(x->w<y->w)
{
x->ch[1]=merge(x->ch[1],y),x->push_up();
return x;
}
else
{
y->ch[0]=merge(x,y->ch[0]),y->push_up();
return y;
}
}
public:
void insert(int s,type v)
{
auto tmp=split_siz(s,rt);
auto l=split_siz(s-1,tmp.first);
node *nd;
if(l.second==nullptr) nd=new node(v);
auto ll=merge(l.first,l.second==nullptr?nd:l.second);
rt=merge(ll,tmp.second);
}
void reverse(int l,int r)
{
auto tmp=split_siz(l-1,rt);
auto res=split_siz(r-l+1,tmp.second);
res.first->__upd();
rt=merge(tmp.first,merge(res.first,res.second));
}
type query_val(int s,node *cur)
{
auto tmp=split_siz(s,cur);
auto l=split_siz(s-1,tmp.first);
rt=merge(merge(l.first,l.second),tmp.second);
return l.second->val;
}
private:
void print(node *cur,string en)
{
if(cur==nullptr) return;
cur->push_down();
print(cur->ch[0],en);
cout<<cur->val<<en;
print(cur->ch[1],en);
}
public:
void print(string en=" "){print(rt,en);}
}; // class segtreap
template<typename _Tp>
class pstreap // persistent-treap
{
public:
#ifndef N
#define N 500005
#endif
typedef _Tp type;
struct node
{
node *ch[2];
type val;
int siz,cnt;
int w;
node(type v):val(v),siz(1),cnt(1),w(rand())
{ch[0]=ch[1]=nullptr;}
node(node *x):val(x->val),siz(x->siz),cnt(x->cnt),w(x->w)
{ch[0]=x->ch[0],ch[1]=x->ch[1];}
void push_up()
{
siz=cnt;
if(ch[0]!=nullptr) siz+=ch[0]->siz;
if(ch[1]!=nullptr) siz+=ch[1]->siz;
}
};
node *rt[N];
pstreap(){rt[0]=nullptr;}
private:
inline int siz(node *cur)
{return cur==nullptr?0:cur->siz;}
pair<node*,node*> split_val(type v,node *cur)
{
if(cur==nullptr) return {nullptr,nullptr};
if(v<cur->val)
{
node *nd=new node(cur);
auto tmp=split_val(v,nd->ch[0]);
nd->ch[0]=tmp.second,nd->push_up();
return {tmp.first,nd};
}
else
{
node *nd=new node(cur);
auto tmp=split_val(v,nd->ch[1]);
nd->ch[1]=tmp.first,nd->push_up();
return {nd,tmp.second};
}
}
pair<node*,node*> split_val_un(type v,node *cur)
{
if(cur==nullptr) return {nullptr,nullptr};
if(v<=cur->val)
{
node *nd=new node(cur);
auto tmp=split_val_un(v,nd->ch[0]);
nd->ch[0]=tmp.second,nd->push_up();
return {tmp.first,nd};
}
else
{
node *nd=new node(cur);
auto tmp=split_val_un(v,nd->ch[1]);
nd->ch[1]=tmp.first,nd->push_up();
return {nd,tmp.second};
}
}
node* merge(node *x,node *y)
{
if(x==nullptr&&y==nullptr) return nullptr;
if(x!=nullptr&&y==nullptr) return new node(x);
if(x==nullptr&&y!=nullptr) return new node(y);
if(x->w<y->w)
{
node *nd=new node(x);
nd->ch[1]=merge(nd->ch[1],y),nd->push_up();
return nd;
}
else
{
node *nd=new node(y);
nd->ch[0]=merge(x,nd->ch[0]),nd->push_up();
return nd;
}
}
public:
node* insert(type v,node *cur)
{
auto tmp=split_val(v,cur);
auto l=split_val_un(v,tmp.first);
if(l.second==nullptr) l.second=new node(v);
else l.second->cnt++,l.second->push_up();
return merge(merge(l.first,l.second),tmp.second);
}
node* erase(type v,node *cur)
{
auto tmp=split_val(v,cur);
auto l=split_val_un(v,tmp.first);
if(l.second==nullptr) return merge(l.first,tmp.second);
if(l.second->cnt>1)
{
l.second->cnt--,l.second->push_up();
l.first=merge(l.first,l.second);
}
else
{
if(l.second==tmp.first)
tmp.first=nullptr;
delete l.second;
l.second=nullptr;
}
return merge(l.first,tmp.second);
}
int query_rnk(type v,node *cur)
{
auto tmp=split_val_un(v,cur);
int res=siz(tmp.first)+1;
cur=merge(tmp.first,tmp.second);
return res;
}
type query_val(int k,node *cur)
{
while(cur!=nullptr)
{
if(k<=siz(cur->ch[0])) cur=cur->ch[0];
else if(k<=siz(cur->ch[0])+cur->cnt) return cur->val;
else k-=siz(cur->ch[0])+cur->cnt,cur=cur->ch[1];
}
return type(1838);
}
type query_pre(type v,node *cur)
{
auto tmp=split_val_un(v,cur);
type res=query_val(siz(tmp.first),tmp.first);
cur=merge(tmp.first,tmp.second);
return res;
}
type query_nxt(type v,node *cur)
{
auto tmp=split_val(v,cur);
type res=query_val(1,tmp.second);
cur=merge(tmp.first,tmp.second);
return res;
}
auto& print(node *cur)
{
if(cur==nullptr) return cout;
print(cur->ch[0]);
cout<<cur->val<<" ";
print(cur->ch[1]);
return cout;
}
}; // class pstreap
} // namespace wyzds剪贴板
剪贴板 q5gwhy6i
剪贴板信息
剪贴板信息
- 收录
- 更新