跳到正文

个人记录

模板库

dong0717约 7 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月28日
篇幅
约 7 分钟
获赞
1

版本

  1. v1当前

dong0717这个大菜鸡的模板库

1. 快读

C++
int read(){
    char c=getchar_unlocked();
	int f=1;
	while(!isdigit(c)){
		if(c=='-') f=-1;
		c=getchar_unlocked();
	}
	int k=0;
	while(isdigit(c)){
		k=(k<<3)+(k<<1)+(c^'0');
		c=getchar_unlocked();
	}
	return f*k;
}

2.快速幂

C++
long long kkk(long long x,long long y,long long p){
	if(y==1)
	    return x%p;
	if(y%2==0){
		long long t=kkk(x,y/2)%p;
		return t*t%p;
	}
	if(y%2==1)
	    return kkk(x,y-1)*x%p;
}

3. 并查集

C++
#include<bits/stdc++.h>
using namespace std;
int n,m,f[10005];
int find(int x){
	if(f[x]==x) return x;
	return f[x]=find(f[x]);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) f[i]=i;
	for(int i=1;i<=m;i++){
		int op,p1,p2;
		scanf("%d%d%d",&op,&p1,&p2);
		if(op==1)
		    f[find(p1)]=find(p2);
		else{
			if(find(p1)==find(p2))
			    printf("Y\n");
			else
			    printf("N\n");
		}
	}
	return 0;
}

4. Floyd

C++
for(int i=1;i<=n;i++)
    f[i][i]=0;
for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            f[i][j]=min(f[i][j],f[i][k]+f[k][j]);

5. 最小生成树

C++
#include<bits/stdc++.h>
using namespace std;
struct date{
	int x,y,z;
}a[200005];
bool cmp(date xx,date yy){
	return xx.z<yy.z;
}
int n,m,f[5005],f2[5005];
int find(int x){
	if(f[x]==x) return x;
	return f[x]=find(f[x]);
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	    scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
	sort(a+1,a+m+1,cmp);
	for(int i=1;i<=n;i++)  f[i]=i;
	int cnt=0,ans=0;
	for(int i=1;i<=m;i++){
		int fx=find(a[i].x),fy=find(a[i].y);
		if(fx==fy)
            continue;
		ans+=a[i].z;
        f[fx]=fy;
        f2[a[i].x]=f2[a[i].y]=1;
		if(++cnt==n-1)
            break;
	}
    cout<<ans;
	return 0;
}

6. 堆优化Dijkstra

C++
#include<bits/stdc++.h>
using namespace std;
vector<pair<int,int> > a[100005];
struct node{
	int dis,pos;
	bool operator <(const node &x)const{
		return x.dis<dis;
	}
};
priority_queue<node> q;
bool vis[100005];
int ans[100005];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
	int n,m,s;
	cin>>n>>m>>s;
	for(int i=1;i<=m;i++){
	    int u,v,w;
	    cin>>u>>v>>w;
	    if(u!=v)
	        a[u].push_back(make_pair(v,w));
	}
	memset(ans,0x3f,sizeof(ans));
	for(auto it:a[s]){
		ans[it.first]=min(it.second,ans[it.first]);
		q.push({ans[it.first],it.first});
	}
	ans[s]=0;
	vis[s]=true;
	while(!q.empty()){
		int k=q.top().pos;
		q.pop();
		if(vis[k])
		    continue;
		vis[k]=true;
		for(auto it:a[k]){
			if(ans[k]+it.second<ans[it.first]){
		        ans[it.first]=ans[k]+it.second;
		        if(!vis[it.first])
		            q.push({ans[it.first],it.first});
			}
		}
	}
	for(int i=1;i<=n;i++)
	    cout<<ans[i]<<" ";
	return 0;
}

7.ST表

C++
#include<bits/stdc++.h>
using namespace std;
int f[100005][30],lg[100005];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
	cin>>n>>m;
    lg[1]=0;
    for(int i=2;i<=n;i++)
        lg[i]=lg[i>>1]+1;
    for(int i=1;i<=n;i++)
        f[i][0]=read();
    for(int j=1;j<=lg[n];j++){
        for(int i=1;i<=n-(1<<j)+1;i++)
            f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
    }
    for(int i=1;i<=m;i++){
        int x,y;
        ciin>>x>>y;
        int t=lg[y-x+1];
        cout<<max(f[x][t],f[y-(1<<t)+1][t])<<'\n';
    }
	return 0;
}
原文