跳到正文

个人记录

必须要背的板子,赶紧背啊啊啊

折雾赴海约 15 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年8月9日
篇幅
约 15 分钟

版本

  1. v1当前
  1. 矩阵加速
C++
#include<bits/stdc++.h>
#define int long long 
using namespace std;
struct mar{
    int a[5][5];
}cnt;
const int mod=1e9+7;
mar mul(mar x,mar y){
    mar t;
    memset(t.a,0,sizeof t.a);
    for(int i=1;i<=2;++i){
        for(int j=1;j<=2;++j){
            for(int k=1;k<=2;++k){
                t.a[i][j]=(t.a[i][j]+1LL*x.a[i][k]*y.a[k][j]%mod)%mod;
            }
        }
    }
    return t;
}
mar mpow(mar mart,int b){
    mar res;
    res.a[1][1]=1,res.a[1][2]=1;
    res.a[2][1]=1,res.a[2][2]=0;
    while(b>0){
        if(b&1)res=mul(res,mart);
        mart=mul(mart,mart);
        b>>=1;
    }
    return res;
}
signed main(){
    int n;cin>>n;
    if(n==0){
        cout<<0;
        return 0;
    }
    mar mart;
    mart.a[1][1]=1,mart.a[1][2]=1;
    mart.a[2][1]=1,mart.a[2][2]=0;
    mar res=mpow(mart,n-2);
    cout<<res.a[1][1]%mod;
}
  1. 线段树,要背两个:
C++
#include<bits/stdc++.h>
using namespace std;
int w[500005*4],a[500005],lzj[500005*4],lzc[500005*4],p;
int qpow(int a,int b){
    if(b==1)return a%p;
    if(b<=0)return 1;
    int cnt=qpow(a,b/2);
    if(b%2==1)return 1LL*cnt*cnt%p*a%p;
    return 1LL*cnt*cnt%p;
}
void pushup(int u){
    w[u]=(w[u*2]+w[u*2+1])%p;
}
void build(int u,int l,int r){
    lzc[u]=1;
    lzj[u]=0;
    if(l==r){
        w[u]=a[l]%p;
        return;
    }
    int m=(l+r)/2;
    build(u*2,l,m);
    build(u*2+1,m+1,r);
    pushup(u);
}
bool in(int l,int r,int L,int R){
    return (L<=l)&&(r<=R);
}
bool of(int l,int r,int L,int R){
    return (r<L)||(R<l);
}
void pushdown(int u,int l,int r){
    int m=(l+r)/2;
    w[u*2]=(1LL*w[u*2]*lzc[u]%p+1LL*lzj[u]*(m-l+1)%p)%p;
    lzc[u*2]=1LL*lzc[u*2]*lzc[u]%p;
    lzj[u*2]=(1LL*lzj[u*2]*lzc[u]%p+lzj[u])%p;
    w[u*2+1]=(1LL*w[u*2+1]*lzc[u]%p+1LL*lzj[u]*(r-m)%p)%p;
    lzc[u*2+1]=1LL*lzc[u*2+1]*lzc[u]%p;
    lzj[u*2+1]=(1LL*lzj[u*2+1]*lzc[u]%p+lzj[u])%p;
    lzc[u]=1;
    lzj[u]=0;
}
void uqd1(int u,int l,int r,int x,int p){
    if(l==r){
        w[u]=x%p;
        return;
    }
    pushdown(u,l,r);
    int m=(l+r)/2;
    if(p<=m)uqd1(u*2,l,m,x,p);
    else uqd1(u*2+1,m+1,r,x,p);
    pushup(u);
}
int q1(int u,int l,int r,int p){
    if(l==r)return w[u];
    pushdown(u,l,r);
    int m=(l+r)/2;
    if(p<=m)return q1(u*2,l,m,p);
    else return q1(u*2+1,m+1,r,p);
}
void uqd2(int u,int l,int r,int L,int R,int x){
    if(in(l,r,L,R)){
        w[u]=(w[u]+1LL*(r-l+1)*x%p)%p;
        lzj[u]=(lzj[u]+x)%p;
        return;
    }
    if(of(l,r,L,R))return;
    pushdown(u,l,r);
    int m=(l+r)/2;
    uqd2(u*2,l,m,L,R,x);
    uqd2(u*2+1,m+1,r,L,R,x);
    pushup(u);
}
void uqd3(int u,int l,int r,int L,int R,int x){
    if(in(l,r,L,R)){
        w[u]=1LL*w[u]*x%p;
        lzc[u]=1LL*lzc[u]*x%p;
        lzj[u]=1LL*lzj[u]*x%p;
        return;
    }
    if(of(l,r,L,R))return;
    pushdown(u,l,r);
    int m=(l+r)/2;
    uqd3(u*2,l,m,L,R,x);
    uqd3(u*2+1,m+1,r,L,R,x);
    pushup(u);
}
int q2(int u,int l,int r,int L,int R){
    if(in(l,r,L,R))return w[u];
    if(of(l,r,L,R))return 0;
    pushdown(u,l,r);
    int m=(l+r)/2;
    return (q2(u*2,l,m,L,R)+q2(u*2+1,m+1,r,L,R))%p;
}
int main(){
    int n,q,m;
    cin>>n>>q>>m;
    p=m;
    for(int i=1;i<=n;++i){
        cin>>a[i];
    }
    build(1,1,n);
    while(q--){
        int op,x,y,k;
        cin>>op;
        if(op==1){
            cin>>x>>y>>k;
            uqd3(1,1,n,x,y,k);
        }else if(op==2){
            cin>>x>>y>>k;
            uqd2(1,1,n,x,y,k);
        }else if(op==3){
            cin>>x>>y;
            cout<<q2(1,1,n,x,y)<<endl;
        }
    }
}
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF=1e18;
int a[1000006],w[4000005],lz[4000005],mx[4000005],lz2[4000005];

void pushup(int u){
    w[u]=w[u*2]+w[u*2+1];
    mx[u]=max(mx[u*2],mx[u*2+1]);
}

bool in(int l,int r,int L,int R){
    return (L<=l)&&(r<=R);
}

bool out(int l,int r,int L,int R){
    return (r<L)||(R<l);
}

void maketag(int u,int len,int x){
    w[u]=len*x;
    mx[u]=x;
    lz[u]=x;
    lz2[u]=0;
}

void maketag2(int u,int len,int x){
    w[u]+=len*x;
    mx[u]+=x;
    if(lz[u]!=-INF) lz[u]+=x;
    else lz2[u]+=x;
}

void pushdown(int u,int l,int r){
    int m=(l+r)/2;
    if(lz[u]!=-INF){
        maketag(u*2,m-l+1,lz[u]);
        maketag(u*2+1,r-m,lz[u]);
        lz[u]=-INF;
    }
    if(lz2[u]){ 
        maketag2(u*2,m-l+1,lz2[u]);
        maketag2(u*2+1,r-m,lz2[u]);
        lz2[u]=0; 
    }
}
void build(int u,int l,int r){
    lz[u]=-INF;
    lz2[u]=0;
    if(l==r){
        w[u]=a[l];
        mx[u]=a[l];
        return;
    }
    int m=(l+r)/2;
    build(u*2,l,m);
    build(u*2+1,m+1,r);
    pushup(u);
}

void upd1(int u,int l,int r,int L,int R,int x){
    if(out(l,r,L,R)) return;
    if(in(l,r,L,R)){
        maketag(u,r-l+1,x);
        return;
    }
    pushdown(u,l,r);
    int m=(l+r)/2;
    upd1(u*2,l,m,L,R,x);
    upd1(u*2+1,m+1,r,L,R,x);
    pushup(u);
}

void upd2(int u,int l,int r,int L,int R,int x){
    if(out(l,r,L,R)) return;
    if(in(l,r,L,R)){
        maketag2(u,r-l+1,x);
        return;
    }
    pushdown(u,l,r);
    int m=(l+r)/2;
    upd2(u*2,l,m,L,R,x);
    upd2(u*2+1,m+1,r,L,R,x);
    pushup(u);
}

int query(int u,int l,int r,int L,int R){
    if(out(l,r,L,R)) return -INF;
    if(in(l,r,L,R)) return mx[u];
    pushdown(u,l,r);
    int m=(l+r)/2;
    return max(query(u*2,l,m,L,R), query(u*2+1,m+1,r,L,R));
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int n,q;
    cin>>n>>q;
    for(int i=1;i<=n;++i){
        cin>>a[i];
    }
    
    build(1,1,n);
    
    while(q--){
        int op,l,r;
        cin>>op>>l>>r;
        if(op==1){
            int x;
            cin>>x;
            upd1(1,1,n,l,r,x);
        }
        else if(op==2){
            int x;
            cin>>x;
            upd2(1,1,n,l,r,x);
        }
        else if(op==3){
            cout<<query(1,1,n,l,r)<<'\n';
        }
    }
    
    return 0;
}

LCT、CRT、NTT、Astar、Treap、Tarjan

主席树。

C++
#include<bits/stdc++.h>
#define int long long
#define lc(x) tr[x].son[0]
#define rc(x) tr[x].son[1]
#define sc(x) tr[x].s
#define rt(x) root[x]
#define getid(x) lower_bound(v.begin(),v.end(),x)-v.begin()+1
using namespace std;
inline int read(){
	int x=0,f=1;
	char ch=getchar_unlocked();
	while (!isdigit(ch)){
		if (ch=='-') 
			f=-1;
		ch=getchar_unlocked();
	}
	while (isdigit(ch)){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar_unlocked();
	}
	return x*f;
}
inline void write(int x){
	if (x<0)putchar('-'),x=-x;
	if (x>9)write(x/10);
	putchar(x%10+'0');
}
vector<int>v;
const int N=1e6;
int n,m,a[N];
struct node{
    int son[2];
    int s;
}tr[22*N];
int root[N],tot;
void build(int &x,int l,int r){
    x=++tot;
    if (l==r)return;
    int m=(l+r)/2;
    build(lc(x),l,m);build(rc(x),m+1,r);
}
void insert(int x,int &y,int l,int r,int v){
    y=++tot,tr[y]=tr[x],tr[y].s++;
    if (l==r)return;
    int m=(l+r)/2;
    if (v<=m)insert(lc(x),lc(y),l,m,v);
    else insert(rc(x),rc(y),m+1,r,v);
}
int query(int x,int y,int l,int r,int k){
    if (l==r)return l;
    int m=(l+r)/2,s=sc(lc(y))-sc(lc(x));
    if (k<=s)return query(lc(x),lc(y),l,m,k);
    else return query(rc(x),rc(y),m+1,r,k-s);
}
signed main(){
    n=read(),m=read();
    for(int i=1;i<=n;++i)a[i]=read(),v.push_back(a[i]);
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    build(root[0],1,v.size());
    for (int i=1;i<=n;++i){
        a[i]=getid(a[i]);
        insert(rt(i-1),rt(i),1,v.size(),a[i]);
    }
    while (m--){
        int l,r,k;cin>>l>>r>>k;
        write(v[query(rt(l-1),rt(r),1,v.size(),k)-1]);
        putchar('\n');
    }
}
原文