跳到正文

算法·理论

AW_BCH 的模板

AW_BCH约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 5 分钟
获赞
1

版本

  1. v1当前

致敬 Xzy的模板

头文件

可能出现的宏定义:

C++
#define emdl '\n'
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int MAXN=1+5;
int n,m;

线段树

C++
struct node{
    struct tree{
        ll l,r;
        ll sum;
        ll add;
    }tr[4*MAXN];
    void pushup(ll p){
        #define lc p<<1
        #define rc p<<1|1
        tr[p].sum=tr[lc].sum+tr[rc].sum;
        #undef lc
        #undef rc
        return;
    }
    void pushdown(ll p){
        #define lc p<<1
        #define rc p<<1|1
        if(tr[p].add){
            tr[lc].sum+=tr[p].add*(tr[lc].r-tr[lc].l+1);
            tr[rc].sum+=tr[p].add*(tr[rc].r-tr[rc].l+1);
            tr[lc].add+=tr[p].add;
            tr[rc].add+=tr[p].add;
            tr[p].add=0;
        }
        #undef lc
        #undef rc
        return;
    }
    void build(ll p,ll l,ll r){
        #define lc p<<1
        #define rc p<<1|1
        tr[p]={l,r,0,0};
        if(l==r) return;
        ll mid=(l+r)>>1;
        build(lc,l,mid);
        build(rc,mid+1,r);
        pushup(p);
        #undef lc
        #undef rc
        return;
    }
    void update(ll p,ll l,ll r,ll k){
        if(l<=tr[p].l&&tr[p].r<=r){
            tr[p].sum+=k*(tr[p].r-tr[p].l+1);
            tr[p].add+=k;
            return;
        }
        #define lc p<<1
        #define rc p<<1|1
        pushdown(p);
        ll mid=(tr[p].l+tr[p].r)>>1;
        if(l<=mid) update(lc,l,r,k);
        if(r>mid) update(rc,l,r,k);
        pushup(p);
        #undef lc
        #undef rc
        return;
	}
    ll query(ll p,ll l,ll r){
        #define lc p<<1
        #define rc p<<1|1
        if(l<=tr[p].l&&tr[p].r<=r) return tr[p].sum;
        pushdown(p);
        ll mid=(tr[p].l+tr[p].r)>>1;
        ll res=0;
        if(l<=mid) res+=query(lc,l,r);
        if(r>mid) res+=query(rc,l,r);
        #define lc p<<1
        #define rc p<<1|1
        return res;
    }
};

并查集

C++
struct node{
	ll f[MAXN],sz[MAXN];
	void init(){
		for(ll i=1;i<=n;i++){
			f[i]=i;
			sz[i]=1;
		}
		return;
	}
	void update(ll p,ll k){
		f[p]=k;
		return;
	}
	ll find(ll x){
		if(x!=f[x]){
			f[x]=find(f[x]);
		}
		return f[x];
	}
	void merge(ll p,ll k){
		ll q1=p;
		ll q2=k;
		if(sz[q1]<sz[q2]) swap(q1,q2);
		f[q2]=q1;
		sz[q1]+=sz[q2];
	}
};

扩展并查集

C++
struct node{
	ll f[MAXN];
	void init(ll p){
		for(ll i=1;i<=p*n;i++){
			f[i]=i;
		}
		return;
	}
	void update(ll p,ll k){
		f[p]=k;
		return;
	}
	ll find(ll x){
		if(x!=f[x]){
			f[x]=find(f[x]);
		}
		return f[x];
	}
	void merge1(ll p,ll a,ll b){
		f[find(a)]=find(b);
		return;
	}
	void merge2(ll p,ll a,ll b){
		f[find(n+(p-1)*b)]=find(a);
		f[find(n+(p-1)*a)]=find(b);
		return;
	}
};
原文