各种各样的模版,一个代码解决
(施工中,尚未完工)
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<cmath> using namespace std; #define ll long long #define lc(x) (x<<1) #define rc(x) (x<<1|1) #define fa(x) (x>>1) struct Math{ inline ll Max(ll a,ll b){ return a>b?a:b; } inline ll Min(ll a,ll b){ return a<b?a:b; } inline void swap(ll &a,ll &b){ a^=b^=a^=b; } }M; struct Edge{ ll u,v,w; bool operator <(const Edge a)const{return w<a.w;} }; struct Edges{ ll u[200005],v[200005],w[200005]; ll first[10005],next[200005]; ll tot=0; inline void addedge(ll a,ll b,ll c){ tot++;u[tot]=a,v[tot]=b,w[tot]=c; next[tot]=first[a]; first[a]=tot; } }; struct SegTree{ ll sum[400005],tag[400005]; inline void pushup(ll v){sum[v]=sum[lc(v)]+sum[rc(v)];} inline void pushdown(ll v,ll l,ll r){ if(tag[v]){ ll mid=l+r>>1; tag[lc(v)]+=tag[v]; tag[rc(v)]+=tag[v]; sum[lc(v)]+=tag[v]*(mid-l+1); sum[rc(v)]+=tag[v]*(r-mid); tag[v]=0; } } void update(ll v,ll l,ll r,ll left,ll right,ll add){ if(l>=left&&r<=right){ sum[v]+=add*(r-l+1);tag[v]+=add; return; } pushdown(v,l,r); ll mid=l+r>>1; if(left<=mid)update(lc(v),l,mid,left,right,add); if(mid<right)update(rc(v),mid+1,r,left,right,add); pushup(v); } ll query(ll v,ll l,ll r,ll left,ll right){ if(l>=left&&r<=right)return sum[v]; ll mid=l+r>>1,ans=0; pushdown(v,l,r); if(left<=mid)ans+=query(lc(v),l,mid,left,right); if(mid<right)ans+=query(rc(v),mid+1,r,left,right); return ans; } }; struct Lca{ Edges e; ll n; ll dep[100005],p[100005][25]; void dfs(ll x){ for(ll i=e.first[x];i;i=e.next[i]){ ll y=e.v[i]; if(!dep[y]){ dep[y]=dep[x]+1,p[y][0]=x; dfs(y); } } } void init(){ for(ll i=1;i<=n;i++)if(!dep[i])dep[i]=1,dfs(i); for(ll j=1;j<=20;j++)for(ll i=1;i<=n;i++)p[i][j]=p[p[i][j-1]][j-1]; } ll lca(ll a,ll b){ if(dep[a]<dep[b])M.swap(a,b); for(ll i=20;i>=0;i--) if(dep[p[a][i]]>=dep[b]) a=p[a][i]; if(a==b)return a; for(ll i=20;i>=0;i--) if(p[a][i]!=p[b][i]) a=p[a][i],b=p[b][i]; return p[a][0]; } }; struct Tarjan{ Edges e; ll dfn[100005],low[100005]; ll stack[100005],rd[100005]; bool vis[100005]; ll top=0,cnt=0,dfp=0; void tarjan(ll x){ dfn[x]=low[x]=++dfp; stack[++top]=x;vis[x]=1; for(ll i=e.first[x];i;i=e.next[i]){ ll y=e.v[i]; if(!dfn[y]){ tarjan(y); low[x]=M.Min(low[x],low[y]); }else low[x]=M.Min(low[x],dfn[y]); } if(dfn[x]==low[x]){ ++cnt; while(stack[top+1]!=x){ rd[stack[top]]=cnt; vis[stack[top]]=0; top--; } } } }; struct Heap{ ll heap[100005],size; void pushup(ll x){ while(x>1){ if(heap[x]>=heap[fa(x)])M.swap(heap[x],heap[fa(x)]),x=fa(x); else return; } } void pushdown(ll x){ while(lc(x)<=size){ ll k=-1; if(heap[x]<heap[lc(x)])k=lc(x); if(rc(x)<=size&&heap[x]<heap[rc(x)])k=rc(x); if(k==-1)return; M.swap(heap[x],heap[k]),x=k; } } inline ll top(){return heap[1];} inline void pop(){heap[1]=heap[size--];pushdown(1);} inline void push(ll x){heap[++size]=x;pushup(size);} }; struct DijkstraHeap{ struct point{ ll id,d; bool operator <(const point &a)const{return d>a.d;} }; Edges e; priority_queue<point>q; ll dis[100005]; ll dijkstra(ll s,ll t){ memset(dis,0x3f,sizeof dis); dis[s]=0;q.push((point){s,0}); while(!q.empty()){ point g=q.top(); ll x=g.id,d=g.d; if(d!=dis[x])continue; for(ll i=e.first[x];i;i=e.next[i]){ ll y=e.v[i]; if(dis[x]+e.w[i]<dis[y]){ dis[y]=dis[x]+e.w[i]; q.push((point){y,dis[y]}); } } } return dis[t]; } }; struct Floyd{ ll map[205][205]; ll dis[205][205]; ll n; void floyd(){ for(ll i=1;i<=n;i++) for(ll j=1;j<=n;j++) dis[i][j]=map[i][j]; for(ll k=1;k<=n;k++) for(ll i=1;i<=n;i++) for(ll j=1;j<=n;j++) dis[i][j]=M.Min(dis[i][j],dis[i][k]+dis[k][j]); } }; struct DisjointSet{ ll f[100005]; ll n; void init(){ for(ll i=1;i<=n;i++)f[i]=i; } ll gf(ll x){ if(x!=f[x])f[x]=gf(f[x]); return f[x]; } inline void merge(ll a,ll b){ ll fa=gf(a); ll fb=gf(b); if(fa!=fb)f[fb]=fa; } }; struct Kruskal{ DisjointSet G; ll n,m; Edge e[100005]; Edges ans; void kruskal(){ G.n=n;G.init(); sort(e+1,e+m+1); for(ll i=1;i<=m;i++){ if(G.gf(e[i].u)!=G.gf(e[i].v)){ G.merge(e[i].u,e[i].v); ans.addedge(e[i].u,e[i].v,e[i].w); ans.addedge(e[i].v,e[i].u,e[i].w); } } } }; struct Prim{ struct point{ ll id,d; bool operator <(const point &a)const{return d>a.d;} }; Edges e; Edges ans; priority_queue<point>q; ll dis[100005]; void prim(){ memset(dis,0x3f,sizeof dis); dis[1]=0;q.push((point){1,0}); while(!q.empty()){ point g=q.top(); ll x=g.id,d=g.d; if(d!=dis[x])continue; for(ll i=e.first[x];i;i=e.next[i]){ ll y=e.v[i]; if(dis[x]+e.w[i]<dis[y]){ dis[y]=dis[x]+e.w[i]; ans.addedge(x,y,e.w[i]); ans.addedge(y,x,e.w[i]); q.push((point){y,dis[y]}); } } } } }; struct Kmp{ char p[100005],t[100005]; ll next[100005]; ll ans[100005],cnt=0; void kmp(){ ll n=strlen(p+1),m=strlen(t+1); ll j=0; for(ll i=2;i<=m;i++){ while(j&&t[j+1]==t[j])j=next[j]; if(t[j+1]==t[i])j++; next[i]=j; }j=0; for(ll i=1;i<=n;i++){ while(j&&t[j+1]==p[i])j=next[j]; if(t[j+1]==p[i])j++; if(j==m)ans[++cnt]=i-j+1; } } }; struct ST{ #define lg2(x) (log(x)/log(2)) ll a[100005],f[100005][25]; ll n; void init(){ for(ll i=1;i<=n;i++)f[i][0]=a[i]; for(ll j=1;j<=20;j++) for(ll i=1;i<=n;i++) f[i][j]=M.Max(f[i][j-1],f[i+(1<<(j-1))][j-1]); } ll RMQ(ll l,ll r){ ll k=lg2(r-l+1); return M.Max(f[l][k],f[r-(1<<k)+1][k]); } }; struct Bag01{ ll weight[100005],value[100005]; ll dp[100005]; ll n,m; ll bag(){ for(ll i=1;i<=n;i++) for(ll j=m;j>=weight[i];j--) dp[j]=M.Max(dp[j],dp[j-weight[i]]+value[i]); return dp[m]; } }; struct BagComplete{ ll weight[100005],value[100005]; ll dp[100005]; ll n,m; ll bag(){ for(ll i=1;i<=n;i++) for(ll j=weight[i];j<=m;j++) dp[j]=M.Max(dp[j],dp[j-weight[i]]+value[i]); return dp[m]; } }; struct BagMultiple{ ll weight[100005],value[100005],sum[100005]; ll w[100005],v[100005],cnt=0; ll dp[100005]; ll n,m; ll bag(){ for(ll i=1;i<=n;i++){ ll y=1; while(sum[i]){ ++cnt; w[cnt]=y*weight[i],v[cnt]=y*value[i]; y<<=1; if(y<=sum[i]){ sum[i]-=y; }else{ y=sum[i];sum[i]=0; } } } for(ll i=1;i<=cnt;i++) for(ll j=m;j>=w[i];j--) dp[j]=M.Max(dp[j],dp[j-w[i]]+v[i]); return dp[m]; } }; int main(){ return 0; }