Submission #569806

#TimeUsernameProblemLanguageResultExecution timeMemory
569806OttoTheDinoFurniture (JOI20_furniture)C++17
100 / 100
525 ms21672 KiB
    #include <bits/stdc++.h>
    using namespace std;
     
    #define rep(i,s,e)                  for (int i = s; i <= e; ++i)
    #define rrep(i,s,e)                 for (int i = s; i >= e; --i)
    #define pb                          push_back
    #define pf                          push_front
    #define fi                          first
    #define se                          second
    #define all(a)                      a.begin(), a.end()
    #define len(a)                      (int)a.size()
    typedef long long ll;
    typedef pair<int, int> ii;
    typedef vector<ii> vii;
    typedef vector<int> vi;
    typedef vector<double> vd;
    typedef vector<string> vs;
    typedef vector<ll> vll;
     
    const int mx = 1e3;
     
    int ftree[mx+1][mx+1][2];
     
    void upd (int tp, int p, int x, int val) {
        x++;
        while (x <= mx) {
            ftree[x][p][tp] += val;
            x += x&(-x);
        }
    }
     
    int pref (int tp, int p, int x) {
        int sum = 0;
        while (x>0) {
            sum += ftree[x][p][tp];
            x -= x&(-x);
        }
        return sum;
    }
     
    int query (int tp, int p, int l, int r) {
        l++, r++;
        return pref(tp, p, r)-pref(tp, p, l-1);
    }
     
    int main() {
        ios::sync_with_stdio(0);
        cin.tie(0);
     
        int n, m; cin >> n >> m;
     
        bool c[n+1][m+1] = {}, d[n+1][m+1] = {};
     
        rep (i,0,n-1) {
            rep (j,0,m-1) {
                cin >> c[i][j];
                c[i][j] ^= 1;
                d[i][j]=c[i][j];
            }
        }
     
        rrep (i,n-1,0) {
            rrep (j,m-1,0) {
                bool b = 0;
                if (j<m-1) b |= c[i][j+1];
                if (i<n-1) b |= c[i+1][j];
                if (i==n-1 && j==m-1) b = 1;
                c[i][j] &= b;
            }
        }

        rep (i,0,n-1) {
            rep (j,0,m-1) {
                bool b = 0;
                if (j) b |= d[i][j-1];
                if (i) b |= d[i-1][j];
                if (!i && !j) b = 1;
                d[i][j] &= b;
                if (c[i][j] && d[i][j]) {
                    upd(0,j,i,1);
                    upd(1,i,j,1);
                }
            }
        }
     
        int Q; cin >> Q;
        while (Q--) {
            int x, y; cin >> x >> y;
            x--, y--;
            if ((y==m-1 || query(0,y+1,0,x-1)==0) && (x==n-1 || query(1,x+1,0,y-1)==0)) cout << "0\n";
            else {
                cout << "1\n";
                if (!c[x][y] || !d[x][y]) continue;
                queue<ii> q;
                q.push({x,y});
                while (len(q)) {
                    int i = q.front().fi, j = q.front().se;
                    q.pop();
                    if (i!=x || j!=y) {
                        if (!c[i][j] || c[i+1][j] || c[i][j+1]) continue;
                    }
                    c[i][j] = 0;
                    if(d[i][j]){
                       upd (0, j, i, -1);
                       upd (1, i, j, -1);
                    }
                    if (i && c[i-1][j]) q.push({i-1,j});
                    if (j && c[i][j-1]) q.push({i,j-1});
                }
                d[x][y] = 0;
                q.push({x+1,y});
                q.push({x,y+1});

                while (len(q)) {
                    int i = q.front().fi, j = q.front().se;
                    q.pop();
                    if (!d[i][j] || (i&&d[i-1][j]) || (j&&d[i][j-1])) continue;
                    d[i][j] = 0;
                    if(c[i][j]){
                       upd (0, j, i, -1);
                       upd (1, i, j, -1);
                    }
                    if (i<n-1 && d[i+1][j]) q.push({i+1,j});
                    if (j<m-1 && d[i][j+1]) q.push({i,j+1});
                }
            }
        }
     
       return 0;
    }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...