跳到正文

科技·工程

打包数据结构

TMXtmx20121001约 9 分钟
原文

这是 2026年9月6日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年9月5日
收录
2026年9月6日
更新
2026年11月20日
篇幅
约 9 分钟
获赞
1

版本

  1. v2当前
  2. v1

树状数组

C++
template<class florr>
struct bit{
	florr tree[N];
	inline void add(florr x,florr d){
		for (florr i=x;i<=n;i+=lowbit(i))
			tree[i]+=d;
	}
	inline florr query(florr x){
		florr s=0;
		for (florr i=x;i>=1;i-=lowbit(i))
			s+=tree[i];
		return s;
	}
};

注:附带

C++
#define lowbit(x) (x&(-x))

并查集

C++
template<class digdig>
struct dsu{
	digdig fa[N];
	inline void init(){
		for (digdig i=1;i<=n;i++)
			fa[i]=i;
	}
	inline digdig findset(digdig x){
		while (x!=fa[x])
			x=fa[x]=fa[fa[x]];
		return fa[x];
	}
	inline void merge(digdig x,digdig y){
		digdig xx=findset(x),yy=findset(y);
		if (xx==yy)
			return ;
		fa[xx]=yy;
	}
};

线段树

C++
template<class zorr>
struct segmentTree{
	zorr tree[N<<4],tag[N<<4];
	inline zorr ls(zorr x){return x<<1;}
	inline zorr rs(zorr x){return x<<1|1;}
	inline void push_up(zorr x){
		tree[x]=tree[ls(x)]+tree[rs(x)];
	}
	inline void addtag(zorr p,zorr pl,zorr pr,zorr d){
		tag[p]+=d;
		tree[p]+=d*(pr-pl+1);
	}
	inline void push_down(zorr p,zorr pl,zorr pr){
		if (tag[p]){
			zorr 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(zorr p,zorr pl,zorr pr){
		tag[p]=0;
		if (pl==pr){
			tree[p]=a[pl];
			return ;
		}
		zorr mid=(pl+pr)>>1;
		build(ls(p),pl,mid);
		build(rs(p),mid+1,pr);
		push_up(p);
	}
	inline void update(zorr p,zorr pl,zorr pr,zorr L,zorr R,zorr d){
		if (L<=pl && pr<=R){
			addtag(p,pl,pr,d);
			return ;
		}
		push_down(p,pl,pr);
		zorr 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 zorr query(zorr p,zorr pl,zorr pr,zorr L,zorr R){
		if (L<=pl && pr<=R)
			return tree[p];
		push_down(p,pl,pr);
		zorr ans=0,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;
	}
};

平衡树(无旋treap)

C++
template<class hornex>
struct FHQ{
	struct leaf{
		hornex l,r,x,w,s;
	}tree[N];
	hornex root=0,cnt=0;
	inline void push_up(hornex p){
		tree[p].s=tree[tree[p].l].s+tree[tree[p].r].s+1;
	}
	inline hornex 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(hornex p,hornex x,hornex &L,hornex &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(hornex L,hornex 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(hornex p){
		hornex L,R;
		split(root,p,L,R);
		hornex now=init(p);
		root=merge(merge(L,now),R);
	}
	inline void clean(hornex p){
		hornex 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(hornex p){
		hornex L,R;
		split(root,p-1,L,R);
		hornex now=tree[L].s+1;
		root=merge(L,R);
		return now;
	}
	inline hornex getth(hornex 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(hornex p){
		hornex L,R;
		split(root,p-1,L,R);
		hornex now=getth(L,tree[L].s);
		root=merge(L,R);
		return now;
	}
	inline hornex getmi(hornex p){
		hornex L,R;
		split(root,p,L,R);
		hornex now=getth(R,1);
		root=merge(L,R);
		return now;
	}
};

笛卡尔树

C++
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);
	}
};

随机promax

C++
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);
}