跳到正文

个人记录

各种模板

FishDeity约 44 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年3月1日
篇幅
约 44 分钟

版本

  1. v3当前
  2. v2
  3. v1

各种模板

字符串相关
【模板】manacher
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 11000005
char s[N<<1];
LL dp[N<<1];
int main(){
	LL i,l,c,len=1,r=0,ans=1,mid;
	string a;
	cin>>a;
	l=a.length();
	s[0]=s[1]='#';
	for(i=0;i<l;i++){
		s[++len]=a[i];
		s[++len]='#';
	}
	l=len+1;
	s[l]=0;
	for(i=1;i<l;i++){
		if(i<r)dp[i]=min(dp[mid*2-i],dp[mid]+mid-i);
		else dp[i]=1;
		while(s[i+dp[i]]==s[i-dp[i]])++dp[i];
		if(dp[i]+i>r){
			r=dp[i]+i;
			mid=i;
		}
		ans=max(ans,dp[i]);
	}
	printf("%lld",ans-1);
	return 0;
}
【模板】KMP
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL Next[1000005];
int main(){
	string s,t;
	LL ls,lt,i,p=0;
	cin>>s>>t;
	ls=s.length(),lt=t.length();
	s=' '+s,t=' '+t;
	for(i=2;i<=lt;i++){
		while(p&&t[p+1]!=t[i])p=Next[p];
		if(t[p+1]==t[i])p++;
		Next[i]=p;
	}
	p=0;
	for(i=1;i<=ls;i++){
		while(p&&t[p+1]!=s[i])p=Next[p];
		if(t[p+1]==s[i])p++;
		if(p==lt){
			p=Next[lt];
			printf("%lld\n",i-lt+1);
		}
	}
	for(i=1;i<=lt;i++)printf("%lld ",Next[i]);
	return 0;
}
【模板】AC 自动机
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 200005
inline LL read(){
	char ch=getchar();
	int w=1;
	LL s=0;
	while(ch<'0'||ch>'9'){
		if(ch=='-')w*=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		s=(s<<3)+(s<<1)+(ch^48);
		ch=getchar();
	}
	return w*s;
}
inline void write(LL n){
	if(n<0)putchar('-'),n=-n;
	if(n>9)write(n/10);
	putchar(n%10+'0');
	return ;
}
LL n,tr[N][26],fail[N],edgecnt,e[N],sum[N];
vector<LL>refail[N];
inline LL ins(string str){
	LL u=0;
	for(auto ch:str){
		int c=ch-'a';
		if(!tr[u][c])tr[u][c]=++edgecnt;
		u=tr[u][c];
	}
	return u;
}
void bfs(){
	queue<LL>q;
	for(LL i=0;i<26;i++)
		if(tr[0][i])
			q.push(tr[0][i]);
	while(!q.empty()){
		LL tp=q.front();
		q.pop();
		for(LL i=0;i<26;i++){
			if(tr[tp][i]){
				fail[tr[tp][i]]=tr[fail[tp]][i];
				q.push(tr[tp][i]);
			}
			else tr[tp][i]=tr[fail[tp]][i];
		}
	}
}
inline void dfs(LL pos){
	// printf("pos:%lld\n",pos);
	for(LL i:refail[pos]){
		dfs(i);
		sum[pos]+=sum[i];
	}
}
	LL pos;
int main(){
	string str,target;
	n=read();
	for(LL i=1;i<=n;i++){
		cin>>str;
		e[i]=ins(str);
	}
	// printf("start bfs\n");
	bfs();
	for(LL i=1;i<=edgecnt;i++)
		refail[fail[i]].push_back(i);
	cin>>target;
	// printf("start two\n");
	for(auto ch:target){
		int c=ch-'a';
		pos=tr[pos][c];
		sum[pos]++;
	}
	// printf("start dfs\n");
	dfs(0);
	for(LL i=1;i<=n;i++)
		printf("%lld\n",sum[e[i]]);
	return 0;
}
Tarjan相关
【模板】缩点
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 10005
#define M 100005
struct node
{
	LL from,to,next;
}edge[M],ed[M];
LL vis[N],top,stk[N],n,m,sum,s,head[N],h[N],low[N],dfn[N],tim,sd[N],p[N],in[N],dist[N];
void add(LL x,LL y)
{
	edge[++sum].next=head[x];
	edge[sum].from=x;
	edge[sum].to=y;
	head[x]=sum;
}
void tarjan(LL x)
{
	low[x]=dfn[x]=++tim;
	stk[++top]=x;
	vis[x]=1;
	for(LL i=head[x];i;i=edge[i].next)
	{
		LL v=edge[i].to;
		if(!dfn[v])
		{
			tarjan(v);
			low[x]=min(low[x],low[v]);
		}
		else if(vis[v])
			low[x]=min(low[x],dfn[v]);
	}
	if(dfn[x]==low[x])
	{
		LL y;
		while(y=stk[top--])
		{
			sd[y]=x;
			vis[y]=0;
			if(x==y)break;
			p[x]+=p[y];
		}
	}
}
int main()
{
	LL i,x,y;
	scanf("%lld%lld",&n,&m);
	for(i=1;i<=n;i++)scanf("%lld",&p[i]);
	for(i=1;i<=m;i++)
	{
		scanf("%lld%lld",&x,&y);
		add(x,y);
	}
	for(i=1;i<=n;i++)
		if(!dfn[i])
			tarjan(i);
	for(i=1;i<=m;i++)
	{
		LL x=sd[edge[i].from],y=sd[edge[i].to];
		if(x!=y)
		{
			ed[++s].next=h[x];
			ed[s].to=y;
			ed[s].from=x;
			h[x]=s;
			in[y]++;
		}
	}

	queue<LL>q;
	for(i=1;i<=n;i++)
		if(sd[i]==i&&!in[i])
			q.push(i),dist[i]=p[i];
	while(!q.empty())
	{
		LL k=q.front();
		q.pop();
		for(i=h[k];i;i=ed[i].next)
		{
			LL v=ed[i].to;
			dist[v]=max(dist[v],dist[k]+p[v]);
			if(!--in[v])q.push(v);
		}
	}
	LL ans=0;
	for(i=1;i<=n;i++)
		ans=max(ans,dist[i]);
	printf("%lld",ans);
	return 0;
}
【模板】割点
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 20005
LL n,m,cnt,dfn[N],low[N],tim,top,stk[N];
vector<LL>edge[N];
bool cut[N];
void tarjan(LL x,LL fa)
{
	dfn[x]=low[x]=++tim;
	LL i,ch=0;
	for(i=0;i<edge[x].size();i++)
	{
		LL v=edge[x][i];
		if(!dfn[v])
		{
			tarjan(v,fa);
			low[x]=min(low[x],low[v]);
			if(low[v]>=dfn[x]&&x!=fa)
				if(!cut[x])
					cut[x]=1,cnt++;
			if(x==fa)ch++;
		}
		else
			low[x]=min(low[x],dfn[v]);
	}
	if(ch>=2&&x==fa)
		if(!cut[x])
			cut[x]=1,cnt++;
}
int main()
{
	LL i,x,y;
	scanf("%lld%lld",&n,&m);
	for(i=1;i<=m;i++)
	{
		scanf("%lld%lld",&x,&y);
		edge[x].push_back(y);
		edge[y].push_back(x);
	}
	for(i=1;i<=n;i++)
		if(!dfn[i])
			tarjan(i,i);
	printf("%lld\n",cnt);
	for(i=1;i<=n;i++)
		if(cut[i])
			printf("%lld ",i);
	return 0;
}
【模板】2-SAT
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 4000005
inline LL read(){
	char ch=getchar();
	int w=1;
	LL s=0;
	while(ch<'0'||ch>'9')
	{
		if(ch=='-')w*=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		s=(s<<3)+(s<<1)+(ch^48);
		ch=getchar();
	}
	return w*s;
}
inline void write(LL n){
	if(n<0)putchar('-'),n=-n;
	if(n>9)write(n/10);
	putchar(n%10+'0');
	return ;
}

LL cnt,tim,top,head[N],dfn[N],low[N],vis[N],st[N],scc[N],sum;
struct node{
	LL to,next;
}edge[N];
void add(LL x,LL y){
	edge[++cnt].next=head[x];
	edge[cnt].to=y;
	head[x]=cnt;
}
void tarjan(LL x){
	dfn[x]=low[x]=++tim;
	st[++top]=x;
	vis[x]=1;
	for(LL i=head[x];i;i=edge[i].next){
		LL y=edge[i].to;
		if(!dfn[y]){
			tarjan(y);
			low[x]=min(low[x],low[y]);
		}
		else if(vis[y])low[x]=min(low[x],dfn[y]);
	}
	if(dfn[x]==low[x]){
		sum++;
		while(st[top]!=x){
			scc[st[top]]=sum;
			vis[st[top]]=0;
			top--;
		}
		scc[st[top]]=sum;
		vis[st[top]]=0;
		top--;
	}
}
LL n,m;
int main(){
	LL a,x,b,y;
	n=read(),m=read();
	while(m--){
		a=read(),x=read(),b=read(),y=read();
		if(x==0&&y==0){
            add(a+n,b);
            add(b+n,a);
        }
        if(x==0&&y==1){
            add(a+n,b+n);
            add(b,a);
        }
        if(x==1&&y==0){
            add(a,b);
            add(b+n,a+n);
        }
        if(x==1&&y==1){
            add(a,b+n);
			add(b,a+n);
        }
		//add(a+n*(!x),b+n*y);
		//add(b+n*x,a+n*(!y));
	}
	for(LL i=1;i<=2*n;i++)
		if(!dfn[i])
			tarjan(i);
	for(LL i=1;i<=n;i++)
		if(scc[i]==scc[i+n]){
            printf("IMPOSSIBLE\n");
            return 0;
		}
    printf("POSSIBLE\n");
    for(LL i=1;i<=n;i++){
    	if(scc[i]>scc[i+n])printf("1 ");
    	else printf("0 ");
	}
	return 0;
}
线段树相关
【模板】线段树
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 100005
#define ls p<<1
#define rs p<<1|1
LL a[N],tree[N<<2],mark[N<<2];
void build(LL l,LL r,LL p){
	if(l==r)tree[p]=a[l];
	else{
		LL mid=l+r>>1;
		build(l,mid,ls);
		build(mid+1,r,rs);
		tree[p]=tree[ls]+tree[rs];
	}
}
void pd(LL p,LL len){
	mark[ls]+=mark[p];
	mark[rs]+=mark[p];
	tree[ls]+=mark[p]*(len-len/2);
	tree[rs]+=mark[p]*(len/2);
	mark[p]=0;
}
void update(LL l,LL r,LL cl,LL cr,LL p,LL d){
	if(cl>r||cr<l)return ;
	else if(l<=cl&&cr<=r){
		tree[p]+=d*(cr-cl+1);
		if(cl<cr)mark[p]+=d;
	}
	else{
		LL mid=cl+cr>>1;
		pd(p,cr-cl+1);
		update(l,r,cl,mid,ls,d);
		update(l,r,mid+1,cr,rs,d);
		tree[p]=tree[ls]+tree[rs];
	}
}
LL query(LL l,LL r,LL cl,LL cr,LL p){
	if(cl>r||cr<l)return 0;
	else if(l<=cl&&cr<=r)return tree[p];
	else{
		LL mid=cl+cr>>1;
		pd(p,cr-cl+1);
		return query(l,r,cl,mid,ls)+query(l,r,mid+1,cr,rs);
	}
}
int main(){
	LL op,x,y,k,n,q;
	scanf("%lld%lld",&n,&q);
	for(LL i=1;i<=n;i++)scanf("%lld",&a[i]);
	build(1,n,1);
	while(q--){
		scanf("%lld%lld%lld",&op,&x,&y);
		if(op==1){
			scanf("%lld",&k);
			update(x,y,1,n,1,k);
		}
		else{
			printf("%lld\n",query(x,y,1,n,1));
		}
	}
	return 0;
}
【模板】可持久化线段树
C++
#include<bits/stdc++.h>
using namespace std;
int n,m,root[1000005],cnt;
struct node
{
	int ls,rs,val;
}st[32000005];
void build(int &pos,int l,int r)
{
	pos=++cnt;
	if(l==r)
	{
		scanf("%d",&st[pos].val);
		return ;
	}
	int mid=l+r>>1;
	build(st[pos].ls,l,mid);
	build(st[pos].rs,mid+1,r);
}
void update(int &point,int last,int l,int r,int pos,int val)
{
	point=++cnt;
	st[point]=st[last];
	if(l==r)
	{
		st[point].val=val;
		return ;
	}
	int mid=l+r>>1;
	if(pos<=mid)update(st[point].ls,st[last].ls,l,mid,pos,val);
	else update(st[point].rs,st[last].rs,mid+1,r,pos,val);
}
int query(int point,int l,int r,int loc)
{
	if(l==r)return st[point].val;
	int mid=l+r>>1;
	if(loc<=mid)return query(st[point].ls,l,mid,loc);
	else return query(st[point].rs,mid+1,r,loc);
}
int main()
{
	int last,op,x,y,i;
	scanf("%d%d",&n,&m);
	build(root[0],1,n);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d",&last,&op);
		if(op==1)
		{
			scanf("%d%d",&x,&y);
			update(root[i],root[last],1,n,x,y);
		}
		else
		{
			scanf("%d",&x);
			printf("%d\n",query(root[last],1,n,x));
			root[i]=root[last];
		}
	}
	return 0;
}
【模板】树链剖分
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 400005
#define ls p<<1
#define rs p<<1|1
vector<LL>edge[N];
LL cnt,n,mod,top[N],son[N],wt[N],w[N],tree[N],mark[N],id[N],siz[N],deep[N],fa[N];
void dfs1(LL p,LL f,LL dep){
	fa[p]=f;
	siz[p]=1;
	deep[p]=dep;
	LL maxson=0;
	for(LL i=0;i<edge[p].size();i++){
		LL v=edge[p][i];
		if(v==f)continue;
		dfs1(v,p,dep+1);
		siz[p]+=siz[v];
		if(siz[v]>maxson){
			maxson=siz[v];
			son[p]=v;
		}
	}
}
void dfs2(LL p,LL topf){
	id[p]=++cnt;
	wt[id[p]]=w[p];
	top[p]=topf;
	if(!son[p])return ;
	dfs2(son[p],topf);
	for(LL i=0;i<edge[p].size();i++){
		LL v=edge[p][i];
		if(v==fa[p]||v==son[p])continue;
		dfs2(v,v);
	}
}
void build(LL l,LL r,LL p){
	if(l==r)tree[p]=wt[l]%mod;
	else{
		LL mid=l+r>>1;
		build(l,mid,ls);
		build(mid+1,r,rs);
		tree[p]=(tree[ls]+tree[rs])%mod;
	}
}
void pd(LL p,LL len){
	mark[ls]+=mark[p];
	mark[rs]+=mark[p];
	tree[ls]=(tree[ls]+mark[p]*(len-len/2))%mod;
	tree[rs]=(tree[rs]+mark[p]*(len/2))%mod;
	mark[p]=0;
}
void update(LL l,LL r,LL cl,LL cr,LL p,LL d){
	if(cr<l||r<cl)return ;
	else if(l<=cl&&cr<=r){
		tree[p]=(tree[p]+d*(cr-cl+1))%mod;
		if(cl<cr)mark[p]+=d;
	}
	else{
		LL mid=cl+cr>>1;
		pd(p,cr-cl+1);
		update(l,r,cl,mid,ls,d);
		update(l,r,mid+1,cr,rs,d);
		tree[p]=(tree[ls]+tree[rs])%mod;
	}
}
LL query(LL l,LL r,LL cl,LL cr,LL p){
	if(cr<l||r<cl)return 0;
	else if(l<=cl&&cr<=r)return tree[p];
	else{
		LL mid=cl+cr>>1;
		pd(p,cr-cl+1);
		return (query(l,r,cl,mid,ls)+query(l,r,mid+1,cr,rs))%mod;
	}
}
void updtree(LL x,LL y,LL d){
	d%=mod;
	while(top[x]!=top[y]){
		if(deep[top[x]]<deep[top[y]])swap(x,y);
		update(id[top[x]],id[x],1,n,1,d);
		x=fa[top[x]];
	}
	if(deep[x]>deep[y])swap(x,y);
	update(id[x],id[y],1,n,1,d);
}
LL qrytree(LL x,LL y){
	LL ans=0;
	while(top[x]!=top[y]){
		if(deep[top[x]]<deep[top[y]])swap(x,y);
		ans=(ans+query(id[top[x]],id[x],1,n,1))%mod;
		x=fa[top[x]];
	}
	if(deep[x]>deep[y])swap(x,y);
	return (ans+query(id[x],id[y],1,n,1))%mod;
}
void updson(LL x,LL d){
	update(id[x],id[x]+siz[x]-1,1,n,1,d%mod);
}
LL qryson(LL x){
	return query(id[x],id[x]+siz[x]-1,1,n,1)%mod;
}
int main(){
	LL op,x,y,z,q,rt;
	scanf("%lld%lld%lld%lld",&n,&q,&rt,&mod);
	for(LL i=1;i<=n;i++)scanf("%lld",&w[i]);
	for(LL i=1;i<n;i++){
		scanf("%lld%lld",&x,&y);
		edge[x].push_back(y);
		edge[y].push_back(x);
	}
	dfs1(rt,0,1);
	dfs2(rt,rt);
	build(1,n,1);
	while(q--){
		scanf("%lld",&op);
		if(op==1){
			scanf("%lld%lld%lld",&x,&y,&z);
			updtree(x,y,z);
		}
		else if(op==2){
			scanf("%lld%lld",&x,&y);
			printf("%lld\n",qrytree(x,y));
		}
		else if(op==3){
			scanf("%lld%lld",&x,&y);
			updson(x,y);
		}
		else{
			scanf("%lld",&x);
			printf("%lld\n",qryson(x));
		}
	}
	return 0;
}
【模板】树状数组
单点修改 区间查询
C++
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define N 200005
LL a[N],n;
LL lowbit(LL x){
	return x&-x;
}
void add(LL x,LL d){
	for(LL i=x;i<=n;i+=lowbit(i))
		a[i]+=d;
}
LL query(LL x){
	LL res=0;
	for(LL i=x;i;i-=lowbit(i))
		res+=a[i];
	return res;
}
int main(){
	LL m,x,y,op;
	scanf("%lld%lld",&n,&m);
	for(LL i=1;i<=n;i++){
		scanf("%lld",&x);
		add(i,x);
	}
	while(m--){
		scanf("%lld%lld%lld",&op,&x,&y);
		if(op==1)add(x,y);
		else printf("%lld\n",query(max(x,y))-query(min(x,y)-1));
	}
	return 0;
}
区间修改 单点查询
C++
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define N 100005
LL n,a[N];
LL lowbit(LL x){
	return x&-x;
}
void add(LL x,LL d){
	for(LL i=x;i<=n;i+=lowbit(i))
		a[i]+=d;
}
LL ask(LL x){
	LL res=0;
	for(LL i=x;i;i-=lowbit(i))
		res+=a[i];
	return res;
}
int main(){
	LL m,op,x,y,z;
	scanf("%lld%lld",&n,&m);
	for(LL i=1;i<=n;i++){
		scanf("%lld",&x);
		add(i,x);
		add(i+1,-x);
	}
	while(m--){
		scanf("%lld",&op);
		if(op==1){
			scanf("%lld%lld%lld",&x,&y,&z);
			add(x,z);
			add(y+1,-z);
		}
		else{
			scanf("%lld",&x);
			printf("%lld\n",ask(x));
		}
	}
	return 0;
}
区间修改 区间查询
C++
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define N 500005
LL n,t1[N],t2[N];
LL lowbit(LL x){
	return x&-x;
}
void add(LL x,LL d){
	for(LL i=x;i<=n;i+=lowbit(i))
		t1[i]+=d,t2[i]+=d*x;
}
LL ask(LL x){
	LL res=0;
	for(LL i=x;i;i-=lowbit(i))
		res+=(x+1)*t1[i]-t2[i];
	return res;
}
int main(){
	LL m,op,x,y,z;
	scanf("%lld%lld",&n,&m);
	for(LL i=1;i<=n;i++){
		scanf("%lld",&x);
		add(i,x);
		add(i+1,-x);
	}
	while(m--){
		scanf("%lld",&op);
		if(op==1){
			scanf("%lld%lld%lld",&x,&y,&z);
			add(x,z);
			add(y+1,-z);
		}
		else{
			scanf("%lld%lld",&x,&y);
			printf("%lld\n",ask(y)-ask(x-1));
		}
	}
	return 0;
}
最短路相关
【模板】单源最短路径
C++
#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL head[5000005],cnt,dist[100005],n ,m,s;
bool vis[100005] ;
struct qwq
{
	int v,next,c;
}edge[5000005];
inline void add(int x,int y,int c)
{
	edge[++cnt]=(qwq){y,head[x],c};
	head[x]=cnt;
}
priority_queue<pair<int, int> >q;
void dijkstra()
{
	int i;
	for(i=1;i<=n;i++)dist[i]=0x7fffffff;
	memset(vis,0,sizeof(vis));
	dist[s]=0;
	q.push({0,s});
	while(!q.empty())
	{
		int u=q.top().second;
		q.pop();
		if(!vis[u])
		{
			vis[u]=1;
			for(i=head[u];i!=-1;i=edge[i].next)
			{
				int v=edge[i].v,z=edge[i].c;
				if(dist[v] >dist[u]+z)
				{
					dist[v]=dist[u]+z;
					q.push({-dist[v],v});
				}
			}
		}
	}
}
int main()
{
	int x,y,z,i;
	cin>>n>>m>>s;
	memset(head,-1,sizeof(head));
	for(i=1;i<=m;i++)
	{
		cin>>x>>y>>z;
		add(x,y,z);
	}
	dijkstra();
	for(i=1;i<=n;i++)
		printf("%lld ",dist[i]);
	return 0;
}
【模板】差分约束
C++
#include<bits/stdc++.h>
using namespace std;
struct node
{
	int v,w,next;
}edge[10005];
int  head[10005],tot[10005],cnt,n,m;
int vis[5005];
long long dis[5005];
void add(int u,int v,int w)
{
	edge[++cnt].v=v;
	edge[cnt].w=w;
	edge[cnt].next=head[u];
	head[u]=cnt;
}
bool spfa(int s)
{
	queue<int>q;
	memset(dis,63,sizeof(dis));
	dis[s]=0;
	vis[s]=1;
	q.push(s);
	while(!q.empty())
	{
		int tp=q.front();
		q.pop();
		vis[tp]=0;
		for(int i=head[tp];i;i=edge[i].next)
		{
			int v=edge[i].v;
			if(dis[v]>dis[tp]+edge[i].w)
			{
				dis[v]=dis[tp]+edge[i].w;
				if(!vis[v])
				{
					vis[v]=1;
					tot[v]++;
					if(tot[v]==n+1)return 0;
					q.push(v);
				}
			}
		}
	}
	return 1;
}
int main()
{
	int i,x,y,z;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)add(0,i,0);
	for(i=1;i<=m;i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		add(y,x,z);
	}
	if(!spfa(0))printf("NO");
	else
		for(i=1;i<=n;i++)
			printf("%lld ",dis[i]);
	return 0;
}
DP相关
【模板】最长公共子序列
C++
#include<bits/stdc++.h>
using namespace std;
int a[100005],apos[100005],b[100005],bpos[100005],num[100005];
int main()
{
	int n,i,temp,ans=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)scanf("%d",&a[i]),apos[a[i]]=i;
	for(i=1;i<=n;i++)scanf("%d",&b[i]),bpos[i]=apos[b[i]];
	num[++ans]=bpos[1];
    for(i=2;i<=n;i++)
	{
        temp=lower_bound(num+1,num+ans+1,bpos[i])-num;
        num[temp]=bpos[i];
        if(temp>ans)
            ans++;
    }
	cout<<ans;
	return 0;
}
STL相关
【模板】单调队列
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 1000005
deque<LL>dq,dq2;
LL out[N][2],a[N];
int main(){
    LL i,n,k,cnt0=0;
    scanf("%lld%lld",&n,&k);
    for(i=1;i<=n;i++)scanf("%lld",&a[i]);
    for(i=1;i<=n;i++){
        while(!dq.empty()&&dq.front()<=i-k)dq.pop_front();
        while(!dq2.empty()&&dq2.front()<=i-k)dq2.pop_front();
        while(!dq.empty()&&a[dq.back()]>=a[i])dq.pop_back();
        while(!dq2.empty()&&a[dq2.back()]<=a[i])dq2.pop_back();
        dq.push_back(i);
        dq2.push_back(i);
        if(i>=k){
            out[++cnt0][0]=a[dq.front()];
            out[cnt0][1]=a[dq2.front()];
        }
    }
    for(i=1;i<=cnt0;i++)printf("%lld ",out[i][0]);
    printf("\n");
    for(i=1;i<=cnt0;i++)printf("%lld ",out[i][1]);
    return 0;
}
树相关
【模板】最小生成树
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
LL f[201005];
struct node
{
	LL x,y,z;
}a[200105];
bool cmp(node a,node b)
{
	return a.z<b.z;
}
LL find(LL x)
{
	LL temp=x;
	while(f[x]!=x)x=f[x];
	return f[temp]=x;
}
bool judge(LL x,LL y)
{
	return find(x)==find(y);
}
void unionn(LL x,LL y)
{
	f[find(x)]=find(y);
}
int main()
{
	LL i,n,m,cnt=0,ans=0;
	scanf("%lld%lld",&n,&m);
	for(i=1;i<=m;i++)scanf("%lld%lld%lld",&a[i].x,&a[i].y,&a[i].z);
	for(i=1;i<=n;i++)f[i]=i;
	sort(a+1,a+m+1,cmp);
	for(i=1;i<=m;i++)
	{
		if(!judge(a[i].x,a[i].y))
		{
			unionn(a[i].x,a[i].y);
			cnt++;
			ans+=a[i].z;
		}
		if(cnt==n-1)break;
	}
	if(cnt<n-1)cout<<"orz";
	else cout<<ans;
	return 0;
}
【模板】最近公共祖先(LCA)
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 1000005
LL cnt,f[N][50],d[N],head[N];
struct node
{
	LL v,next;
}edge[N];
void add(LL x,LL y)
{
	edge[++cnt]={y,head[x]};
	head[x]=cnt;
}
void dfs(LL u,LL fa)
{
	LL i;
	d[u]=d[fa]+1;
	for(i=1;i<=20;i++)
		f[u][i]=f[f[u][i-1]][i-1];
	for(i=head[u];i;i=edge[i].next)
	{
		if(edge[i].v!=fa)
		{
			f[edge[i].v][0]=u;
			dfs(edge[i].v,u);
		}
	}
}
LL LCA(LL x,LL y)
{
	LL i;
	if(d[x]<d[y])swap(x,y);
	for(i=20;i>=0;i--)
	{
		if(d[f[x][i]]>=d[y])
			x=f[x][i];
		if(x==y)return x;
	}
	for(i=20;i>=0;i--)
		if(f[x][i]!=f[y][i])
			x=f[x][i],y=f[y][i];
	return f[x][0];
}
int main()
{
	LL i,n,m,s,x,y;
	scanf("%lld%lld%lld",&n,&m,&s);
	for(i=1;i<=n-1;i++)
	{
		scanf("%lld%lld",&x,&y);
		add(x,y),add(y,x);
	}
	dfs(s,0);
	while(m--)
	{
		scanf("%lld%lld",&x,&y);
		printf("%lld\n",LCA(x,y));
	}
	return 0;
}
网络流相关
【模板】网络最大流
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define INF 11147483647000
#define N 205
#define M 5005
#define K (M+N)<<7
LL n,m,s,t,edgecnt=1,deep[K],now[K],head[K];
struct node{
	LL to,next,val;
}edge[K];
inline void add(LL x,LL y,LL c){
	edge[++edgecnt]={y,head[x],c};
	head[x]=edgecnt;
}
bool bfs(){
	LL i;
	queue<LL>q;
	for(i=1;i<=n;i++)deep[i]=INF;
	q.push(s);
	deep[s]=0;
	now[s]=head[s];
	while(!q.empty()){
		LL tp=q.front();
		q.pop();
		for(i=head[tp];i;i=edge[i].next){
			LL v=edge[i].to;
			if(edge[i].val>0&&deep[v]==INF){
				deep[v]=deep[tp]+1;
				now[v]=head[v];
				q.push(v);
				if(v==t)return 1;
			}
		}
	}
	return 0;
}
LL dfs(LL pos,LL sum){
	LL i,res=0;
	if(pos==t)return sum;
	for(i=now[pos];i&&sum;i=edge[i].next){
		LL w=edge[i].val,v=edge[i].to;
		now[pos]=i;
		if(w>0&&deep[pos]+1==deep[v]){
			LL flag=dfs(v,min(w,sum));
			if(!flag)deep[v]=INF;
			edge[i].val-=flag;
			sum-=flag;
			edge[i^1].val+=flag;
			res+=flag;
		}
	}
	return res;
}
int main(){
	LL i,x,y,z,ans=0;
	scanf("%lld%lld%lld%lld",&n,&m,&s,&t);
	for(i=1;i<=m;i++){
		scanf("%lld%lld%lld",&x,&y,&z);
		add(x,y,z);
		add(y,x,0);
	}
	while(bfs())ans+=dfs(s,INF);
	printf("%lld",ans);
	return 0;
}
ST表相关
【模板】ST 表 & RMQ 问题
C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 100005

char buf[1],*p1=buf,*p2=buf;
inline char gc(){
if(p1==p2)p2=(p1=buf)+fread(buf,1,1,stdin);return *p1++;
}
inline LL read(){
LL s=0,w=1;char ch;ch=gc();while(ch<'0'||ch>'9'){if(ch=='-')w*=-1;ch=gc();}while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+(ch^48);ch=gc();}return s*w;
}
inline void output(LL n){
if(n<0)putchar('-'),n=-n;if(n>9)output(n/10);putchar((n%10)|0x30);return ;
}

LL n,f[N][20];
inline LL query(LL x,LL y)
{
	LL k=log(y-x+1)/log(2);
	return max(f[x][k],f[y-(1<<k)+1][k]);
}
int main()
{
	LL i,j,q,x,y;
	n=read(),q=read();
	for(i=1;i<=n;i++)f[i][0]=read();
	for(j=1;1<<j<=n;j++)
		for(i=1;i+(1<<j)-1<=n;i++)
			f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1]);
	while(q--)
	{
		x=read(),y=read();
		output(query(x,y));
		putchar('\n');
	}
	return 0;
}
素数相关
【模板】线性筛素数
C++
#include<bits/stdc++.h>
using namespace std;
#define N 100000005
#define M 6000005
#define UN 100000000
#define LL long long
bool isprime[N];
LL prime[M];

char buf[1],*p1=buf,*p2=buf;
inline char gc(){
if(p1==p2)p2=(p1=buf)+fread(buf,1,1,stdin);return *p1++;
}
inline int read(){
int s=0,w=1;char ch;ch=gc();while(ch<'0'||ch>'9'){if(ch=='-')w*=-1;ch=gc();}while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+(ch^48);ch=gc();}return s*w;
}
inline void output(int n){
if(n<0)putchar('-'),n=-n;if(n>9)output(n/10);putchar((n%10)|0x30);return ;
}

void init()
{
	LL cnt=0;
	isprime[1]=1;
	for(LL i=2;i<=UN;i++)
	{
		if(!isprime[i])prime[++cnt]=i;
		for(LL j=1;j<=cnt&&i*prime[j]<=UN;j++)
		{
			isprime[i*prime[j]]=1;
		}
	}
}
int main()
{
	init();
	LL n,q,x;
	n=read(),q=read();
	while(q--)
	{
		x=read();
		output(prime[x]);
		putchar('\n');
	}
	return 0;
}
离散化相关
【模板】离散化
C++
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define N 100005
LL a[N],ans[N];
void solve(){
	LL n;
	scanf("%lld",&n);
	for(LL i=1;i<=n;i++)scanf("%lld",&a[i]),ans[i]=a[i];
	sort(a+1,a+n+1);
	LL cnt=unique(a+1,a+n+1)-(a+1);
	for(LL i=1;i<=n;i++){
		ans[i]=lower_bound(a+1,a+cnt+1,ans[i])-a;
	}
	for(LL i=1;i<=n;i++)printf("%lld ",ans[i]);
	printf("\n");
}
int main(){
	LL T;
	scanf("%lld",&T);
	while(T--){
		solve();
	}
	return 0;
}
原文