#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e3;
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)
{
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,int s2)
{
auto [a,b]=edg(x,y);
if((f(a)==s1 && f(b)==s2) || (f(a)==s2 && f(b)==s1))
return false;
tab[x][y]=true;
st[x].insert(y);
un_all(x,y);
return true;
}
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>>tab[i][j];
}
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);
}
}
int q;
cin>>q;
while(q--)
{
int a,b;
cin>>a>>b;
cout<<try_en(a,b,fv(0,2),fv(2,0))<<"\n";
//for(int i=0;i<=n+1;i++)
//{
// for(int j=0;j<=m+1;j++)
// cerr<<fv(i,j)<<" \n"[j==m+1];
//}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |