This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define size(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
int n,m;
int dim(int v)
{
return (v/m) + (v - (v/m)*m);
}
bool blocked[1000000];
int good[1000000];
bool bad[1000000];
bitset<1000000> odw2;
bitset<1000000> odw;
vector<int> graf1[1000001];
vector<int> graf2[1000001];
int good_on_dim[1000001];
void dfs1(int v)
{
odw[v] = 1;
good[v]++;
forall(it,graf1[v])
{
if(odw[it] == 0 && !blocked[it])
{
dfs1(it);
}
}
}
void dfs2(int v)
{
odw2[v] = 1;
good[v]++;
if(good[v] == 2)
{
good_on_dim[dim(v)]++;
bad[v] = false;
}
forall(it,graf2[v])
{
if(odw2[it] == 0 && !blocked[it])
{
dfs2(it);
}
}
}
void dfs_bad(int v, int pocz = -1)
{
int c_bad = 0;
int c_bad2 = 0;
forall(it,graf1[v]) if(bad[it]) c_bad2++;
forall(it,graf2[v]) if(bad[it]) c_bad++;
if(c_bad != 2 && c_bad2 != 2 && !(v < m && c_bad == 1) && !(v % m == 0 && c_bad == 1) && !(v > m*n-1-m && c_bad2 == 1) && !(v%m == m-1 && c_bad2== 1) && v != pocz) return;
// cout << v << " dfs\n";
bad[v] = true;
if(v != pocz) good_on_dim[dim(v)]--;
forall(it,graf1[v])
{
if(!bad[it]) dfs_bad(it);
}
forall(it,graf2[v])
{
if(!bad[it]) dfs_bad(it);
}
}
int get_int()
{
int x = 0;
char c = getchar();
for(;c < '0' || c > '9'; c = getchar());
for(;c >= '0' && c <= '9'; c = getchar())
{
x = 10*x + (c - '0');
}
return x;
}
void solve()
{
n = get_int(); m = get_int();
rep(i,n) rep(j,m) blocked[i*m+j] = get_int();
rep(i,n)
{
rep(j,m)
{
if(i != n-1)
{
graf1[i*m+j].pb((i+1)*m+j);
graf2[(i+1)*m+j].pb(i*m+j);
}
if(j != m-1)
{
graf1[i*m+j].pb(i*m+j+1);
graf2[i*m+j+1].pb(i*m+j);
}
}
}
//cout << "xd\n";
dfs1(0);
rep(i,n*m) bad[i] = true;
//cout << "xd\n";
dfs2(n*m-1);
// cout << "xd\n";
int q;
q = get_int();
// rep(i,n*m)
/// {
// cout << bad[i] << " ";
// }
// cout << "bad\n";
rep(i,q)
{
int x,y;
y = get_int(); x = get_int();
x--;
y--;
int v = y*m+x;
// cout << v<< " " << bad[v] << " " << dim(v) << " " << size(good_on_dim[dim(v)]) << " d\n";
// forall(it,good_on_dim[dim(v)])cout << it << " ";
// cout << " dim\n";
if(bad[v])
{
cout << "1\n";
continue;
}
if(good_on_dim[dim(v)] == 1)
{
cout << "0\n";
continue;
}
cout << "1\n";
bad[v] = true;
good_on_dim[dim(v)]--;
dfs_bad(v,v);
}
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random();
int t = 1;
// cin >> t;
while(t--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |