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 F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
//#define int long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=3005;
int n,m,q,a[N][N],d[N][N],c,vis[N][N],f[N],e[N],dp[N][N];
bool b[N][N];
int rec(int x,int y,int dd){
    if (x>n || y>m || b[x][y]) {return INT_MIN;}
    if (x==n && y==m){
        d[x][y]=dd;
        return dp[x][y]=c;
    }if (vis[x][y]==-c) return dp[x][y];
    vis[x][y]=-c;
    dp[x][y]=max(rec(x+1,y,dd+1),rec(x,y+1,dd+1));
    d[x][y]=dd*bool(dp[x][y]==c);
    if (e[d[x][y]]==c) f[d[x][y]]++;
    else f[d[x][y]]=1,e[d[x][y]]=c;
    return dp[x][y];
}
void dfs1(int x,int y){
    f[d[x][y]]--;
    d[x][y]=0;
    if (x<n && !d[x+1][y-1] && d[x+1][y]) dfs1(x+1,y);
    if (y<m && !d[x-1][y+1] && d[x][y+1]) dfs1(x,y+1);
    return;
}
void dfs2(int x,int y){
    vis[x][y]=c;
    if (!d[x+1][y] && !d[x][y+1]) f[d[x][y]]--,d[x][y]=0;
    if (d[x][y]) return;
    if (x>1 && vis[x-1][y]!=c) dfs2(x-1,y);
    if (y>1 && vis[x][y-1]!=c) dfs2(x,y-1);
}
int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    cin>>n>>m;
    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) cin>>b[i][j];
    --c;rec(1,1,0);
    /*for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++) cout<<dp[i][j]<<' ';
        cout<<endl;
    }*/
    cin>>q;
    while (q--){
        int x,y;cin>>x>>y;
        if (dp[x][y]!=c || !d[x][y]){
            b[x][y]=true;
            cout<<1<<endl;
            continue;
        }
        if (f[d[x][y]]>1 && e[d[x][y]]==c){
            b[x][y]=true;
            c++;
            rec(1,1,0);
            cout<<1<<endl;
            continue;
        }cout<<0<<endl;
    }
    return 0;
}
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |