跳到正文

个人记录

模板

_dmy_约 22 分钟
原文

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

文章信息与版本

文章信息

发布
2025年6月2日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 22 分钟
获赞
1

版本

  1. v4当前
  2. v3
  3. v2
  4. v1

模板

缺省源

C++
#include<bits/stdc++.h>
// #define int long long
#define endl '\n'
using namespace std;
char buf[1<<20], *pp1, *pp2;
char gc() {if (pp1 == pp2) {pp1 = buf;pp2 = buf + fread(buf, 1, 1<<20, stdin); if (pp1 == pp2) return EOF;}return *pp1++;}
inline int read(){int x=0,f=1;char ch=gc();while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=gc();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch-48);ch=gc();}return f*x;}
struct FastIO {
    template<class T>
    FastIO& operator>>(T& ans) {T x=0,f=1;char ch;ch=gc();while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=gc();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch-48);ch=gc();}ans=f*x;return *this;}
	FastIO& operator>>(string& s) {s.clear(); char ch = gc();while (isspace(ch)) ch = gc();while (!isspace(ch) && ch != EOF) { s += ch; ch = gc(); }return *this;}
}fin;
signed main() {
#if !defined(ONLINE_JUDGE)
	freopen("1.in", "r", stdin);
	//freopen("1.out", "w", stdout);
#endif    
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
}

高精度(不含负数)

C++
const long long base=1000000LL;
const int width=6;
struct bigint{
    int f=1;
	vector<long long>num;
    bigint():f(1),num(1,0){}
    bigint(int n):num(n,0){}
    bigint(int _f,vector<long long>_v):f(_f),num(_v){}
    int size()const{
        return (int)num.size();
    }
    void rlz(){
        while(num.size()>1&&num.back()==0)num.pop_back();
        if(num.size()==1&&num[0]==0)f=1;
    }
    long long& operator[](int x){return num[x];}
    long long operator[](int x)const{
        return num[x];
    }
    bool operator==(const bigint&b)const{
        return f==b.f&&num==b.num;
    }
    bool operator<(const bigint&b)const{
        if((int)num.size()!=b.size())return (int)num.size()<b.size();
        for (int i=size()-1;i>=0;i--)
            if(num[i]!=b[i])return num[i]<b[i];
        return false;
    }
    bool operator<=(const bigint&b)const{
        return ((*this)<b)||((*this)==b);
    }
	bigint operator+(const bigint&b)const{
        if(b.size()>size())return b+(*this);
		vector<long long>c;
		long long t=0;
		for(int i=0;i<size();i++){
			t+=num[i]+(i<b.size()?b.num[i]:0);
			c.push_back(t%base);
			t/=base;
		}
		if(t)c.push_back(t);
		return bigint(1,c);
	}
	bigint operator-(const bigint&b)const{
        bigint a=(*this);
        bigint ans;
        if(a<b){
            bigint t=b-a;
            t.f=-1;
            return t;
        }
		vector<long long>c=num;
		for(int i=0;i<b.size();i++){
			c[i]-=b.num[i];
			if(c[i]<0){
				c[i]+=base;
				c[i+1]--;
			}
		}
		bigint res(1,c);
		res.rlz();
		return res;
	}
    bigint operator/(long long x)const{
        if(x==0)return bigint(1,{0});
        if(x<0){
            bigint res=(*this)/(-x);
            res.f*=-1;
            return res;
        }
        bigint res(*this);
        long long t=0;
        for(int i=res.size()-1;i>=0;i--){
            t=t*base+res[i];
            res[i]=t/x;
            t%=x;
        }
        res.rlz();
        if(res.size()==1&&res[0]==0)res.f=1;
        else res.f=f;
        return res;
    }
	bigint operator*(const bigint&b)const{
        bigint a=(*this);
        int n=a.size(),m=b.size();
        bigint ans(n+m+1);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                int id=i+j;
                ans[id]+=a[i]*b[j];
            }
        }
        long long t = 0;
        for (int i=0;i<(int)ans.size();i++){
            ans.num[i]+=t;
            t=ans.num[i]/base;
            ans.num[i]%=base;
        }
        ans.rlz();
        return ans;
	}
    pair<bigint,bigint> divide(bigint&b)const{
        if(b.size()==1&&b[0]==0)throw runtime_error("Can not be zero");
        int ans_f=f*b.f;
        bigint a_abs=*this;
        a_abs.f=1;
        bigint b_abs=b;
        b_abs.f=1;
        if(a_abs<b_abs){
            return {a_abs,bigint(1,{0})};
        }
        bigint ans((int)a_abs.size());
        ans.f=ans_f;
        bigint t;
        t.f=1;
        for(int i=a_abs.size()-1;i>=0;i--){
            long long l=-1,r=base;
            t.num.insert(t.num.begin(),a_abs[i]);
            t.rlz();
            while(l+1<r){
                long long mid=l+(r-l)/2;
                bigint tmp=b_abs*bigint(1,{mid});
                if(tmp<=t){
                    l=mid;
                }
                else r=mid;
            }
            ans[i]=l;
            t=t-b_abs*bigint(1,{ans[i]});
        }
        ans.rlz();
        return {t,ans};
    }
	bigint operator/(bigint&b)const{
        return divide(b).second;
	}
    bigint operator%(bigint&b)const{
        return divide(b).first;
    }
    bigint operator+=(bigint&b)const{
        return (*this)+b;
    }
    bigint operator*=(bigint&b)const{
        return (*this)*b;
    }
    bigint operator/=(bigint&b)const{
        return (*this)/b;
    }
    bigint operator-=(bigint&b)const{
        return (*this)-b;
    }
    bigint operator%=(bigint&b)const{
        return (*this)%b;
    }
	friend istream&operator>>(istream&in,bigint&t){
		string s;
		if(!(in>>s))return in;
        int end=(s[0]=='-'?1:0);
        t.f=(s[0]=='-'?-1:1);
        t.num.clear();
		for(int i=s.size()-1;i>=end;i-=width){
            int len=min(width,i-end+1);
            string xx=s.substr(i-len+1,len);
            t.num.push_back(stoll(xx));
        }
        return in;
	}
	friend ostream&operator<<(ostream&os,bigint t){
        t.rlz();
        if (t.f==-1&&(t.size()>1||t.num[0]>0))os<<'-';
        os<<t.num.back();
        for(int i=t.size()-2;i>=0;i--) {
            os<<setfill('0')<<setw(width)<<t.num[i];
        }
        return os;
	}
};

st表

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int n,m;
template<class T,class compare=less<T>>
struct st{
    int logx[N];
    T f[N][30];
    compare cmp=compare();
    void to_log(T n){
        logx[1]=0;
        for(int i=2;i<=n;i++)logx[i]=logx[(i>>1)]+1;
    }
    void ST(T n){
        to_log(n);
        for(int j=1;(1<<j)<=n;j++){
            for(int i=1;i+(1<<j)-1<=n;i++){
                int l=f[i][j-1],r=f[i+(1<<(j-1))][j-1];
                f[i][j]=cmp(l,r)?r:l;
            }
        }
    }
    void build(T n,T a[]){
        for(int i=1;i<=n;i++)f[i][0]=a[i];
        ST(n);
    }
    void build(vector<T>a){
        for(int i=1;i<=a.size();i++)f[i][0]=a[i-1];
        ST(a.size());
    }
    T find(int l,int r){
        int cnt=0,x=r-l+1;
        cnt=logx[x];
        int left=f[l][cnt],right=f[r-(1<<cnt)+1][cnt];
        return cmp(left,right)?right:left;
    }
};
int a[N];
int main(){
    n=read();
    m=read();
    for(int i=1;i<=n;i++)a[i]=read();
    int l,r;
    st<int>ans;
    ans.build(n,a);
    while(m--){
        int l=read(),r=read();
        printf("%d\n",ans.find(l,r));
    }
}

并查集

C++
struct unifind{
    vector<int>fa;
    int n;
    unifind(int _n,int x=0){
        fa.resize(_n+1);
        n=_n;
        for(int i=0;i<=n;i++){
            fa[i]=i+x;
        }
    }
    int find(int x){
        if(fa[x]==x)return x;
        else {
            fa[x]=find(fa[x]);
            return fa[x];
        }
    }
    void merge(int x,int y){
        fa[find(x)]=find(y);
    }
    bool check(int x,int y){
        return find(x)==find(y);
    }
};

线段树

C++
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
const int N=4e5+5;
int a[N],d[N],lazy[N];
using namespace std;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch-48);
		ch=getchar();
	}
	return f*x;
}
void push_down(int s,int t,int p){
    if(lazy[p]==0)return;
    int mid=((s+t)>>1);
    d[(p<<1)]+=(mid-s+1)*lazy[p];
    d[(p<<1)+1]+=(t-mid)*lazy[p];
    lazy[p<<1]+=lazy[p];
    lazy[(p<<1)+1]+=lazy[p];
    lazy[p]=0;
}
int n;
int build(int s,int t,int p){
    int mid=((s+t)>>1);
    if(s==t){
        d[p]=a[s];
        return a[s];
    }
    int&ans=d[p];
    ans=build(s,mid,(p<<1))+build(mid+1,t,((p<<1)|1));
    return ans;
}
void update(int l,int r,int v,int s,int t,int p){
    int mid=((s+t)>>1);
    if(l<=s&&r>=t){
        lazy[p]+=v;
        d[p]+=v*(t-s+1);
        return;
    }
    push_down(s,t,p);
    if(l<=mid){
        update(l,r,v,s,mid,(p<<1));
    }
    if(r>mid){
        update(l,r,v,mid+1,t,(p<<1)+1);
    }
    d[p]=d[p<<1]+d[(p<<1)+1];
    return;
}
int find(int l,int r,int s,int t,int p){
    int mid=((s+t)>>1);
    if(l<=s&&r>=t)return d[p];
    int ans=0;
    push_down(s,t,p);
    if(l<=mid){
        ans=find(l,r,s,mid,(p<<1));
    }
    if(r>mid){
        ans+=find(l,r,mid+1,t,(p<<1)+1);
    }
    return ans;
}
void change(int x,int v,int s,int t,int p){
    if(s==t&&s==x){
        d[p]+=v;
        return;
    }
    if(s==t)return;
    int mid=((s+t)>>1);
    if(x<=mid){
        change(x,v,s,mid,(p<<1));
    }
    else{
        change(x,v,mid+1,t,(p<<1)+1);
    }
    d[p]=d[(p<<1)]+d[(p<<1)+1];
    return;
}
signed main() {
#if !defined(ONLINE_JUDGE)
	freopen("1.in", "r", stdin);
	//freopen("1.out", "w", stdout);
#endif    
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
    int m;
    n=read(),m=read();
    for(int i=1;i<=n;i++)a[i]=read();
    build(1,n,1);
    int x,y,k;
    while(m--){
        int op=read();
        if(op==1){
            x=read(),y=read(),k=read();
            update(x,y,k,1,n,1);
            // for(int i=x;i<=y;i++){
            //     change(i,k,1,n,1);
            // }
        }
        else{
            x=read(),y=read();
            cout<<find(x,y,1,n,1)<<endl;
        }
    }
	return 0;
}

快速幂

C++
long long qpow(long long a,long long b,long long p=mod){
    long long res=1;
    while(b){
        if(b&1)res=res*a%p;
        a=a*a%p;
        b>>=1;
    }
    return res%p;
}

st表

C++
struct bit{
	int c[N];
	int lowbit(int x){
		return (x)&(-x);
	}
	void build(){
		for(int i=1;i<=n;i++){
			c[i]=a[i];
		}
		for(int i=1;i<=n;i++){
			int fa=i+lowbit(i);
			if(fa<=n){
				c[fa]+=c[i];
			}
		}
	}
	void add(int i,int val){
		while(i<=n){
			c[i]+=val;
			i+=lowbit(i);
		}
	}
	int query(int i){
		int ans=0;
		while(i>0){
			ans+=c[i];
			i-=lowbit(i);
		}
		return ans;
	}
	int ask(int l,int r){
		return query(r)-query(l-1);
	}
};

树状数组

C++
struct bit{
	int c[N];
	int lowbit(int x){
		return (x)&(-x);
	}
	void build(){
		for(int i=1;i<=n;i++){
			c[i]=a[i];
		}
		for(int i=1;i<=n;i++){
			int fa=i+lowbit(i);
			if(fa<=n){
				c[fa]+=c[i];
			}
		}
	}
	void add(int i,int val){
		while(i<=n){
			c[i]+=val;
			i+=lowbit(i);
		}
	}
	int query(int i){
		int ans=0;
		while(i>0){
			ans+=c[i];
			i-=lowbit(i);
		}
		return ans;
	}
	int ask(int l,int r){
		return query(r)-query(l-1);
	}
};