NOIP 考前模板复习--zhengjun

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
#ifdef DEBUG
template<typename T>
ostream& operator << (ostream &out,vector<T> a){
	out<<'[';
	for(T x:a)out<<x<<',';
	return out<<']';
}
template<typename T>
vector<T> ary(T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<endl;
}
template<typename T,typename...S>
void debug(T x,S...y){
	cerr<<x<<' ',debug(y...);
}
#else
#define debug(...) void()
#endif
const int N=1e5+10;
int n,a[N];
namespace SGT1{
	int t[N<<2],laz[N<<2];
	void pushup(int rt){
		t[rt]=min(t[rt<<1],t[rt<<1|1])+laz[rt];
	}
	void pushadd(int rt,int x){
		t[rt]+=x,laz[rt]+=x;
	}
	void build(int l=1,int r=n,int rt=1){
		if(l==r){
			t[rt]=a[l];return;
		}
		int mid=(l+r)>>1;
		build(l,mid,rt<<1);
		build(mid+1,r,rt<<1|1);
		pushup(rt);
	}
	void update(int L,int R,int x,int l=1,int r=n,int rt=1){
		if(L<=l&&r<=R)return pushadd(rt,x);
		int mid=(l+r)>>1;
		if(L<=mid)update(L,R,x,l,mid,rt<<1);
		if(mid<R)update(L,R,x,mid+1,r,rt<<1|1);
		pushup(rt);
	}
	void modify(int x,int y,int l=1,int r=n,int rt=1){
		if(l==r)return pushadd(rt,x);
		int mid=(l+r)>>1;
		if(x<=mid)modify(x,y,l,mid,rt<<1);
		else modify(x,y,mid+1,r,rt<<1|1);
		pushup(rt);
	}
	int find(int L,int R,int x,int l=1,int r=n,int rt=1){
		if(t[rt]>x)return 0;
		if(l==r)return l;
		x-=laz[rt];
		int mid=(l+r)>>1,s=0;
		if(L<=mid)s=find(L,R,x,l,mid,rt<<1);
		if(s)return s;
		if(mid<R)s=find(L,R,x,mid+1,r,rt<<1|1);
		return s;
	}
	int query(int L,int R,int l=1,int r=n,int rt=1){
		if(L<=l&&r<=R)return t[rt];
		int mid=(l+r)>>1;
		if(R<=mid)return query(L,R,l,mid,rt<<1)+laz[rt];
		if(mid<L)return query(L,R,mid+1,r,rt<<1|1)+laz[rt];
		return min(query(L,R,l,mid,rt<<1),query(L,R,mid+1,r,rt<<1|1))+laz[rt];
	}
}
namespace SGT2{
	ll t[N<<2],laz[N<<2];
	int siz[N<<2];
	void pushup(int rt){
		t[rt]=t[rt<<1]+t[rt<<1|1];
	}
	void pushadd(int rt,ll x){
		t[rt]+=1ll*x*siz[rt],laz[rt]+=x;
	}
	void pushdown(int rt){
		if(laz[rt]){
			pushadd(rt<<1,laz[rt]);
			pushadd(rt<<1|1,laz[rt]);
			laz[rt]=0;
		}
	}
	void build(int l=1,int r=n,int rt=1){
		if(l==r){
			siz[rt]=1,t[rt]=a[l];return;
		}
		int mid=(l+r)>>1;
		build(l,mid,rt<<1);
		build(mid+1,r,rt<<1|1);
		pushup(rt);
		siz[rt]=siz[rt<<1]+siz[rt<<1|1];
	}
	void update(int L,int R,int x,int l=1,int r=n,int rt=1){
		if(L<=l&&r<=R)return pushadd(rt,x);
		int mid=(l+r)>>1;
		pushdown(rt);
		if(L<=mid)update(L,R,x,l,mid,rt<<1);
		if(mid<R)update(L,R,x,mid+1,r,rt<<1|1);
		pushup(rt);
	}
	ll query(int L,int R,int l=1,int r=n,int rt=1){
		if(L<=l&&r<=R)return t[rt];
		int mid=(l+r)>>1;
		pushdown(rt);
		if(R<=mid)return query(L,R,l,mid,rt<<1);
		if(mid<L)return query(L,R,mid+1,r,rt<<1|1);
		return query(L,R,l,mid,rt<<1)+query(L,R,mid+1,r,rt<<1|1);
	}
}
namespace SGT3{
	const int K=1e5,INF=1e9;
	struct tree{
		int ls,rs,mn,laz;
		tree():mn(INF),laz(0),ls(0),rs(0){}
	}t[K];
	int k;
	void pushup(int rt){
		t[rt].mn=min(t[t[rt].ls].mn,t[t[rt].rs].mn)+t[rt].laz;
	}
	void pushadd(int rt,int x){
		if(rt)t[rt].mn+=x,t[rt].laz+=x;
	}
	void update(int &rt,int L,int R,int x,int l=1,int r=n){
		if(!rt)rt=++k;
		if(L<=l&&r<=R)return pushadd(rt,x);
		int mid=(l+r)>>1;
		if(L<=mid)update(t[rt].ls,L,R,l,mid);
		if(mid<R)update(t[rt].rs,L,R,mid+1,r);
		pushup(rt);
	}
	void merge(int &x,int y,int l=1,int r=n){
		if(!x||!y){
			x|=y;return;
		}
		if(l==r){
			t[x].mn+=t[y].mn;return;
		}
		int mid=(l+r)>>1;
		t[x].laz+=t[y].laz;
		merge(t[x].ls,t[y].ls,l,mid);
		merge(t[x].rs,t[y].rs,mid+1,r);
		pushup(x);
	}
	int query(int rt,int L,int R,int l=1,int r=n){
		if(!rt)return INF;
		if(L<=l&&r<=R)return t[rt].mn;
		int mid=(l+r)>>1,s=INF;
		if(L<=mid)s=min(s,query(t[rt].ls,L,R,l,mid));
		if(mid<R)s=min(s,query(t[rt].rs,L,R,mid+1,r));
		return s+t[rt].laz;
	}
}
namespace BIT{
	int c[N];
	void add(int x,int y){
		for(;x<=n;x+=x&-x)c[x]+=y;
	}
	int get(int x,int y=0){
		for(;x;x^=x&-x)y+=c[x];
		return y;
	}
}
namespace DSU1{
	int fa[N];
	void init(){
		iota(fa,fa+1+n,0);
	}
	int find(int x){
		return fa[x]==x?x:fa[x]=find(fa[x]);
	}
	bool merge(int x,int y){
		x=find(x),y=find(y);
		if(x==y)return 0;
		return fa[x]=y,1;
	}
}
namespace DSU2{
	int fa[N],siz[N];
	void init(){
		iota(fa,fa+1+n,0),fill(siz+1,siz+1+n,1);
	}
	int find(int x){
		return fa[x]==x?x:find(fa[x]);
	}
	int top,stk[N];
	bool merge(int x,int y){
		x=find(x),y=find(y);
		if(x==y)return 0;
		if(siz[x]>siz[y])swap(x,y);
		siz[y]+=siz[x],fa[x]=y,stk[++top]=x;
		return 1;
	}
	void undo(int x){
		siz[fa[x]]-=siz[x],fa[x]=x;
	}
	void reduce(int k){
		for(;top>k;top--)undo(stk[top--]);
	}
}
vector<int>to[N];
int kk,head[N];
struct edges{
	int to,nex;
}edge[N*2];
void add(int u,int v){
	edge[++kk]={v,head[u]},head[u]=kk;
	edge[++kk]={u,head[v]},head[v]=kk;
}
namespace TJ1{
	int dft,top,sct,dfn[N],low[N],scc[N],stk[N];
	void tarjan(int u){
		low[u]=dfn[u]=++dft,stk[++top]=u;
		for(int v:to[u]){
			if(!dfn[v])tarjan(v),low[u]=min(low[u],low[v]);
			else if(!scc[v])low[u]=min(low[u],dfn[v]);
		}
		if(low[u]==dfn[u]){
			sct++;
			do scc[stk[top]]=sct;while(stk[top--]^u);
		}
	}
}
namespace TJ2{
	int dft,sct,top,dfn[N],low[N],scc[N],stk[N];
	void tarjan(int u,int fa=0){
		low[u]=dfn[u]=++dft,stk[++top]=u;
		for(int i=head[u];i;i=edge[i].nex)if(i^fa^1){
			int v=edge[i].to;
			if(!dfn[v])tarjan(v,i),low[u]=min(low[u],low[v]);
			else if(!scc[v])low[u]=min(low[u],dfn[v]);
		}
		if(dfn[u]==low[u]){
			sct++;
			do scc[stk[top]]=sct;while(stk[top--]^u);
		}
	}
}
namespace TJ3{
	vector<int>A[N];
	int k,dft,top,dfn[N],low[N],stk[N];
	void add(int u,int v){
		A[u].push_back(v),A[v].push_back(u);
	}
	void tarjan(int u,int fa=0){
		dfn[u]=low[u]=++dft,stk[++top]=u;
		for(int v:to[u])if(v^fa){
			if(!dfn[v]){
				tarjan(v),low[u]=min(low[u],low[v]);
				if(low[v]>=dfn[u]){
					add(++k,u);
					do add(k,stk[top]);while(stk[top--]^v);
				}
			}else low[u]=min(low[u],dfn[v]);
		}
	}
}
namespace FHQ{
	struct tree{
		int ls,rs,rnd,val,siz,laz;
	}t[N];
	void pushup(int rt){
		t[rt].siz=t[t[rt].ls].siz+t[t[rt].rs].siz+1;
	}
	void pushadd(int rt,int x){
		if(rt)t[rt].val+=x,t[rt].laz+=x;
	}
	void pushdown(int rt){
		if(t[rt].laz){
			pushadd(t[rt].ls,t[rt].laz);
			pushadd(t[rt].rs,t[rt].laz);
		}
	}
	void split(int rt,int val,int &x,int &y){
		if(!rt){
			x=y=0;return;
		}
		pushdown(rt);
		if(val<t[rt].val)y=rt,split(t[rt].ls,val,x,t[rt].ls);
		else x=rt,split(t[rt].rs,val,t[rt].rs,y);
		pushup(rt);
	}
	int merge(int x,int y){
		if(!x||!y)return x|y;
		if(t[x].rnd<t[y].rnd){
			pushdown(x);
			t[x].rs=merge(t[x].rs,y);
			return pushup(x),x;
		}else{
			pushdown(y);
			t[y].ls=merge(x,t[y].ls);
			return pushup(y),y;
		}
	}
}
const int INF=1e9;
namespace Floyd{
	const int N=1e2+10;
	int a[N][N],f[N][N];
	void solve(){
		for(int i=1;i<=n;i++){
			copy(a[i]+1,a[i]+1+n,f[i]+1);
			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]);
				}
			}
		}
	}
}
namespace DIJ{
	vector<pair<int,int> >to[N];
	#define v e.first
	#define w e.second
	ll dis[N];
	const ll INF=1e18;
	int vis[N];
	void dij(int s){
		fill(dis+1,dis+1+n,INF),dis[s]=0;
		priority_queue<pair<ll,int> >q;
		q.push({0,s});
		for(int u;!q.empty();){
			u=q.top().second,q.pop();
			if(vis[u])continue;
			vis[u]=1;
			for(auto e:to[u]){
				if(dis[v]>dis[u]+w){
					dis[v]=dis[u]+w,q.push({-dis[v],v});
				}
			}
		}
	}
	#undef v
	#undef w
}
namespace Flow1{
	const int V=1e5,E=1e5;
	int s,t,kk,head[V],cur[V],d[V];
	struct edges{
		int to,c,nex;
	}edge[E];
	void init(int x,int y){
		s=x,t=y,kk=1;
		fill(head+s,head+1+t,0);
	}
	int add(int u,int v,int c){
		edge[++kk]={v,c,head[u]},head[u]=kk;
		edge[++kk]={u,0,head[v]},head[v]=kk;
		return kk-1;
	}
	bool bfs(){
		fill(d+s,d+1+t,-1),d[s]=0;
		copy(head+s,head+1+t,cur+s);
		queue<int>q;
		q.push(s);
		for(int u;!q.empty();){
			u=q.front(),q.pop();
			for(int i=head[u];i;i=edge[i].nex){
				int v=edge[i].to,c=edge[i].c;
				if(c&&!~d[v])d[v]=d[u]+1,q.push(v);
			}
		}
		return !~d[t];
	}
	int dfs(int u,int lim=INF){
		if(u==t)return lim;
		int flow=0;
		for(int i=cur[u];i&&flow<lim;i=edge[i].nex){
			cur[u]=i;
			int v=edge[i].to,c=edge[i].c;
			if(!c||d[v]!=d[u]+1)continue;
			int f=dfs(v,min(c,lim-flow));
			if(!f)d[v]=-1;
			flow+=f,edge[i].c+=f,edge[i^1].c-=f;
		}
		return flow;
	}
		static const int M=2;
	int dinic(){
		int sum=0;
		for(;bfs();)sum+=dfs(s);
		return sum;
	}
}
namespace Flow2{
	const int V=1e5,E=1e5;
	int s,t,kk,head[V],dis[V],cur[V];
	struct edges{
		int to,c,w,nex;
	}edge[E];
	void init(int x,int y){
		s=x,t=y,kk=1;
		fill(head+s,head+1+t,0);
	}
	int add(int u,int v,int c,int w){
		edge[++kk]={v,c,w,head[u]},head[u]=kk;
		edge[++kk]={u,0,-w,head[v]},head[v]=kk;
		return kk-1;
	}
	bool spfa(){
		fill(dis+s,dis+1+t,INF),dis[s]=0;
		queue<int>q;
		q.push(s);
		for(int u;!q.empty();){
			u=q.front(),q.pop();
			for(int i=head[u];i;i=edge[i].nex){
				int v=edge[i].to,c=edge[i].c,w=edge[i].w;
				if(c&&dis[v]>dis[u]+w){
					dis[v]=dis[u]+w,q.push(v);
				}
			}
		}
		return dis[t]<INF;
	}
	int cost,vis[N];
	int dfs(int u,int lim=INF){
		if(u==t)return lim;
		vis[u]=1;
		int flow=0;
		for(int i=cur[u];i&&flow<lim;i=edge[i].nex){
			cur[u]=i;
			int v=edge[i].to,c=edge[i].c,w=edge[i].w;
			if(vis[v]||!c||dis[v]!=dis[u]+w)continue;
			int f=dfs(v,min(lim-flow,c));
			edge[i].c-=f,edge[i^1].c+=f;
			flow+=c,cost+=c*w;
		}
		vis[u]=0;
		return flow;
	}
	int dinic(){
		int sum=0;
		for(;spfa();)sum+=dfs(s);
		return sum;
	}
}
const int mod=998244353;
namespace Matrix{
	struct matrix{
		static const int M=2;
		int a[M][M];
		matrix(){
			memset(a,0,sizeof a);
		}
		matrix operator * (const matrix &x)const{
			matrix b;
			for(int k=0;k<M;k++){
				for(int i=0;i<M;i++){
					for(int j=0;j<M;j++){
						b.a[i][j]=(b.a[i][j]+1ll*a[i][k]*x.a[k][j])%mod;
					}
				}
			}
			return b;
		}
	};
	struct vec{
		static const int M=2;
		int a[M];
		vec(){
			memset(a,0,sizeof a);
		}
		vec operator * (const matrix &x)const{
			vec b;
			for(int i=0;i<M;i++){
				for(int j=0;j<M;j++){
					b.a[j]=(b.a[j]+1ll*a[i]*x.a[i][j])%mod;
				}
			}
			return b;
		}
	};
}
namespace binom{
	int fac[N],ifac[N];
	ll qpow(ll x,ll y=mod-2,ll ans=1){
		for(;y;(x*=x)%=mod,y>>=1)if(y&1)(ans*=x)%=mod;
		return ans;
	}
	void init(int n){
		for(int i=fac[0]=1;i<=n;i++)fac[i]=1ll*fac[i-1]*i%mod;
		ifac[n]=qpow(fac[n]);
		for(int i=n;i>=1;i--)ifac[i-1]=1ll*ifac[i]*i%mod;
	}
	int C(int n,int m){
		if(0>m||m>n)return 0;
		return 1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod;
	}
}
namespace Path{
	int fa[N],siz[N],dep[N],top[N],son[N];
	void dfs1(int u){
		siz[u]=1,son[u]=0;
		for(int v:to[u])if(v^fa[u]){
			fa[v]=u,dfs1(v);
			siz[u]+=siz[v];
			if(siz[v]>siz[son[u]])son[u]=v;
		}
	}
	int dft,dfn[N],pos[N];
	void dfs2(int u,int t){
		top[u]=t,pos[dfn[u]=++dft]=u;
		if(son[u])dfs2(son[u],t);
		for(int v:to[u])if(v^fa[u]&&v^son[u])dfs2(v,v);
	}
	int LCA(int u,int v){
		for(;top[u]^top[v];u=fa[top[u]]){
			if(dep[top[u]]<dep[top[v]])swap(u,v);
		}
		return dep[u]<dep[v]?u:v;
	}
	int jump(int u,int k){
		for(;k>dfn[u]-dfn[top[u]];u=fa[top[u]]){
			k-=dfn[u]-dfn[top[u]]+1;
		}
		return pos[dfn[u]-k];
	}
}
namespace Euler_LCA{
	const int M=N*2,K=__lg(M)+2;
	int dft,dfn[N],f[K][M],dep[N];
	bool cmp(int x,int y){
		return dep[x]<dep[y];
	}
	void make(int u,int fa=0){
		f[0][dfn[u]=++dft]=u,dep[u]=dep[fa]+1;
		for(int v:to[u])if(v^fa){
			make(v,u);
			f[0][++dft]=u;
		}
	}
	void init(){
		dft=0,make(1);
		for(int i=1;(1<<i)<=dft;i++){
			for(int j=1;j+(1<<i)-1<=dft;j++){
				f[i][j]=min(f[i-1][j],f[i-1][j+(1<<i-1)],cmp);
			}
		}
	}
	int LCA(int l,int r){
		if(l=dfn[l],r=dfn[r],l>r)swap(l,r);
		int k=__lg(r-l+1);
		return min(f[k][l],f[k][r-(1<<k)+1],cmp);
	}
}
namespace AC{
	const int K=26;
	int k,ch[N][K],fail[N];
	int insert(char *a){
		int n=strlen(a),u=0;
		for(int i=0;i<n;i++){
			int &v=ch[u][a[i]-'a'];
			if(!v)v=++k;
			u=v;
		}
		return u;
	}
	void getfail(){
		queue<int>q;
		for(int i=0;i<K;i++){
			int v=ch[0][i];
			if(v)q.push(v),fail[v]=0;
		}
		for(int u;!q.empty();){
			u=q.front(),q.pop();
			for(int i=0;i<K;i++){
				int &v=ch[u][i],x=ch[fail[u]][i];
				if(v)fail[v]=x,q.push(v);
				else v=x;
			}
		}
	}
}
namespace KMP{
	char a[N];
	int nex[N];
	void kmp(){
		int n=strlen(a+1);
		for(int i=2,j=0;i<=n;i++){
			for(;j&&a[j+1]!=a[i];j=nex[j]);
			nex[i]=j+=a[j+1]==a[i];
		}
	}
}
namespace Manacher{
	int n,m;
	char a[N],b[N*2];
	int len[N*2];
	void init(){
		n=strlen(a+1),m=0,b[++m]='$';
		for(int i=1;i<=n;i++){
			b[++m]=a[i],b[++m]='$';
		}
		for(int i=1,mx=0,id=0;i<=n;i++){
			len[i]=i<=mx?min(len[id*2-i],mx-i+1):0;
			for(;i+len[i]<=m&&i-len[i]>=1&&b[i+len[i]]==b[i-len[i]];len[i]++);
			if(i+len[i]>mx)mx=i+len[i]-1,id=i;
		}
	}
}
namespace Z{
	char a[N];
	int n,z[N];
	void init(){
		n=strlen(a+1);
		fill(z+1,if(fi[rt<<1]>=fi[rt<<1|1])pushcov(rt<<1,tag[rt]);
if(fi[rt<<1|1]>=fi[rt<<1])pushcov(rt<<1|1,tag[rt]);z+1+n,0);
		for(int i=2,gap=0,mx=0;i<=n;i++){
			z[i]=i<=mx?min(z[i-gap],mx-i+1):0;
			for(;i+z[i]<=n&&a[z[i]]==a[i+z[i]-1];z[i]++);
			if(i+z[i]>mx)mx=i+z[i]-1,gap=i-1;
		}
	}
}
namespace MinCircle{
	char a[N];
	int get(){
		int n=strlen(a),i=0,j=1,k=0;
		for(;i<n&&j<n&&k<n;){
			if(a[(i+k)%n]==a[(j+k)%n])k++;
			else{
				if(a[(i+k)%n]<a[(j+k)%n])j+=k+1;
				else i+=k+1;
				if(i==j)i++;
				k=0;
			}
		}
		return min(i,j);
	}
}
namespace Hash{
	char a[N];
	int n,f[N],pw[N];
	const int base=19260817,mod=1e9+7;
	void init(){
		n=strlen(a+1);
		for(int i=pw[0]=1;i<=n;i++)pw[i]=1ll*pw[i-1]*base%mod;
		for(int i=1;i<=n;i++)f[i]=(1ll*f[i-1]*base+a[i])%mod;
	}
	int Hash(int l,int r){
		return (f[r]+1ll*(mod-f[l-1])*pw[r-l+1])%mod;
	}
}
int main(){
	freopen(".in","r",stdin);
	// freopen(".out","w",stdout);
	return 0;
}
posted @ 2023-11-17 07:30  A_zjzj  阅读(29)  评论(0编辑  收藏  举报