Submission #292987

#TimeUsernameProblemLanguageResultExecution timeMemory
292987limabeansFurniture (JOI20_furniture)C++17
100 / 100
380 ms6392 KiB
#include <bits/stdc++.h>
using namespace std;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl





using ll = long long;

const ll mod = 1e9+7;
const int maxn = 1010;


int n, m;
int g[maxn][maxn];
int diag[maxn+maxn];


int upd(int x, int y) {
    // path via (x,y) is already blocked
    if (g[x][y]==1) {
	return 1;
    }

    // all paths go through (x,y)
    if (diag[x+y]==1) {
	return 0;
    }

    stack<pair<int,int>> stk;
    
    auto proc = [&](int x, int y) {
	if (g[x][y]==0) {
	    g[x][y]=1;
	    diag[x+y]--;
	    stk.emplace(x,y);
	}
    };

    proc(x,y);


    while (stk.size()) {
	int x=stk.top().first;
	int y=stk.top().second;
	stk.pop();

	// if neighboring diagonals are blocked, then some cells will also be blocked
	if (g[x-1][y+1]==1) {
	    proc(x-1,y);
	    proc(x,y+1);
	}
	if (g[x+1][y-1]==1) {
	    proc(x,y-1);
	    proc(x+1,y);
	}
    }

    return 1;
}


int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);  cout.tie(0);
    cin>>n>>m;
    for (int i=0; i<=n+1; i++) {
	for (int j=0; j<=m+1; j++) {
	    g[i][j]=1;
	}
    }
    
    for (int i=1; i<=n; i++) {
	for (int j=1; j<=m; j++) {
	    g[i][j]=0;
	    diag[i+j]++;
	}
    }

    for (int i=1; i<=n; i++) {
	for (int j=1; j<=m; j++) {
	    int x;
	    cin>>x;
	    if (x==1) {
		upd(i,j);
	    }
	}
    }


    int q;
    cin>>q;
    while (q--) {
	int x, y;
	cin>>x>>y;
	cout<<upd(x,y)<<"\n";
    }
    
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...