Submission #1292898

#TimeUsernameProblemLanguageResultExecution timeMemory
1292898trandaihao5555Furniture (JOI20_furniture)C++20
100 / 100
240 ms12280 KiB
#include <bits/stdc++.h>

#define int long long

#define debug     cout << "ok\n";
#define SQR(x)    (1LL * ((x) * (x)))
#define MASK(i)   (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define fi        first
#define se        second
#define pb        push_back

#define mp make_pair
#define pii pair<int,int>
#define pli pair<ll,int>
#define vi vector<int>

#define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int ui;

using namespace std;

const int M = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFLL = (ll)2e18 + 7LL;
const ld PI = acos(-1);

const int dx[] = {1, 0, 0, -1, -1, 1, 1, -1};
const int dy[] = {0, 1, -1, 0, -1, -1, 1, 1};

template<class _, class __>
    bool minimize(_ &x, const __ y){
        if(x > y){
            x = y;
            return true;
        } else return false;
    }
template<class _, class __>
    bool maximize(_ &x, const __ y){
        if(x < y){
            x = y;
            return true;
        } else return false;
    }

template<class _,class __>
    void Add(_ &x, const __ y) {
        x += y;
        if (x >= M) {
            x -= M;
        }
        return;
    }

template<class _,class __>
    void Diff(_ &x, const __ y) {
        x -= y;
        if (x < 0) {
            x += M;
        }
        return;
    }

//--------------------------------------------------------------

const int MaxN = 1007;

int n,m,q,a[MaxN][MaxN],cnt[MaxN+MaxN];
bool f[MaxN][MaxN],g[MaxN][MaxN];

bool inSide(int x,int y) {
    return 1 <= x && x <= n && 1 <= y && y <= m;
}

void prepare() {
    for (int i=1;i<=n+m;i++) {
        for (int j=1;j<=n;j++) {
            int y = i - j;
            if (inSide(j,y)) cnt[i]++;
        }
    }
    for (int i=0;i<=m+1;i++) f[n+1][i] = f[0][i] = g[n+1][i] = g[0][i] = true;
    for (int i=0;i<=n+1;i++) f[i][0] = f[i][m+1] = g[i][0] = g[i][m+1] = true;
}


void Del(int _x,int _y) {
    queue<pii> q;
    q.push(mp(_x,_y));
    if (!f[_x][_y] && !g[_x][_y]) cnt[_x + _y]--;
    f[_x][_y] = true;
    g[_x][_y] = true;
    while (!q.empty()) {
        int x = q.front().fi;
        int y = q.front().se;
        q.pop();
        for (int i=0;i<2;i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (!inSide(xx,yy) || f[xx][yy]) continue;
            int _cnt = 0;
            for (int j=0;j<2;j++) {
                int xxx = xx - dx[j];
                int yyy = yy - dy[j];
                _cnt += f[xxx][yyy];
            }
            if (_cnt == 2) {
                if (!f[xx][yy] && !g[xx][yy]) cnt[xx + yy]--;
                f[xx][yy] = true,q.push(mp(xx,yy));
            }
        }
    }

    q.push(mp(_x,_y));
    while (!q.empty()) {
        int x = q.front().fi;
        int y = q.front().se;
        q.pop();
        for (int i=2;i<4;i++) {
            int xx = x + dx[i];
            int yy = y + dy[i];
            if (!inSide(xx,yy) || g[xx][yy]) continue;
            int _cnt = 0;
            for (int j=2;j<4;j++) {
                int xxx = xx - dx[j];
                int yyy = yy - dy[j];
                _cnt += g[xxx][yyy];
            }
            if (_cnt == 2) {
                if (!g[xx][yy] && !f[xx][yy]) cnt[xx+yy]--;
                g[xx][yy] = true,q.push(mp(xx,yy));
            }
        }
    }
}




void sol() {
    cin >> n >> m;
    prepare();
//    for (int i=2;i<=n+m;i++) cout << cnt[i] << ' '; cout << '\n';
    for (int i=1;i<=n;i++) {
        for (int j=1;j<=m;j++) {
            cin >> a[i][j];
            if (a[i][j] == 1) Del(i,j);
        }
    }
    cin >> q;
    for (int i=1;i<=q;i++) {
//        for (int j=1;j<=n;j++) {
//            for (int k=1;k<=m;k++) {
//                cout << f[j][k];
//            }
//            cout << '\n';
//        }
//        for (int j=1;j<=n;j++) {
//            for (int k=1;k<=m;k++) {
//                cout << g[j][k];
//            }
//            cout << '\n';
//        }
        int x,y;
        cin >> x >> y;
        int _cnt = cnt[x+y];
        if (!g[x][y] && !f[x][y]) _cnt--;
        if (_cnt) Del(x,y),cout << 1 << '\n';
        else cout << 0 << '\n';
    }
}

signed main() {
//    freopen("test.inp","r",stdin);
//    freopen("test.out","w",stdout);
    FAST
    int t=1;
//    cin >> t;
    while (t--) sol();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...