Submission #1331642

#TimeUsernameProblemLanguageResultExecution timeMemory
1331642disfyyFurniture (JOI20_furniture)C++20
0 / 100
2 ms1604 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll N = 2e5 + 5;
const ll MOD = 1e9 + 7;
int a[1005][1005], cnt[N], dp[1005][1005], dp1[1005][1005], b[1005][1005], n, m;

bool ok(ll x, ll y) {
	ll can = false;
	if(x - 1 >= 1 && dp[x - 1][y - 1]) {
		can = true;
	}
	if(y - 1 >= 1 && dp[x][y - 1]) {
		can = true;
	}
	return can;
}

void check(ll x, ll y) {
	dp[x][y] = 0, dp1[x][y] = 0;
	cnt[x + y]--;
	if(x - 1 >= 1 && dp[x - 1][y] && dp1[x - 1][y] && !ok(x - 1, y)) {
		check(x - 1, y);
	}
	if(y - 1 >= 1 && dp[x][y - 1] && dp1[x][y - 1] && !ok(x, y - 1)) {
		check(x, y - 1);
	}
	if(x + 1 <= n && dp[x + 1][y] && dp1[x + 1][y] && !ok(x + 1, y)) {
		check(x + 1, y);
	}
	if(y + 1 <= m && dp[x][y + 1] && dp1[x][y + 1] && !ok(x, y + 1)) {
		check(x, y + 1);
	}
}

void solve() {
	cin >> n >> m;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			cin >> a[i][j];
		}
	}
	dp[1][1] = 1;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			if(a[i][j] == 0) {
				if(i - 1 >= 1) {
					dp[i][j] |= dp[i - 1][j];
				}
				if(j - 1 >= 1) {
					dp[i][j] |= dp[i][j - 1];
				}
			}
		}
	}
	dp1[n][m] = 1;
	for(int i = n; i >= 1; i--) {
		for(int j = m; j >= 1; j--) {
			if(a[i][j] == 0) {
				if(i + 1 <= n) {
					dp1[i][j] |= dp1[i + 1][j];
				}
				if(j + 1 <= m) {
					dp1[i][j] |= dp1[i][j + 1];
				}
			}
		}
	}
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			if(dp[i][j] && dp1[i][j]) {
				cnt[i + j]++;
			}
		}
	}
	ll q;
	cin >> q;
	while(q--) {
		ll x, y;
		cin >> x >> y;
		if(cnt[x + y] > 1) {
			if(dp[x][y] && dp1[x][y]) {
				cout << 1 << '\n';
				check(x, y);
			} else {
				cout << 1 << '\n';
			}
		} else {
			if(dp[x][y] && dp1[x][y]) {
				cout << 0 << '\n';
			} else {
				cout << 1 << '\n';	
			}
		}
	}
}

signed main() {
	
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	
	ll test = 1;
	// cin >> test;
	for(int i = 1; i <= test; i++) {
		solve();
	}
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...