QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#385673#3039. CleaningfantisWA 12ms73980kbC++146.9kb2024-04-10 23:00:062024-04-10 23:00:06

Judging History

你现在查看的是最新测评结果

  • [2024-04-10 23:00:06]
  • 评测
  • 测评结果:WA
  • 用时:12ms
  • 内存:73980kb
  • [2024-04-10 23:00:06]
  • 提交

answer

#include<bits/stdc++.h>//0xnnb
using namespace std;
typedef long long ll;
typedef double db;
int read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
const int N=1005,NN=1000005,M=4000005;
int n,m,q,ee,vex[NN],tim,dfn[NN],low[NN];
int stk[NN],top,col[NN],cnt,sum[NN];
int f[NN],fa[NN],nxtl[NN],nxtr[NN],rt;
int pos[NN],maxl[NN],maxr[NN];
bool gol[NN],gor[NN];
int xl[NN],xr[NN],yl[NN],yr[NN],fa2[NN][25];
int sum2[NN],sum3[NN],dep[NN];
bool instk[NN],vis[NN];
char a[N][N];
vector<int>p[NN],son[NN],e2[NN];
struct Node{
	int u,v,next;
}e[M];
int id(int x,int y){
	return (x-1)*m+y;
}
int getx(int u){
	return (u-1)/m+1;
}
int gety(int u){
	return (u-1)%m+1;
}
void add(int u,int v){
	e[++ee].u=u,e[ee].v=v;
	e[ee].next=vex[u],vex[u]=ee;
}
void tarjan(int u){
	dfn[u]=low[u]=++tim;
	stk[++top]=u;
	instk[u]=1;
	for(int i=vex[u];i;i=e[i].next){
		int v=e[i].v;
		if(!dfn[v]){
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(instk[v]){
			low[u]=min(low[u],dfn[v]);
		}
	}
	if(low[u]==dfn[u]){
		cnt++;
		while(1){
			int x=stk[top--];
			col[x]=cnt;
			instk[x]=0;
			p[cnt].push_back(x);
			sum[cnt]++;
			if(x==u)break;
		}
	}
}
int find(int x){
	if(f[x]==x)return x;
	return f[x]=find(f[x]);
}
void addnode(int x){
	vis[x]=1;
	if(nxtl[x]&&(!vis[nxtl[x]]))addnode(nxtl[x]);
	stk[++top]=x;
	if(nxtr[x]&&(!vis[nxtr[x]]))addnode(nxtr[x]);
}
int getsum(int x,int y){
	return sum2[y]-sum2[nxtl[x]];
}
void dfs(int u){
	sum3[u]+=getsum(maxl[u],maxr[u]);
	dep[u]=dep[fa[u]]+1;
	fa2[u][0]=fa[u];
	for(int i=1;i<=20;i++){
		fa2[u][i]=fa2[fa2[u][i-1]][i-1];
	}
	int tmp=son[u].size();
	for(int i=0;i<tmp;i++){
		int v=son[u][i];
		sum3[v]+=sum3[u];
		dfs(v);
	}
}
int solve(int x,int y){
	x=col[x],y=col[y];
	if(dep[x]<dep[y])return 0;
	int ans=sum3[x];
	for(int i=20;i>=0;i--){
		if(dep[fa2[x][i]]<dep[y])continue;
		x=fa2[x][i];
	}
	ans-=sum3[x];
	if(x==y)return ans+sum[x];
	if(fa[x]!=fa[y])return 1;
	if(pos[x]<=pos[y]){
		if(pos[maxr[x]]<pos[y])return 2;
		return ans+getsum(x,y);
	}
	else{
		if(pos[maxl[x]]>pos[y])return 3;
		return ans+getsum(y,x);
	}
}
int main(){
    n=read(),m=read(),q=read();
    for(int i=1;i<=n;i++){
    	scanf("%s",a[i]+1);
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(a[i][j]!='U'&&i>1)add(id(i,j),id(i-1,j));
			if(a[i][j]!='D'&&i<n)add(id(i,j),id(i+1,j));
			if(a[i][j]!='L'&&j>1)add(id(i,j),id(i,j-1));
			if(a[i][j]!='R'&&j<m)add(id(i,j),id(i,j+1));
		}
	}
	for(int i=1;i<=n*m;i++){
		if(!dfn[i])tarjan(i);
	}
	for(int i=1;i<=ee;i++){
		int u=e[i].u,v=e[i].v;
		if(col[u]!=col[v]){
			e2[col[v]].push_back(col[u]);
		}
	}
	for(int i=1;i<=cnt;i++){
		f[i]=i;
	}
	for(int i=cnt;i>=1;i--){
		xl[i]=yl[i]=1000;
		for(int j=0;j<sum[i];j++){
			int u=p[i][j];
			int x=getx(u),y=gety(u);
			xl[i]=min(xl[i],x),xr[i]=max(xr[i],x);
			yl[i]=min(yl[i],y),yr[i]=max(yr[i],y);
		}
		int tmp=e2[i].size();
		for(int j=0;j<tmp;j++){
			int u=e2[i][j];
			int uu=p[u][0];
			u=find(u);
			if(u==i)continue;
			int x=getx(uu),y=gety(uu);
			if(x>=xl[i]&&x<=xr[i]&&y>=yl[i]&&y<=yr[i]){
				f[u]=fa[u]=i;
			}
		}
		//L
		if(yl[i]!=1){
			bool flag=0;
			for(int j=xl[i];j<=xr[i];j++){
				int u=id(j,yl[i]-1);
				u=col[u];
				if(u<i)continue;
				u=find(u);
				if(instk[u])continue;
				stk[++top]=u;
				instk[u]=1;
				if(a[j][yl[i]-1]!='R')flag=1;
			}
			if(top>1){
				cnt++;
				f[cnt]=cnt;
				xl[cnt]=yl[cnt]=1000;
				for(int j=1;j<=top;j++){
					int u=stk[j];
					xl[cnt]=min(xl[cnt],xl[u]),xr[cnt]=max(xr[cnt],xr[u]);
					yl[cnt]=min(yl[cnt],yl[u]),yr[cnt]=max(yr[cnt],yr[u]);
					f[u]=fa[u]=cnt;
				}
				nxtl[i]=cnt;
				nxtr[cnt]=i;
			}
			else if(top==1)nxtl[i]=stk[1],nxtr[stk[1]]=i;
			if(flag)gor[nxtl[i]]=1;
			while(top)instk[stk[top--]]=0;
		}
		//R
		if(yr[i]!=m){
			bool flag=0;
			for(int j=xl[i];j<=xr[i];j++){
				int u=id(j,yr[i]+1);
				u=col[u];
				if(u<i)continue;
				u=find(u);
				if(instk[u])continue;
				stk[++top]=u;
				instk[u]=1;
				if(a[j][yr[i]+1]!='L')flag=1;
			}
			if(top>1){
				cnt++;
				f[cnt]=cnt;
				xl[cnt]=yl[cnt]=1000;
				for(int j=1;j<=top;j++){
					int u=stk[j];
					xl[cnt]=min(xl[cnt],xl[u]),xr[cnt]=max(xr[cnt],xr[u]);
					yl[cnt]=min(yl[cnt],yl[u]),yr[cnt]=max(yr[cnt],yr[u]);
					f[u]=fa[u]=cnt;
				}
				nxtr[i]=cnt,nxtl[cnt]=i;
			}
			else if(top==1)nxtr[i]=stk[1],nxtl[stk[1]]=i;
			if(flag)gol[nxtr[i]]=1;
			while(top)instk[stk[top--]]=0;
		}
		//U
		if(xl[i]!=1){
			bool flag=0;
			for(int j=yl[i];j<=yr[i];j++){
				int u=id(xl[i]-1,j);
				u=col[u];
				if(u<i)continue;
				u=find(u);
				if(instk[u])continue;
				stk[++top]=u;
				instk[u]=1;
				if(a[xl[i]-1][j]!='D')flag=1;
			}
			if(top>1){
				cnt++;
				f[cnt]=cnt;
				xl[cnt]=yl[cnt]=1000;
				for(int j=1;j<=top;j++){
					int u=stk[j];
					xl[cnt]=min(xl[cnt],xl[u]),xr[cnt]=max(xr[cnt],xr[u]);
					yl[cnt]=min(yl[cnt],yl[u]),yr[cnt]=max(yr[cnt],yr[u]);
					f[u]=fa[u]=cnt;
				}
				nxtl[i]=cnt,nxtr[cnt]=i;
			}
			else if(top==1)nxtl[i]=stk[1],nxtr[stk[1]]=i;
			if(flag)gor[nxtl[i]]=1;
			while(top)instk[stk[top--]]=0;
		}
		//D
		if(xr[i]!=n){
			bool flag=0;
			for(int j=yl[i];j<=yr[i];j++){
				int u=id(xr[i]+1,j);
				u=col[u];
				if(u<i)continue;
				u=find(u);
				if(instk[u])continue;
				stk[++top]=u;
				instk[u]=1;
				if(a[xr[i]+1][j]!='U')flag=1;
			}
			if(top>1){
				cnt++;
				f[cnt]=cnt;
				xl[cnt]=yl[cnt]=1000;
				for(int j=1;j<=top;j++){
					int u=stk[j];
					xl[cnt]=min(xl[cnt],xl[u]),xr[cnt]=max(xr[cnt],xr[u]);
					yl[cnt]=min(yl[cnt],yl[u]),yr[cnt]=max(yr[cnt],yr[u]);
					f[u]=fa[u]=cnt;
				}
				nxtr[i]=cnt,nxtl[cnt]=i;
			}
			else if(top==1)nxtr[i]=stk[1],nxtl[stk[1]]=i;
			if(flag)gol[nxtr[i]]=1;
			while(top)instk[stk[top--]]=0;
		}
	}
	for(int i=1;i<=cnt;i++){
		if(!fa[i]){
			stk[++top]=i;
		}
	}
	if(top==1)rt=stk[1];
	else{
		rt=++cnt;
		for(int i=1;i<=top;i++){
			fa[stk[i]]=rt;
		}
	}
	top=0;
	for(int i=1;i<=cnt;i++){
		if(fa[i]){
			son[fa[i]].push_back(i);
		}
	}
	for(int i=1;i<=cnt;i++){
		int tmp=son[i].size();
		for(int j=0;j<tmp;j++){
			int u=son[i][j];
			if(!vis[u])addnode(u);
		}
		for(int i=1;i<=top;i++){
			pos[stk[i]]=i;
			sum2[stk[i]]=sum2[stk[i-1]]+sum[stk[i]];
		}
		for(int i=1;i<=top;i++){
			if(!gol[stk[i]]){
				maxl[stk[i]]=stk[i];
			}
			else{
				maxl[stk[i]]=maxl[stk[i-1]];
			}
		}
		for(int i=top;i>=1;i--){
			if(!gor[stk[i]]){
				maxr[stk[i]]=stk[i];
			}
			else{
				maxr[stk[i]]=maxr[stk[i+1]];
			}
		}
		top=0;
	}
	dfs(rt);
	for(int qq=1;qq<=q;qq++){
		int x1=read(),y1=read(),x2=read(),y2=read();
		if(qq!=248)continue;
		printf("%d\n",solve(id(x1,y1),id(x2,y2)));
	}
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 12ms
memory: 73980kb

input:

1 1 1
L
1 1 1 1

output:


result:

wrong answer Answer contains longer sequence [length = 1], but output contains 0 elements