跳到正文

闲话

ALL 板子

Love_Ti_fjx约 21 分钟
原文

这是 2026年9月21日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年9月2日
收录
2026年9月21日
更新
2026年11月20日
篇幅
约 21 分钟
获赞
1

版本

  1. v2当前
  2. v1
数学性质类
快读快写
C++
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-'0';ch=getchar();}
    return x*f;
}
inline void write(int x) {
	if(x<0){putchar('-');x=-x;}
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
并查集
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=2e5+10;
int n,m;
int fa[N];
int find(int x){
	if(fa[x]==x) return x;
	else return fa[x]=find(fa[x]);
}
void merge(int x,int y){
	int fx=find(x),fy=find(y);
	if(fx!=fy) fa[fx]=fy;
}
void init(){for(int i=1;i<=n;i++) fa[i]=i;}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	init();
	for(int i=1;i<=m;i++){
		int z,x,y;
		cin>>z>>x>>y;
		if(z==1) merge(x,y);
		else{
			int fx=find(x),fy=find(y);
			if(fx==fy) cout<<"Y\n";
			else cout<<"N\n";
		}
	}
	return 0;
}
线性筛素数
C++
#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e8+10;
int n,q,cnt;
int a[N];
bool f[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>q;
	for(int i=1;i<N;i++) f[i]=true;
	f[0]=f[1]=false;
	for(int i=2;i<=n;i++){
		if(f[i]) a[++cnt]=i;
		for(int j=1;j<=cnt&&a[j]*i<=n;j++){
			f[a[j]*i]=false;
			if(i%a[j]==0) break;
		}
	}
	for(int i=1;i<=q;i++){
		int k;
		cin>>k;
		cout<<a[k]<<'\n';
	}
	return 0;
}
堆

用优先队列的,手写堆慢但是不会写

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
int n;
priority_queue<int,vector<int>,greater<int>> q;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
	    int op;
		cin>>op;
		if(op==1){
			int x;
			cin>>x;
			q.push(x);
		}
		else if(op==2) cout<<q.top()<<'\n';
		else q.pop();
	}
	return 0;
}
快速幂
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
int mod;
int qpow(int a,int b){
	int res=1;
	while(b){
		if(b&1) res=((res%mod)*(a%mod))%mod;
		a=((a%mod)*(a%mod))%mod;
		b>>=1;
	}
	return res%mod;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int a,b;
	cin>>a>>b>>mod;
	cout<<a<<"^"<<b<<" mod "<<mod<<"="<<qpow(a,b);
	return 0;
}
单调栈
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=3e6+10;
int n;
int a[N],ans[N];
stack<int> s;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=n;i>=1;i--){
		while(!s.empty()&&a[s.top()]<=a[i]) s.pop();//把在a[i]后面小的都弹出 不会是最近的
		if(!s.empty()) ans[i]=s.top();//最近的那个
		else ans[i]=0;//后面没有比他大的
		s.push(i);//把他加入栈和前面去比
	}
	for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
	return 0;
}
单调队列
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e6+10;
int n,k,l=1,r;
int a[N],q[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++){
		while(l<=r&&a[q[r]]>=a[i]) r--;//如果当前位置大于后面那个 那么把这个位置弹出
		q[++r]=i;//加入当前的队列
		while(l<=r&&q[l]+k<=i) l++;//弹出过期元素
		if(i>=k) cout<<a[q[l]]<<" ";//当前最小值
	}
	cout<<'\n';
	l=1,r=0;
	for(int i=1;i<=n;i++){
		while(l<=r&&a[q[r]]<=a[i]) r--;//最大值则相反即可
		q[++r]=i;
		while(l<=r&&q[l]+k<=i) l++;
		if(i>=k) cout<<a[q[l]]<<" ";
	}
	return 0;
}
dijkstra
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=4e5+10;
const int INF=1e18;
int n,m,s,cnt;
int head[N],ne[N],to[N],we[N],dist[N];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
void add(int u,int v,int w){
	ne[++cnt]=head[u];
	head[u]=cnt;
	to[cnt]=v;
	we[cnt]=w;
}
void dij(int s){
	for(int i=1;i<=n;i++) dist[i]=INF;
	dist[s]=0;
	q.push({0,s});
	while(!q.empty()){
		int d=q.top().f1;
		int k=q.top().s2;
		q.pop();
		if(d>dist[k]) continue;
		for(int i=head[k];i;i=ne[i]){
			int v=to[i];
			int val=we[i];
			if(dist[k]+val<dist[v]){
				dist[v]=dist[k]+val;
				q.push({dist[v],v});
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m>>s;
	for(int i=1;i<=m;i++){
		int u,v,w;
		cin>>u>>v>>w;
		add(u,v,w);
	}
	dij(s);
	for(int i=1;i<=n;i++) cout<<dist[i]<<" ";
	return 0;
}
离散化
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e5+5;
int n;
int a[N],b[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int t;
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			b[i]=a[i];
		}
		sort(b+1,b+n+1);
		int tot=unique(b+1,b+n+1)-b-1;
		for(int i=1;i<=n;i++){
			int rk=lower_bound(b+1,b+tot+1,a[i])-b;
			cout<<rk<<" ";
		}
		cout<<'\n';
	}
	return 0;
}
字符串hash

map 写法还是牛/qiang。

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
map<string,bool> ma;
int n;
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	int sum=0;
    for(int i=1;i<=n;i++){
		string s;
		cin>>s;
		if(ma[s]) continue;
		else ma[s]=true,sum++;
	}
	cout<<sum;
	return 0;
}
LIS nlogn
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e5+10;
int a[N],d[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	int len=0;
	for(int i=1;i<=n;i++){
		if(len==0||a[i]>d[len]) d[++len]=a[i];
		else{
			int pos=lower_bound(d+1,d+1+len,a[i])-d;
			d[pos]=a[i];
		}
	}
	cout<<len;
	return 0;
}
LCS
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e5+10;
int n;
int a[N],b[N],x[N],d[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++) cin>>b[i],x[b[i]]=i;
	for(int i=1;i<=n;i++) a[i]=x[a[i]];
	int len=0;
	for(int i=1;i<=n;i++){
		if(len==0||a[i]>d[len]) d[++len]=a[i];
		else{
			int pos=lower_bound(d+1,d+1+len,a[i])-d;
			d[pos]=a[i];
		}
	}
	cout<<len;
	return 0;
}
链表
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e6+10;
struct node{
	bool f=false;
	int ne=0;
}a[N];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int q;
	cin>>q;
	a[1].f=true;
	while(q--){
		int op;
		cin>>op;
		if(op==1){
			int x,y;
			cin>>x>>y;
			a[y].ne=a[x].ne;
			a[x].ne=y;
		}
		else if(op==2){
			int x;
			cin>>x;
			cout<<a[x].ne<<'\n';
		}
		else{
			int x;
			cin>>x;
			a[x].ne=a[a[x].ne].ne;
		}
	}
	return 0;
}
树状数组/单点加区间查
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e6+10;
int n,m;
int a[N];
int lowbit(int x){return x&(-x);}
void update(int pos,int v){for(;pos<=n;pos+=lowbit(pos)) a[pos]+=v;}
int query(int pos){
	int sum=0;
	for(;pos>0;pos-=lowbit(pos)) sum+=a[pos];
	return sum;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		update(i,x);
	}
	while(m--){
		int op;
		cin>>op;
		if(op==1){
			int x,k;
			cin>>x>>k;
			update(x,k);
		}
		else{
			int x,y;
			cin>>x>>y;
			cout<<query(y)-query(x-1)<<'\n';
		}
	}
	return 0;
}
树状数组/区间加单点查/差分
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e6+10;
int n,m;
int a[N];
int lowbit(int x){return x&(-x);}
void update(int pos,int v){for(;pos<=n;pos+=lowbit(pos)) a[pos]+=v;}
int query(int pos){
	int sum=0;
	for(;pos>0;pos-=lowbit(pos)) sum+=a[pos];
	return sum;
}
void add(int l,int r,int k){update(l,k),update(r+1,-k);}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
		add(i,i,x);
	}
	while(m--){
		int op;
		cin>>op;
		if(op==1){
			int x,y,k;
			cin>>x>>y>>k;
			add(x,y,k);
		}
		else{
			int x;
			cin>>x;
			cout<<query(x)<<'\n';
		}
	}
	return 0;
}
最小生成树kruskal
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=2e5+10;
int n,m;
int fa[N];
struct node{int u,v,w;}e[N];
bool cmp(node x,node y){return x.w<y.w;}
int find(int x){if(x==fa[x]) return x;else return fa[x]=find(fa[x]);}
void init(){for(int i=1;i<=n;i++) fa[i]=i;}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	init();
	for(int i=1;i<=m;i++) cin>>e[i].u>>e[i].v>>e[i].w;
	sort(e+1,e+m+1,cmp);
	int ans=0,cnt=0;
	for(int i=1;i<=m;i++){
		int u=e[i].u,v=e[i].v;
		int fu=find(u),fv=find(v);
		if(fu!=fv){
			ans+=e[i].w;
			fa[fu]=fv;
			cnt++;
		}
		if(cnt==n-1) break;
	}
	if(cnt==n-1) cout<<ans;
	else cout<<"orz";
	return 0;
}
ST表(实在看不懂背板子 学猫树去/ll)
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define y1 y_1
#define f1 first
#define s2 second
const int N=1e5+10;
int n,m;
int st[N][20];
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>st[i][0];
	int k=log2(n);
	for(int j=1;j<=k;j++){
		for(int i=1;i<=n-(1<<j)+1;i++){
			st[i][j]=max(st[i][j-1],st[i+(1<<(j-1))][j-1]);
		}
	}
	while(m--){
		int l,r;
		cin>>l>>r;
		int k=log2(r-l+1);
		cout<<max(st[l][k],st[r-(1<<k)+1][k])<<'\n';
	}
	return 0;
}
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码
INFO
代码