#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e3;
struct S
{
bool tab[N+10][N+10];
set<int> st[N+10];
int fau[(N+10)*(N+10)];
int conv(int x,int y)
{
return (N+2)*x+y;
}
int f(int x)
{
if(fau[x]!=x)
fau[x]=f(fau[x]);
return fau[x];
}
int fv(int x,int y)
{
return f(conv(x,y));
}
void un(int x,int y)
{
fau[f(x)]=f(y);
return;
}
pair<int,int> edg(int x,int y)
{
int a=*prev(st[x-1].upper_bound(y+1));
int b=*st[x+1].lower_bound(y-1);
a=conv(x-1,a);
b=conv(x+1,b);
return {a,b};
}
void un_all(int x,int y)
{
tab[x][y]=true;
st[x].insert(y);
auto [a,b]=edg(x,y);
un(conv(x,y),a);
un(conv(x,y),b);
return;
}
bool try_en(int x,int y)
{
int s1=fv(0,2),s2=fv(2,0);
auto [a,b]=edg(x,y);
if((f(a)==s1 && f(b)==s2) || (f(a)==s2 && f(b)==s1))
return false;
return true;
}
void init(int n,int m)
{
for(int i=0;i<=n+1;i++)
tab[i][0]=tab[i][m+1]=true;
for(int i=0;i<=m+1;i++)
tab[0][i]=tab[n+1][i]=true;
tab[0][0]=tab[n+1][m+1]=false;
for(int i=0;i<=n+1;i++)
{
for(int j=0;j<=m+1;j++)
{
fau[conv(i,j)]=conv(i,j);
if(tab[i][j])
st[i].insert(j);
}
}
for(int i=0;i<=n+1;i++)
{
for(int j=0;j<=m+1;j++)
{
if(tab[i][j] && tab[i+1][j])
un(conv(i,j),conv(i+1,j));
if(tab[i][j] && tab[i][j+1])
un(conv(i,j),conv(i,j+1));
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(tab[i][j])
un_all(i,j);
}
}
return;
}
};
S s1,s2;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>s1.tab[i][j];
s2.tab[j][i]=s1.tab[i][j];
}
}
s1.init(n,m);
s2.init(m,n);
int q;
cin>>q;
while(q--)
{
int a,b;
cin>>a>>b;
bool ok=(s1.try_en(a,b) && s2.try_en(b,a));
if(ok)
{
s1.un_all(a,b);
s2.un_all(b,a);
}
cout<<ok<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
1228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |