跳到正文

个人记录

模板

yyandy约 10 分钟
原文
文章信息与版本

文章信息

发布
2021年3月19日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 10 分钟
获赞
3

版本

  1. v1当前

O(n3)O(n^3) 矩阵乘法

C++
#include<bits/stdc++.h>
using namespace std;
int a[120][120],b[120][120],c[120][120],n,m,p,i,j,k;
void jzc(){
	for(i=1;i<=n;++i)
	for(j=1;j<=m;++j)
	for(k=1;k<=p;++k)
	c[i][k]+=a[i][j]*b[j][k];
}

线段树

C++
void push_down(int p,int l,int r){
    int mid=(l+r)>>1;
    tag[p<<1]+=tag[p];
    Max[p<<1]+=tag[p]*(mid-l+1)%P;
    tag[p<<1|1]+=tag[p];
    Max[p<<1|1]+=tag[p]*(r-mid)%P;
    tag[p]=0;
}
ll que(int x,int y,int l,int r,int s){
    if(x>=l&&y<=r)return Max[s];
    else{
        int mid=(x+y)>>1;push_down(s,x,y);
        if(mid<l)return que(mid+1,y,l,r,(s<<1|1));
        else if(mid>=r)return que(x,mid,l,r,(s<<1));
        else return (que(mid+1,y,l,r,((s<<1)|1))+que(x,mid,l,r,(s<<1)))%P;
    }
}
void cha(int x,int y,int s,ll sk,int l,int r){
    if(x>=l&&y<=r){Max[s]+=sk*(y-x+1)%P;tag[s]+=sk;return;}
    else{
        int mid=(x+y)>>1;push_down(s,x,y);
        if(mid<l)cha(mid+1,y,(s<<1|1),sk,l,r),Max[s]=Max[s<<1]+Max[s<<1|1];
        else if(mid>=r)cha(x,mid,(s<<1),sk,l,r),Max[s]=Max[s<<1]+Max[s<<1|1];
        else cha(mid+1,y,(s<<1|1),sk,l,r),cha(x,mid,(s<<1),sk,l,r),Max[s]=Max[s<<1]+Max[s<<1|1];
        Max[s]%=P;
    }
}

树链剖分

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll tag[900000],Max[900000],P;
int dp[102000],f[102000],sz[102000],pt[202000],nt[202000],hd[102000],i,n,m,r,hs[102000],df[102000],K,k,a[102000],top[102000],x,y,z,op;
void push_down(int p,int l,int r){
    int mid=(l+r)>>1;
    tag[p<<1]+=tag[p];
    Max[p<<1]+=tag[p]*(mid-l+1)%P;
    tag[p<<1|1]+=tag[p];
    Max[p<<1|1]+=tag[p]*(r-mid)%P;
    tag[p]=0;
}
ll que(int x,int y,int l,int r,int s){
    if(x>=l&&y<=r)return Max[s];
    else{
        int mid=(x+y)>>1;push_down(s,x,y);
        if(mid<l)return que(mid+1,y,l,r,(s<<1|1));
        else if(mid>=r)return que(x,mid,l,r,(s<<1));
        else return (que(mid+1,y,l,r,((s<<1)|1))+que(x,mid,l,r,(s<<1)))%P;
    }
}
void cha(int x,int y,int s,ll sk,int l,int r){
    if(x>=l&&y<=r){Max[s]+=sk*(y-x+1)%P;tag[s]+=sk;return;}
    else{
        int mid=(x+y)>>1;push_down(s,x,y);
        if(mid<l)cha(mid+1,y,(s<<1|1),sk,l,r),Max[s]=Max[s<<1]+Max[s<<1|1];
        else if(mid>=r)cha(x,mid,(s<<1),sk,l,r),Max[s]=Max[s<<1]+Max[s<<1|1];
        else cha(mid+1,y,(s<<1|1),sk,l,r),cha(x,mid,(s<<1),sk,l,r),Max[s]=Max[s<<1]+Max[s<<1|1];
        Max[s]%=P;
    }
}
void dfs1(int x,int fa,int dep){
	dp[x]=dep;
	f[x]=fa;
	sz[x]=1;
	for(int i=hd[x];i;i=nt[i])
	if(pt[i]!=fa){
	dfs1(pt[i],x,dep+1),sz[x]+=sz[pt[i]];
	if(sz[pt[i]]>sz[hs[x]])hs[x]=pt[i];
    }
} 
void dfs2(int x,int now){	
	df[x]=++K;
	cha(1,n,1,a[x],df[x],df[x]);
	top[x]=now;
	if(hs[x])
	dfs2(hs[x],now);
	for(int i=hd[x];i;i=nt[i]){
	if(pt[i]!=hs[x]&&pt[i]!=f[x])
	dfs2(pt[i],pt[i]);
	}
}
void add(int x,int y){
	++k;
	nt[k]=hd[x];
	hd[x]=k;
	pt[k]=y;
}
void chan(int x,int y,int z){
	while (top[x]!=top[y]){
		if(dp[top[x]]<dp[top[y]])swap(x,y);
		cha(1,n,1,z,df[top[x]],df[x]);
		x=f[top[x]];
	}
	if(df[x]<df[y])swap(x,y);
	cha(1,n,1,z,df[y],df[x]);
}
int quer(int x,int y){
	ll ans=0; 
	while (top[x]!=top[y]){
		if(dp[top[x]]<dp[top[y]])swap(x,y);
		ans=(ans+que(1,n,df[top[x]],df[x],1))%P;
		x=f[top[x]];
	}
	if(df[x]<df[y])swap(x,y);
	ans+=que(1,n,df[y],df[x],1);
	return ans%P;
}
int main(){
	cin>>n>>m>>r>>P;	
	for(i=1;i<=n;++i)
	scanf("%d",&a[i]); 
	for(i=1;i<n;++i)
	scanf("%d%d",&x,&y),add(x,y),add(y,x);
	dfs1(r,0,1);
	dfs2(r,r);
	while(m--){
		scanf("%d",&op);
		if(op==1){
			scanf("%d%d%d",&x,&y,&z);
			chan(x,y,z%P);
		} 
		else
		if(op==2){
			scanf("%d%d",&x,&y);
			printf("%d\n",quer(x,y));
		}
		else
		if(op==3){
		   scanf("%d%d",&x,&z);
		   cha(1,n,1,z%P,df[x],df[x]+sz[x]-1);
		}
		else
		if(op==4){
			scanf("%d",&x); 
			printf("%d\n",que(1,n,df[x],df[x]+sz[x]-1,1)%P); 
		}
	}
}

01Trie

C++
const int dt=1e7;
typedef unsigned int ui;
struct Trie {
	bool x;
	int s[2],m;
} T[8488608];
int k=1,Q,op,x;
void add(ui y) {
	ui w=1;
	for(int i=25; i>=0; --i) {
		if(!T[w].s[((y>>i)&1)])T[w].s[((y>>i)&1)]=(++k);
		w=T[w].s[((y>>i)&1)],T[w].m++;
	}
}
void del(ui y) {
	ui w=1;
	for(int i=25; i>=0; --i)w=T[w].s[((y>>i)&1)],T[w].m--;
}
ui rankx(ui y) {
	ui w=1,s=0;
	for(int i=25; i>=0; --i) {
		if(y<=T[T[w].s[0]].m)w=T[w].s[0];
		else y-=T[T[w].s[0]].m,w=T[w].s[1],s+=(1u<<i);
	}
	return s;
}
ui lst(ui y) {
	int w=1,as=0,Dep=0,Lst=0,ans;
	T[0].m=0;
	for(int i=25; i>=0; --i) {
		if(!((y>>i)&1u)&&T[T[w].s[1]].m)Lst=T[w].s[1],Dep=i-1,ans=as+(1u<<i);
		if(!T[T[w].s[((y>>i)&1u)]].m)break;
		as+=(((y>>i)&1u)<<i),w=T[w].s[(y>>i)&1u];
	}
	for(int i=Dep; i>=0; --i)if(T[T[Lst].s[0]].m)Lst=T[Lst].s[0];
		else Lst=T[Lst].s[1],ans+=(1u<<i);
	return ans;
}
ui pre(ui y) {
	int w=1,as=0,Dep=0,Pre,ans;
	T[0].m=0;
	for(int i=25; i>=0; --i) {
		if(((y>>i)&1u)&&T[T[w].s[0]].m)Pre=T[w].s[0],Dep=i-1,ans=as;
		if(!T[T[w].s[((y>>i)&1u)]].m)break;
		as+=(((y>>i)&1u)<<i),w=T[w].s[(y>>i)&1u];
	}
	for(int i=Dep; i>=0; --i)if(T[T[Pre].s[1]].m)Pre=T[Pre].s[1],ans+=(1u<<i);
		else Pre=T[Pre].s[0];
	return ans;
}
ui rk(ui y) {
	ui x=1,w=1;
	for(int i=25; i>=0; --i) x+=(((y>>i)&1)?(T[T[w].s[0]].m):0),w=T[w].s[(y>>i)&1];
	return x;
}
原文
存档评论1 条抓取于