打包数据结构
TMXtmx20121001 · · 算法·理论
线段树
template<class idle>
struct segmentTree{
idle tree[N<<4];
int tag[N<<4];
inline int ls(int p){return p<<1;}
inline int rs(int p){return p<<1|1;}
inline void push_up(int p){
tree[p]=tree[ls(p)]+tree[rs(p)];
}
inline void addtag(int p,int pl,int pr,idle d){
tag[p]+=d;
tree[p]+=d*(pr-pl+1);
}
inline void push_down(int p,int pl,int pr){
if (tag[p]){
int mid=(pl+pr)>>1;
addtag(ls(p),pl,mid,tag[p]);
addtag(rs(p),mid+1,pr,tag[p]);
tag[p]=0;
}
}
inline void build(int p,int pl,int pr){
tag[p]=0;
if (pl==pr){
tree[p]=a[pl];
return ;
}
int mid=(pl+pr)>>1;
build(ls(p),pl,mid);
build(rs(p),mid+1,pr);
push_up(p);
}
inline void update(int p,int pl,int pr,int L,int R,idle d){
if (L<=pl && pr<=R){
addtag(p,pl,pr,d);
return ;
}
push_down(p,pl,pr);
int mid=(pl+pr)>>1;
if (L<=mid)
update(ls(p),pl,mid,L,R,d);
if (R>mid)
update(rs(p),mid+1,pr,L,R,d);
push_up(p);
}
inline idle query(int p,int pl,int pr,int L,int R){
if (L<=pl && pr<=R)
return tree[p];
push_down(p,pl,pr);
idle ans=0;
int mid=(pl+pr)>>1;
if (L<=mid)
ans+=query(ls(p),pl,mid,L,R);
if (R>mid)
ans+=query(rs(p),mid+1,pr,L,R);
return ans;
}
};
树状数组
template<class florr>
struct bit{
florr tree[N];
inline void add(int x,florr d){
for (int i=x;i<=n;i+=lowbit(i))
tree[i]+=d;
}
inline florr query(int x){
florr s=0;
for (int i=x;i>=1;i-=lowbit(i))
s+=tree[i];
return s;
}
};
附录
#define lowbit(x) (x&(-x))
并查集
template<class zorr>
struct dsu{
zorr fa[N];
inline void init(){
for (int i=1;i<=n;i++)
fa[i]=i;
}
inline int findset(int x){
while (x!=fa[x])
x=fa[x]=fa[fa[x]];
return fa[x];
}
inline void merge(int x,int y){
int xx=findset(x),yy=findset(y);
if (xx==yy)
return ;
fa[xx]=yy;
}
};
字典树
template<class box>
struct trie{
box nxt[N][26],w[N],cnt=0;
inline void insert(string s){
int now=0;
for (int i=0;i<s.size();i++){
int x=s[i]-'a';
if (!nxt[now][x])
nxt[now][x]=++cnt;
now=nxt[now][x];
}
w[now]++;
}
inline box search(string s){
int now=0;
for (int i=0;i<s.size();i++){
int x=s[i]-'a';
if (!nxt[now][x])
return 0;
now=nxt[now][x];
}
return w[now]>0;
}
};
treap
template<class hornex>
struct FHQ{
struct leaf{
hornex x,w,s;
int l,r;
}tree[N];
int root=0,cnt=0;
inline void push_up(int p){
tree[p].s=tree[tree[p].l].s+tree[tree[p].r].s+1;
}
inline int init(hornex d){
cnt++;
tree[cnt].l=tree[cnt].r=0;
tree[cnt].x=d;
tree[cnt].w=rand_int(1,114514);
tree[cnt].s=1;
return cnt;
}
inline void split(int p,hornex x,int &L,int &R){
if (p==0){
L=R=0;
return ;
}
if (tree[p].x<=x){
L=p;
split(tree[p].r,x,tree[p].r,R);
push_up(L);
}
else{
R=p;
split(tree[p].l,x,L,tree[p].l);
push_up(R);
}
}
inline hornex merge(int L,int R){
if (L==0 || R==0)
return L+R;
if (tree[L].w<tree[R].w){
tree[L].r=merge(tree[L].r,R);
push_up(L);
return L;
}
else{
tree[R].l=merge(L,tree[R].l);
push_up(R);
return R;
}
}
inline void create(int p){
int L,R;
split(root,p,L,R);
hornex now=init(p);
root=merge(merge(L,now),R);
}
inline void clean(int p){
int L,R,mid;
split(root,p,L,R);
split(L,p-1,L,mid);
mid=merge(tree[mid].l,tree[mid].r);
root=merge(merge(L,mid),R);
}
inline hornex getrank(int p){
int L,R;
split(root,p-1,L,R);
hornex now=tree[L].s+1;
root=merge(L,R);
return now;
}
inline hornex getth(int p,hornex d){
while (true){
hornex now=tree[tree[p].l].s;
if (now>=d)
p=tree[p].l;
else if (now+1==d)
return tree[p].x;
else{
d-=now+1;
p=tree[p].r;
}
}
}
inline hornex getma(int p){
int L,R;
split(root,p-1,L,R);
hornex now=getth(L,tree[L].s);
root=merge(L,R);
return now;
}
inline hornex getmi(int p){
int L,R;
split(root,p,L,R);
hornex now=getth(R,1);
root=merge(L,R);
return now;
}
};
随机化
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rand_int(int l,int r){
uniform_int_distribution<int> abt(l, r);
return abt(rng);
}
pbds平衡树(splay)
template<class super>
using bst=tree<super,null_type,less<super>,rb_tree_tag,tree_order_statistics_node_update>;
附录
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
文艺平衡树(treap)
template<class world>
struct LBT{
struct leaf{
world l,r,x,w,s;
bool b;
}tree[N];
int root=0,cnt=0;
inline void addtag(int p){
if (!p)
return ;
tree[p].b^=1;
swap(tree[p].l,tree[p].r);
}
inline void push_down(int p){
if (tree[p].b){
addtag(tree[p].l);
addtag(tree[p].r);
tree[p].b=false;
}
}
inline void push_up(int p){
tree[p].s=1;
if (tree[p].l)
tree[p].s+=tree[tree[p].l].s;
if (tree[p].r)
tree[p].s+=tree[tree[p].r].s;
}
inline int init(int d){
cnt++;
tree[cnt].l=tree[cnt].r=0;
tree[cnt].x=d;
tree[cnt].w=rand_int(1,100000);
tree[cnt].s=1;
tree[cnt].b=false;
return cnt;
}
inline void split(int p,int x,int &L,int &R){
if (p==0){
L=R=0;
return ;
}
push_down(p);
if (tree[tree[p].l].s<x){
L=p;
split(tree[p].r,x-tree[tree[p].l].s-1,tree[p].r,R);
}
else{
R=p;
split(tree[p].l,x,L,tree[p].l);
}
push_up(p);
}
inline int merge(int L,int R){
if (L==0 || R==0)
return L+R;
if (tree[L].w<tree[R].w){
push_down(L);
tree[L].r=merge(tree[L].r,R);
push_up(L);
return L;
}
else{
push_down(R);
tree[R].l=merge(L,tree[R].l);
push_up(R);
return R;
}
}
inline void query(int p){
if (!p)
return ;
push_down(p);
query(tree[p].l);
cout << tree[p].x << ' ';
query(tree[p].r);
}
};
笛卡尔树
template<class thoita>
struct ct{
thoita l[N],r[N],ans[N],cnt;;
inline void dfs(thoita x){
ans[x]=++cnt;
if (l[x])
dfs(l[x]);
if (r[x])
dfs(r[x]);
}
inline void build(){
stack<thoita> c;
thoita root=0;
for (thoita i=1;i<=n;i++){
thoita last=0;
while (!c.empty() && a[c.top()]>a[i]){
last=c.top();
c.pop();
}
if (!c.empty())
r[c.top()]=i;
else
root=i;
l[i]=last;
c.push(i);
}
dfs(root);
}
};