来源:Comet OJ - Contest #13
一眼并查集,然后发现这题 tmd 要卡常数的说卧槽...
发现这里又要用并查集跳过访问点,又要用并查集维护联通块,于是开俩并查集分别维护就好了
一开始 XJB 搞了两个并查集建了个完全的连接方式,然后 xjb 写了堆合并,调了一会儿交上去喜见 TLE (自闭现场)
挺好的啊,然后改成动态开点并且访问点跳过的操作也优化了一下,终于爬过去了 ORZ
原 Code
代码挺好打&&极不清爽
//by Judge (zlw ak ioi)
#include<bits/stdc++.h>
#define P pair<int,int>
#define fi first
#define se second
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(Rg int i=(a),I=(b)-1;i>I;--i)
#define ll long long
using namespace std;
const int N=1003,M=N*N;
typedef int ARR[N][N];
typedef int arr[M];
#ifndef Judge
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){ int x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;
} inline int read1(){ char c=getchar();
while(!isdigit(c)) c=getchar(); return c-48;
} char sr[1<<21],z[20];int CCF=-1,Z;
inline void Ot(){fwrite(sr,1,CCF+1,stdout),CCF=-1;}
inline void print(int x,char chr='\n'){
if(CCF>1<<20)Ot();if(x<0)sr[++CCF]=45,x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++CCF]=z[Z],--Z);sr[++CCF]=chr;
} int n,m,q,cnt,ans; ARR id,a; arr fa,f; P po[M];
inline int getf(int x){
return f[x]^x?f[x]=getf(f[x]):x;
}
inline bool merge(int x,int y){
return x=getf(x),y=getf(y),x^y?f[y]=x,1:0;
}
inline void newnode(int x,int y){ ++ans;
if(x>1&&a[x-1][y]&&merge(id[x][y],id[x-1][y])) --ans;
if(x<n&&a[x+1][y]&&merge(id[x][y],id[x+1][y])) --ans;
if(y>1&&a[x][y-1]&&merge(id[x][y],id[x][y-1])) --ans;
if(y<m&&a[x][y+1]&&merge(id[x][y],id[x][y+1])) --ans;
}
inline int find(int x){
return fa[x]^x?fa[x]=find(fa[x]):x;
}
#define X po[now].fi
#define Y po[now].se
inline void Out(){
cerr<<ans<<":\n";
fp(i,1,n){
fp(j,1,m) cerr<<a[i][j];
cerr<<"\n";
} cerr<<"\n\n";
}
int main(){ n=read(),m=read();
fp(i,1,n) fp(j,1,m+1) ++cnt,
fa[cnt]=f[cnt]=cnt,po[id[i][j]=cnt]=P(i,j);
fp(i,1,n) fp(j,1,m) if((a[i][j]=read1()))
newnode(i,j),fa[id[i][j]]=id[i][j+1];
q=read(); Rg int x1,x2,y1,y2,now,nxt;
while(q--){ x1=read(),y1=read(),x2=read(),y2=read();
fp(i,x1,x2){ now=find(id[i][y1]);
while(po[now].se<=y2) nxt=find(id[X][Y+1]),
a[X][Y]=1,newnode(X,Y),fa[now]=nxt,now=nxt;
} print(ans);
} return Ot(),0;
}
Code
代码挺好打
//by Judge (zlw ak ioi)
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define Rg register
#define fp(i,a,b) for(Rg int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(Rg int i=(a),I=(b)-1;i>I;--i)
#define ll long long
using namespace std;
const int N=3003,M=N*N;
typedef int ARR[N][N];
typedef int arr[M];
#ifndef Judge
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
char buf[1<<21],*p1=buf,*p2=buf;
inline int read(){ int x=0,f=1; char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*f;
} inline int read1(){ char c=getchar();
while(!isdigit(c)) c=getchar(); return c-48;
} char sr[1<<21],z[20];int CCF=-1,Z;
inline void Ot(){fwrite(sr,1,CCF+1,stdout),CCF=-1;}
inline void print(int x,char chr='\n'){
if(CCF>1<<20)Ot();if(x<0)sr[++CCF]=45,x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++CCF]=z[Z],--Z);sr[++CCF]=chr;
} int n,m,q,cnt,ans; ARR a,id,fa; arr f;
int getf(int x){
return f[x]^x?f[x]=getf(f[x]):x;
}
inline void merge(int x,int y){
x=getf(x),y=getf(y); if(x^y) f[y]=x,--ans;
}
const int h[5]={0,-1,1,0,0},l[5]={0,0,0,-1,1};
inline bool in(int x,int y){
return x>0&&x<=n&&y>0&&y<=m&&a[x][y];
}
int find(int x,int y){
return fa[x][y]^y?fa[x][y]=find(x,fa[x][y]):y;
}
inline void newnode(int x,int y){
a[x][y]=1,++ans,++cnt,f[id[x][y]=cnt]=cnt;
fa[x][y]=find(x,fa[x][y+1]);
fp(i,1,4){ Rg int dx=x+h[i],dy=y+l[i];
if(in(dx,dy)) merge(id[x][y],id[dx][dy]);
}
}
inline void Out(){ cerr<<"\n\n"<<ans<<":\n";
fp(i,1,n){ fp(j,1,m) cerr<<a[i][j]; cerr<<"\n"; }
}
int main(){ n=read(),m=read(); fp(i,1,n) fp(j,1,m+1) fa[i][j]=j;
fp(i,1,n) fp(j,1,m) if((a[i][j]=read1())) newnode(i,j);
q=read(); Rg int x1,x2,y1,y2,now;
while(q--){ x1=read(),y1=read(),x2=read(),y2=read();
fp(i,x1,x2){ now=find(i,fa[i][y1]);
while(now<=y2) newnode(i,now),now=find(i,fa[i][y1]);
} print(ans);
} return Ot(),0;
}