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 fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 5e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
int n;
int m;
int numq;
int a[1007][1007];
int f[mxN];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
queue<pair<int, int>> q;
bool ok(int x, int y)
{
return (1 <= x && x <= n && 1 <= y && y <= m);
}
bool add(int x, int y)
{
if(a[x][y] == 1) return 1;
if(f[x + y] == 1) return 0;
a[x][y] = 1;
f[x + y]--;
q.push({x, y});
while(!q.empty()) {
int x = q.front().fi;
int y = q.front().se;
q.pop();
if(a[x - 1][y + 1] == 1) {
if(ok(x - 1, y) == true && a[x - 1][y] == 0) {
a[x - 1][y] = 1;
f[x - 1 + y]--;
q.push({x - 1, y});
}
if(ok(x, y + 1) == true && a[x][y + 1] == 0) {
a[x][y + 1] = 1;
f[x + y + 1]--;
q.push({x, y + 1});
}
}
if(a[x + 1][y - 1] == 1) {
if(ok(x, y - 1) == true && a[x][y - 1] == 0) {
a[x][y - 1] = 1;
f[x + y - 1]--;
q.push({x, y - 1});
}
if(ok(x + 1, y) == true && a[x + 1][y] == 0) {
a[x + 1][y] = 1;
f[x + 1 + y]--;
q.push({x + 1, y});
}
}
}
return true;
}
void solve()
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) f[i + j]++;
for(int i = 0; i <= n + 1; i++)
for(int j = 0; j <= m + 1; j++) {
if(1 <= i && i <= n && 1 <= j && j <= m) continue;
a[i][j] = 1;
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) {
int x;
cin >> x;
if(x == 1) add(i, j);
}
cin >> numq;
while(numq--) {
int x, y;
cin >> x >> y;
cout << add(x, y) << endl;
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen(TASK".inp" , "r" , stdin);
//freopen(TASK".out" , "w" , stdout);
int tc = 1;
//cin >> tc;
while(tc--) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |