2023“钉耙编程”中国大学生算法设计超级联赛(3)
题解:
https://files.cnblogs.com/files/clrs97/2023HDU%E7%AC%AC%E4%B8%89%E5%9C%BA%E9%A2%98%E8%A7%A3.pdf
Code:
A. Magma Cave
#include<iostream> #include<algorithm> using namespace std; const int N=50005,M=200005,K=N+M; int Case,n,q,i,ok[M],e[M][4],at[M],bit[M]; int f[K],son[K][2],val[K],mx[K],tmp[K];bool rev[K]; inline bool isroot(int x){return !f[x]||son[f[x]][0]!=x&&son[f[x]][1]!=x;} inline void rev1(int x){if(!x)return;swap(son[x][0],son[x][1]);rev[x]^=1;} inline void pb(int x){if(rev[x])rev1(son[x][0]),rev1(son[x][1]),rev[x]=0;} inline void umax(int&a,int b){a<b?(a=b):0;} inline void up(int x){ mx[x]=val[x]; if(son[x][0])umax(mx[x],mx[son[x][0]]); if(son[x][1])umax(mx[x],mx[son[x][1]]); } inline void rotate(int x){ int y=f[x],w=son[y][1]==x; son[y][w]=son[x][w^1]; if(son[x][w^1])f[son[x][w^1]]=y; if(f[y]){ int z=f[y]; if(son[z][0]==y)son[z][0]=x;else if(son[z][1]==y)son[z][1]=x; } f[x]=f[y];f[y]=x;son[x][w^1]=y;up(y); } inline void splay(int x){ int s=1,i=x,y;tmp[1]=i; while(!isroot(i))tmp[++s]=i=f[i]; while(s)pb(tmp[s--]); while(!isroot(x)){ y=f[x]; if(!isroot(y)){if((son[f[y]][0]==y)^(son[y][0]==x))rotate(x);else rotate(y);} rotate(x); } up(x); } inline void access(int x){for(int y=0;x;y=x,x=f[x])splay(x),son[x][1]=y,up(x);} inline int root(int x){access(x);splay(x);while(son[x][0])x=son[x][0];splay(x);return x;} inline void makeroot(int x){access(x);splay(x);rev1(x);} inline void link(int x,int y){makeroot(x);f[x]=y;access(x);} inline void cutf(int x){access(x);splay(x);f[son[x][0]]=0;son[x][0]=0;up(x);} inline void cut(int x,int y){makeroot(x);cutf(y);} inline int ask(int x,int y){makeroot(x);access(y);splay(y);return mx[y];} inline void modify(int x,int p){for(;x<=q;x+=x&-x)bit[x]+=p;} inline int getsum(int x){int t=0;for(;x;x-=x&-x)t+=bit[x];return t;} inline void solve(){ for(i=1;i<=q;i++)bit[i]=at[i]=0; for(i=1;i<=n+q;i++){ f[i]=0; son[i][0]=son[i][1]=0; val[i]=mx[i]=0; rev[i]=0; } for(i=1;i<=q;i++)if(e[i][0]==1){ at[e[i][3]]=i; val[n+i]=mx[n+i]=e[i][3]; } int cnt=n-1; for(i=1;i<=q;i++)if(e[i][0]==1){ int x=e[i][1],y=e[i][2],z=e[i][3]; if(root(x)==root(y)){ int o=ask(x,y); if(z>o)continue; o=at[o]; cut(n+o,e[o][1]); cut(n+o,e[o][2]); modify(e[o][3],-1); }else cnt--; link(n+i,x); link(n+i,y); modify(z,1); }else if(!cnt&&at[e[i][2]]&&at[e[i][2]]<=i&&getsum(e[i][2])>=e[i][1])ok[i]++; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>Case; while(Case--){ cin>>n>>q; for(i=1;i<=q;i++){ cin>>e[i][0]>>e[i][1]>>e[i][2]; if(e[i][0]==1)cin>>e[i][3];else ok[i]=0; } solve(); for(i=1;i<=q;i++)if(e[i][0]==1)e[i][3]=q-e[i][3]+1; else{ e[i][1]=n-e[i][1]; e[i][2]=q-e[i][2]+1; } solve(); for(i=1;i<=q;i++)if(e[i][0]==2)cout<<(ok[i]==2?"YES":"NO")<<endl; } }
B. King’s Ruins
#include<iostream> #include<algorithm> using namespace std; typedef unsigned long long ull; const int N=50005,BATCH=23,D=5; int Case,n,m,i,j,k,o,st[N],en[N],x[N][D],w[N],f[N],now,mx[BATCH*4][65537]; ull who[D][N][BATCH]; inline void up(int&a,int b){a<b?(a=b):0;} inline void flip(ull*f,int x){f[x>>6]^=1ULL<<(x&63);} inline bool check(int*a,int*b){ for(int i=0;i<D;i++)if(a[i]>b[i])return 0; return 1; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>Case; while(Case--){ cin>>n; for(i=0;i<n;i++){ for(j=0;j<D;j++)cin>>x[i][j]; cin>>w[i]; f[i]=0; } for(i=0;i<n;i++)en[i>>6]=i; for(i=n-1;~i;i--)st[i>>6]=i; m=(n-1)>>6; int L,R; for(L=0;L<=m;L=R+1){ R=min(L+BATCH-1,m); for(i=0;i<BATCH*4;i++)for(j=0;j<65536;j++)mx[i][j]=0; for(i=1;i<=n;i++)for(j=0;j<BATCH;j++){ who[0][i][j]=0; who[1][i][j]=0; who[2][i][j]=0; who[3][i][j]=0; who[4][i][j]=0; } int ST=st[L],EN=en[R]; for(i=ST;i<=EN;i++){ o=i-ST; for(j=0;j<D;j++)flip(who[j][x[i][j]],o); } for(i=2;i<=n;i++)for(j=0;j<BATCH;j++){ who[0][i][j]|=who[0][i-1][j]; who[1][i][j]|=who[1][i-1][j]; who[2][i][j]|=who[2][i-1][j]; who[3][i][j]|=who[3][i-1][j]; who[4][i][j]|=who[4][i-1][j]; } for(i=L;i<=m;i++){ int _ST=st[i],_EN=en[i]; if(i>L){ int lim=min(BATCH,i-L); for(j=_ST;j<=_EN;j++){ now=f[j]; int A=x[j][0],B=x[j][1],C=x[j][2],D=x[j][3],E=x[j][4]; for(k=0;k<lim;k++){ ull S=who[0][A][k]&who[1][B][k]&who[2][C][k]&who[3][D][k]&who[4][E][k]; up(now,mx[k<<2][S&65535]); up(now,mx[(k<<2)+1][(S>>16)&65535]); up(now,mx[(k<<2)+2][(S>>32)&65535]); up(now,mx[(k<<2)+3][S>>48]); } f[j]=now; } } if(i<=R){ for(j=_ST;j<=_EN;j++){ now=f[j]; for(k=_ST;k<j;k++)if(now<f[k]&&check(x[k],x[j]))now=f[k]; now+=w[j]; f[j]=now; o=j-ST; mx[o>>4][1<<(o&15)]=now; } o=(i-L)<<2; for(j=1;j<65536;j++){ mx[o][j]=max(mx[o][j&-j],mx[o][j-(j&-j)]); mx[o+1][j]=max(mx[o+1][j&-j],mx[o+1][j-(j&-j)]); mx[o+2][j]=max(mx[o+2][j&-j],mx[o+2][j-(j&-j)]); mx[o+3][j]=max(mx[o+3][j&-j],mx[o+3][j-(j&-j)]); } } } } for(i=0;i<n;i++)cout<<f[i]<<endl; } }
C. Leshphon
#include<cstdio> typedef unsigned long long ull; const int N=55,M=N*N,K=5; int Case,n,m,k,i,j,x,y,id[N][N],u[M],v[M],keep[M],mark[M],preg[N],preh[N],pool[K][N*2]; ull ans,g[N],h[N],full,C[M][K]; inline void flip(int o){ int x=u[o],y=v[o]; g[x]^=1ULL<<y; h[y]^=1ULL<<x; } inline bool bfs(ull*g,int*pre){ ull mask=full^1; int h=1,t=1; static int q[N]; q[1]=0; while(h<=t){ int x=q[h++]; for(ull S=mask&g[x];S;S-=S&-S){ mask^=S&-S; int y=__builtin_ctzll(S&-S); pre[y]=x; q[++t]=y; } } return t==n; } inline void ext(int x,int*pool,int&cp){ if(mark[x])return; mark[x]=1; pool[cp++]=x; } void solve(int k,int m){ if(!bfs(g,preg)||!bfs(h,preh)){ ans+=C[m][k]; return; } if(!k)return; int i,cp=0; for(i=1;i<n;i++){ ext(id[preg[i]][i],pool[k],cp); ext(id[i][preh[i]],pool[k],cp); } for(i=0;i<cp;i++)mark[pool[k][i]]=0; for(i=0;i<cp;i++){ int o=pool[k][i]; if(keep[o])continue; m--; flip(o); solve(k-1,m); flip(o); keep[o]=k; } for(i=0;i<cp;i++)if(keep[pool[k][i]]==k)keep[pool[k][i]]=0; } int main(){ for(C[0][0]=i=1;i<M;i++)for(C[i][0]=j=1;j<K;j++)C[i][j]=C[i-1][j-1]+C[i-1][j]; scanf("%d",&Case); while(Case--){ scanf("%d%d",&n,&m); k=3; for(i=0;i<n;i++)g[i]=h[i]=0; for(i=0;i<m;i++){ scanf("%d%d",&x,&y); x--,y--; id[x][y]=i; u[i]=x,v[i]=y; flip(i); } full=(1ULL<<n)-1; ans=0; solve(k,m); printf("%llu\n",ans); } }
D. Chaos Begin
#include<iostream> #include<algorithm> #include<map> #include<set> using namespace std; typedef long long ll; typedef pair<int,int>PI; const int N=50005*2; int Case,n,m,i,t,DX,DY;set<PI>ans,done; struct P{ int x,y; P(){} P(int _x,int _y){x=_x,y=_y;} P operator-(const P&b)const{return P(x-b.x,y-b.y);} }a[N],b[N],q[N],pool[N]; inline ll cross(const P&a,const P&b){return 1LL*a.x*b.y-1LL*a.y*b.x;} inline bool cmp1(const P&a,const P&b){return a.x==b.x?a.y<b.y:a.x<b.x;} inline bool cmp2(const P&a,const P&b){return a.x==b.x?a.y>b.y:a.x<b.x;} inline bool cmpd(const P&a,const P&b){ if(a.x==b.x)return DY>=0?a.y<b.y:a.y>b.y; return DX>=0?a.x<b.x:a.x>b.x; } inline void check(int dx,int dy){ if(done.find(PI(dx,dy))!=done.end())return; done.insert(PI(dx,dy)); done.insert(PI(-dx,-dy)); map<PI,int>T; int i,cnt=0; for(i=1;i<=m;i++)T[PI(b[i].x,b[i].y)]++; DX=dx,DY=dy; sort(b+1,b+m+1,cmpd); for(i=1;i<=m;i++){ int&now=T[PI(b[i].x,b[i].y)]; if(!now)continue; now--; cnt++; int&nxt=T[PI(b[i].x+dx,b[i].y+dy)]; if(!nxt)return; nxt--; } if(cnt==n){ ans.insert(PI(dx,dy)); ans.insert(PI(-dx,-dy)); } } inline void findline(const P&A,const P&B){ int i,j,cp=0; for(i=1;i<=m;i++)if(!cross(B,a[i]-A))pool[++cp]=a[i]; for(i=1;i<=cp;i++)for(j=1;j<=cp;j++)check(pool[i].x-pool[j].x,pool[i].y-pool[j].y); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); cin>>Case; while(Case--){ cin>>n; m=n*2; for(i=1;i<=m;i++)cin>>a[i].x>>a[i].y,b[i]=a[i]; ans.clear(); done.clear(); check(0,0); sort(a+1,a+m+1,cmp1); findline(a[1],P(0,1)); findline(a[m],P(0,1)); for(i=1,t=0;i<=m;i++){ if(i>1&&a[i].x==a[i-1].x)continue; while(t>1&&1LL*(q[t].y-q[t-1].y)*(a[i].x-q[t].x)>=1LL*(a[i].y-q[t].y)*(q[t].x-q[t-1].x))t--; q[++t]=a[i]; } for(i=1;i<t;i++)findline(q[i],q[i+1]-q[i]); sort(a+1,a+m+1,cmp2); for(i=1,t=0;i<=m;i++){ if(i>1&&a[i].x==a[i-1].x)continue; while(t>1&&1LL*(q[t].y-q[t-1].y)*(a[i].x-q[t].x)<=1LL*(a[i].y-q[t].y)*(q[t].x-q[t-1].x))t--; q[++t]=a[i]; } for(i=1;i<t;i++)findline(q[i],q[i+1]-q[i]); cout<<ans.size()<<endl; for(set<PI>::iterator it=ans.begin();it!=ans.end();it++)cout<<it->first<<" "<<it->second<<endl; } }
E. Out of Control
#include<cstdio> #include<algorithm> using namespace std; const int N=3005,P=1000000007; int Case,n,m,i,j,sum,ans,a[N],b[N],f[N]; inline void up(int&a,int b){a=a+b<P?a+b:a+b-P;} int main(){ scanf("%d",&Case); while(Case--){ scanf("%d",&n); for(i=1;i<=n;i++)scanf("%d",&a[i]),b[i]=a[i]; sort(b+1,b+n+1); for(m=0,i=1;i<=n;i++)if(i==1||b[i]>b[i-1])b[++m]=b[i]; for(i=1;i<=n;i++)a[i]=lower_bound(b+1,b+m+1,a[i])-b; sort(a+1,a+n+1); for(i=1;i<=m;i++)f[i]=1; printf("%d\n",m); for(i=2;i<=n;i++){ sum=ans=0; for(j=a[i-1];j<a[i];j++)up(sum,f[j]); for(j=a[i];j<=m;j++){ up(sum,f[j]); f[j]=sum; up(ans,sum); } printf("%d\n",ans); } } }
F. Dragon Seal
#include<cstdio> #include<vector> #include<algorithm> using namespace std; typedef unsigned long long ull; const int N=65; int Case,n,i,j,x,y,g[N],v[N<<1],nxt[N<<1],ed,ans; ull num[N][2];int val[N][2],f[N][2][2]; struct Item{ ull num;int val,col; Item(){} Item(ull _num,int _val,int _col){num=_num,val=_val,col=_col;} }; namespace Matroid{ const int M=N*2,E=100005,inf=~0U>>1,K=64; int n,tot,ans,S,T,g[M],v[E],nxt[E],ed,q[E],h,t,d[M],pre[M],w[M],cnt[N]; bool in[M],use[M]; Item item[M]; ull base[K]; inline bool check1(){ for(int i=0;i<tot;i++)if(cnt[i]>1)return 0; return 1; } inline bool check2(){ int i,j; for(i=0;i<K;i++)base[i]=0; for(i=0;i<n;i++)if(use[i]){ ull x=item[i].num; bool flag=0; for(j=0;j<K;j++)if(x>>j&1){ if(!base[j]){ base[j]=x; flag=1; break; }else x^=base[j]; } if(!flag)return 0; } return 1; } inline void add(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;} inline void ext(int x,int y,int z){ if(d[x]>=y)return; d[x]=y; pre[x]=z; if(in[x])return; q[++t]=x; in[x]=1; } inline bool find(){ int i,j,S=n+1,T=n+2; for(ed=i=0;i<=T;i++)g[i]=w[i]=0; for(i=0;i<n;i++)if(!use[i]){ w[i]=item[i].val; use[i]^=1; cnt[item[i].col]++; if(check1())add(S,i); if(check2())add(i,T); cnt[item[i].col]--; use[i]^=1; }else w[i]=-item[i].val; for(i=0;i<n;i++)if(use[i])for(j=0;j<n;j++)if(!use[j]){ use[i]^=1,use[j]^=1; cnt[item[i].col]--;cnt[item[j].col]++; if(check1())add(i,j); if(check2())add(j,i); cnt[item[i].col]++;cnt[item[j].col]--; use[i]^=1,use[j]^=1; } for(i=0;i<=T;i++)d[i]=-inf,in[i]=0; q[h=t=1]=S; d[S]=0,in[S]=1; while(h<=t){ int x=q[h++]; for(i=g[x];i;i=nxt[i])ext(v[i],d[x]+w[v[i]],x); in[x]=0; } if(d[T]==-inf)return 0; ans+=d[T]; while(pre[T]!=S){ T=pre[T]; if(use[T])cnt[item[T].col]--;else cnt[item[T].col]++; use[T]^=1; } return 1; } inline int intersection(const vector<Item>&pool,int _tot){ int i; n=ans=0; tot=_tot; for(i=0;i<pool.size();i++)if(pool[i].val>=0)item[n++]=pool[i]; for(i=0;i<tot;i++)cnt[i]=0; for(i=0;i<n;i++)use[i]=0; for(i=0;i<tot;i++)if(!find())return -1; return ans; } } inline void add(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;} void dfs(int x,int y){ for(int i=g[x];i;i=nxt[i]){ int u=v[i]; if(u==y)continue; dfs(u,x); } for(int now=0;now<2;now++){ int tot=0; vector<Item>pool; for(int i=g[x];i;i=nxt[i]){ int u=v[i]; if(u==y)continue; for(int j=0;j<2;j++)pool.push_back(Item(num[u][j],f[u][j][now],tot)); tot++; } pool.push_back(Item(num[x][now],val[x][now],tot)); tot++; if(y){ for(int fa=0;fa<2;fa++){ vector<Item>npool=pool; npool.push_back(Item(num[y][fa],0,tot)); f[x][now][fa]=Matroid::intersection(npool,tot+1); } }else{ ans=max(ans,Matroid::intersection(pool,tot)); } } } int main(){ scanf("%d",&Case); while(Case--){ scanf("%d",&n); ans=-1; for(ed=i=0;i<=n;i++)g[i]=0; for(i=1;i<=n;i++)for(j=0;j<2;j++)scanf("%llu%d",&num[i][j],&val[i][j]); for(i=1;i<n;i++)scanf("%d%d",&x,&y),add(x,y),add(y,x); dfs(1,0); printf("%d\n",ans); } }
G. Casino Royale
#include<iostream> #include<map> #include<vector> #include<algorithm> using namespace std; typedef vector<int>V; typedef pair<int,V>P; typedef long long ll; const int N=55; map<P,int>pre,nxt; int Case,n,m,q,i,j,k,l,r,diff,x,cur,cnt[N],tmp[N]; vector<vector<int> >g[N]; vector<ll>f[N]; ll ans[N*2][N*2]; char s[N]; struct E{ int a,b; void init(int sum,int diff){ a=(sum+diff)/2; b=(sum-diff)/2; } }; vector<E>e[N]; void prework(int n){ V v; pre[P(0,v)]=1; cnt[0]=1; for(i=1;i<=n;i++){ g[i-1].resize(cnt[i-1]); for(j=0;j<cnt[i-1];j++)g[i-1][j].resize(2); nxt.clear(); cur=0; for(map<P,int>::iterator it=pre.begin();it!=pre.end();it++){ for(j=1;j<=2;j++){ m=it->first.second.size(); for(k=0;k<m;k++)tmp[k]=it->first.second[k]; tmp[m++]=j; while(m>=3&&tmp[m-3]<=tmp[m-2]&&tmp[m-2]>=tmp[m-1]){ tmp[m-3]+=tmp[m-1]-tmp[m-2]; m-=2; } V now(m); for(k=0;k<m;k++)now[k]=tmp[k]; int&to=nxt[P(it->first.first+j,now)]; if(!to)to=++cur; g[i-1][it->second-1][j-1]=to-1; } } swap(pre,nxt); cnt[i]=cur; e[i].resize(cur); for(map<P,int>::iterator it=pre.begin();it!=pre.end();it++){ m=it->first.second.size(); for(k=0;k<m;k++)tmp[k]=it->first.second[k]; j=0,k=m-1,x=1,diff=0; while(j<=k){ if(tmp[j]>tmp[k])diff+=x*tmp[j++]; else diff+=x*tmp[k--]; x*=-1; } e[i][it->second-1].init(it->first.first,diff); } } for(i=0;i<=n;i++)f[i].resize(cnt[i]); } int main(){ prework(50); ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>Case; while(Case--){ cin>>n>>q>>s; for(i=0;i<=n;i++)for(j=0;j<cnt[i];j++)f[i][j]=0; f[0][0]=1; for(i=0;i<n;i++){ l=0,r=1; if(s[i]=='1')r=0; if(s[i]=='2')l=1; for(j=0;j<cnt[i];j++)if(f[i][j])for(k=l;k<=r;k++)f[i+1][g[i][j][k]]+=f[i][j]; } for(i=0;i<=n*2;i++)for(j=0;j<=n*2;j++)ans[i][j]=0; for(i=0;i<cnt[n];i++)ans[e[n][i].a][e[n][i].b]+=f[n][i]; while(q--)cin>>i>>j,cout<<ans[i][j]<<endl; } }
H. Teyberrs
#include<iostream> #include<algorithm> using namespace std; typedef long long ll; const int N=200005,M=524305,Q=200005; int Case,n,q,start,i,j,x,y,g[N],w[Q],nxt[Q];ll ans[Q]; int a[N],d[N],l[N],r[N],pool[N]; int L,R;ll base;bool no; int cnt[M];ll sum[M]; void build(int x,int a,int b){ cnt[x]=sum[x]=0; if(a==b)return; int mid=(a+b)>>1; build(x<<1,a,mid),build(x<<1|1,mid+1,b); } inline void ins(int o){ int c=lower_bound(pool+1,pool+n+1,o)-pool; int x=1,a=1,b=n,mid; while(1){ cnt[x]++; sum[x]+=o; if(a==b)return; mid=(a+b)>>1; x<<=1; if(c<=mid)b=mid;else a=mid+1,x++; } } inline int delmin(){ int x=1,a=1,b=n,mid; while(a<b){ mid=(a+b)>>1; x<<=1; if(cnt[x])b=mid;else a=mid+1,x++; } int o=pool[a]; for(;x;x>>=1)cnt[x]--,sum[x]-=o; return o; } inline void delmax(){ int x=1,a=1,b=n,mid; while(a<b){ mid=(a+b)>>1; x=x<<1|1; if(cnt[x])a=mid+1;else b=mid,x--; } int o=pool[a]; for(;x;x>>=1)cnt[x]--,sum[x]-=o; } inline void adjust(int l,int r){ while(L<R){ if(l<=L&&L<=r)break; base+=delmin(); L+=2; } while(L<R){ if(l<=R&&R<=r)break; delmax(); R-=2; } if(L==R&&L<l||L>r)no=1; } inline ll getsum(int k){ int x=1,a=1,b=n,mid,t;ll ret=0; while(a<b){ mid=(a+b)>>1; x<<=1; t=cnt[x]; if(k<=t)b=mid; else{ k-=t; ret+=sum[x]; a=mid+1; x++; } } return ret+1LL*k*pool[a]; } inline ll ask(int x){ if(no||x<L||x>R||((x-L)&1))return -1; if(x==L)return base; return base+getsum((x-L)>>1); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>Case; while(Case--){ cin>>n>>q>>start; for(i=1;i<=n;i++){ cin>>x>>y>>l[i]>>r[i]; a[i]=x,d[i]=pool[i]=y-x; } for(i=1;i<=n;i++)g[i]=0; for(i=1;i<=q;i++){ cin>>x>>y; w[i]=y; nxt[i]=g[x]; g[x]=i; } sort(pool+1,pool+n+1); build(1,1,n); L=R=start; base=0; no=0; for(i=1;i<=n;i++){ if(!no){ base+=a[i]; ins(d[i]); L--,R++; adjust(l[i],r[i]); } for(j=g[i];j;j=nxt[j])ans[j]=ask(w[j]); } for(i=1;i<=q;i++)cout<<ans[i]<<endl; } }
I. Operation Hope
#include<iostream> #include<algorithm> using namespace std; const int N=100005,K=3,inf=~0U>>1; int Case,n,i,j,mi,ma,l,r,mid,ans; int cnt,t,q[N*2],f[N*2];bool vis[N*2]; int e[N*2][K]; struct DS{ int q[N*2],h,t,d; int get(int val){ if(h<=t){ int x=q[h]; if(e[x][d]<val-mid){ h++; return x; } } if(h<=t){ int x=q[t]; if(e[x][d]>val+mid){ t--; return x; } } return 0; } void reset(){h=1,t=n+n;} }T[K]; inline bool cmp(int x,int y){return e[x][j]<e[y][j];} void dfs0(int x){ if(vis[x])return; vis[x]=1; for(int d=0;d<K;d++)while(1){ int y=T[d].get(e[x][d]); if(!y)break; dfs0(y<=n?y+n:y-n); } q[++t]=x; } void dfs1(int x){ if(!vis[x])return; vis[x]=0,f[x]=cnt; for(int d=0;d<K;d++)while(1){ int y=T[d].get(e[x<=n?x+n:x-n][d]); if(!y)break; dfs1(y); } } inline bool check(){ for(t=cnt=0,i=1;i<=n+n;i++)vis[i]=0; for(i=0;i<K;i++)T[i].reset(); for(i=1;i<=n+n;i++)if(!vis[i])dfs0(i); for(i=0;i<K;i++)T[i].reset(); for(i=t;i;i--)if(vis[q[i]])cnt++,dfs1(q[i]); for(i=1;i<=n;i++)if(f[i]==f[i+n])return 0; return 1; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); cin>>Case; while(Case--){ cin>>n; for(i=1;i<=n;i++){ for(j=0;j<K;j++)cin>>e[i][j]; for(j=0;j<K;j++)cin>>e[i+n][j]; } mi=inf,ma=-inf; for(i=1;i<=n+n;i++)for(j=0;j<K;j++){ mi=min(mi,e[i][j]); ma=max(ma,e[i][j]); } for(j=0;j<K;j++){ for(i=1;i<=n+n;i++)T[j].q[i]=i; T[j].h=1,T[j].t=n+n,T[j].d=j; sort(T[j].q+1,T[j].q+n+n+1,cmp); } l=0,r=ma-mi; while(l<=r){ mid=(l+r)>>1; if(check())r=(ans=mid)-1;else l=mid+1; } cout<<ans<<endl; } }
J. The Mine of Abyss
#include<iostream> #include<algorithm> #include<vector> using namespace std; typedef long long ll; typedef pair<ll,ll>P; typedef vector<P>V; const int N=50005,M=131075; int Case,n,m,i,op,x,y,pos[N];V ans,v[M]; inline void ext(V&nxt,P&now,const P&o){ if(o.first>now.second+1){ if(now.second>=0)nxt.push_back(now); now=o; }else{ now.second=max(now.second,o.second); } } inline V push(V v,const P&o){ for(int i=0;i<v.size();i++){ v[i].first+=o.first; v[i].second+=o.second; } return v; } inline V combine(const V&a,const V&b){ V nxt; int j=0,k=0; P now(0,-2); while(j<a.size()&&k<b.size())ext(nxt,now,a[j].first<b[k].first?a[j++]:b[k++]); while(j<a.size())ext(nxt,now,a[j++]); while(k<b.size())ext(nxt,now,b[k++]); nxt.push_back(now); return nxt; } inline V merge(const V&a,const V&b){ if(!a.size())return b; if(!b.size())return a; V c=a; for(int i=0;i<b.size();i++)c=combine(c,push(a,b[i])); return c; } inline void init(int x){ v[x].resize(2); v[x][0]=P(0,0); cin>>v[x][1].first>>v[x][1].second; } void build(int x,int a,int b){ if(a==b){ pos[a]=x; init(x); return; } int mid=(a+b)>>1; build(x<<1,a,mid); build(x<<1|1,mid+1,b); v[x]=merge(v[x<<1],v[x<<1|1]); } inline void change(int x){ x=pos[x]; init(x); for(x>>=1;x;x>>=1)v[x]=merge(v[x<<1],v[x<<1|1]); } void ask(int x,int a,int b,int c,int d){ if(c<=a&&b<=d){ ans=merge(ans,v[x]); return; } int mid=(a+b)>>1; if(c<=mid)ask(x<<1,a,mid,c,d); if(d>mid)ask(x<<1|1,mid+1,b,c,d); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>Case; while(Case--){ cin>>n>>m; build(1,1,n); while(m--){ cin>>op>>x; if(op==1)change(x); else{ cin>>y; ans.clear(); ask(1,1,n,x,y); ll fin=0; for(i=0;i<ans.size();i++)fin+=ans[i].second-ans[i].first+1; cout<<fin<<endl; } } } }
K. 8-bit Zoom
#include<cstdio> const int N=55; int Case,n,rate;char a[N][N],b[N*8][N*8]; bool check(){ rate/=25; if(n*rate%4)return 0; int m=n*rate; for(int i=0;i<n;i++)for(int j=0;j<n;j++){ int xl=i*rate,xr=xl+rate; int yl=j*rate,yr=yl+rate; char w=a[i][j]; for(int A=xl;A<xr;A++)for(int B=yl;B<yr;B++)b[A][B]=w; } for(int i=0;i<m;i+=4)for(int j=0;j<m;j+=4){ char w=b[i][j]; for(int A=i;A<i+4;A++)for(int B=j;B<j+4;B++)if(b[A][B]!=w)return 0; } for(int i=0;i<m;i+=4){ for(int j=0;j<m;j+=4)putchar(b[i][j]); puts(""); } return 1; } int main(){ scanf("%d",&Case); while(Case--){ scanf("%d%d",&n,&rate); for(int i=0;i<n;i++)scanf("%s",a[i]); if(!check())puts("error"); } }
L. Noblesse Code
#include<iostream> #include<algorithm> #include<vector> using namespace std; typedef long long ll; typedef pair<ll,ll>P; typedef vector<ll>V; const int N=50005; int Case,n,q,i,x,cnt,ce,ans[N];ll A,B; struct E{V s;int t;}e[N*3]; inline int compare(const V&a,const V&b){ int n=a.size(),m=b.size(),i; if(a[0]!=b[0])return a[0]<b[0]?-1:1; for(i=1;i<n&&i<m;i+=2){ if(a[i]!=b[i])return a[i]<b[i]?-1:1; if(!a[i])return 0; if(a[i+1]<b[i+1]){ if(i+2>=n)return -1; return a[i+2]<b[i]?-1:1; } if(a[i+1]>b[i+1]){ if(i+2>=m)return 1; return a[i]<b[i+2]?-1:1; } } if(i<n)return 1; if(i<m)return -1; return 0; } inline bool cmp(const E&a,const E&b){ int sgn=compare(a.s,b.s); if(sgn)return sgn<0; return a.t<b.t; } inline P decode(const V&s){ P ret(s[0],s[0]); for(int i=1;i+1<s.size();i+=2){ if(s[i]==-1)ret.first+=ret.second*s[i+1]; else ret.second+=ret.first*s[i+1]; } return ret; } inline V zip(P p){ V s; while(1){ if(p.first==p.second)break; if(p.first>p.second){ ll old=p.first; p.first%=p.second; if(!p.first)p.first=p.second; s.push_back((old-p.first)/p.second); s.push_back(-1); }else{ ll old=p.second; p.second%=p.first; if(!p.second)p.second=p.first; s.push_back((old-p.second)/p.first); s.push_back(-2); } } s.push_back(p.first); reverse(s.begin(),s.end()); return s; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>Case; while(Case--){ cin>>n>>q; ce=0; for(i=1;i<=n;i++){ cin>>A>>B; e[++ce].t=0; e[ce].s=zip(P(A,B)); } for(i=1;i<=q;i++){ ans[i]=0; cin>>A>>B; V s=zip(P(A,B)); e[++ce].t=-i; e[ce].s=s; e[++ce].t=i; s.push_back(0); e[ce].s=s; } sort(e+1,e+ce+1,cmp); cnt=0; for(i=1;i<=ce;i++){ x=e[i].t; if(!x)cnt++; if(x>0)ans[x]+=cnt; if(x<0)ans[-x]-=cnt; } for(i=1;i<=q;i++)cout<<ans[i]<<endl; } }