Submission #557360

#TimeUsernameProblemLanguageResultExecution timeMemory
557360Jarif_RahmanFurniture (JOI20_furniture)C++17
100 / 100
317 ms14940 KiB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, m; cin >> n >> m;
    bool **s = new bool*[n], **blocked = new bool*[n], **nice = new bool*[n];

    for(int i = 0; i < n; i++){
        s[i] = new bool[m], blocked[i] = new bool[m], nice[i] = new bool[m];
        fill(s[i], s[i]+m, 0);
        fill(blocked[i], blocked[i]+m, 0);
        fill(nice[i], nice[i]+m, 1);
    }

    int* dig = new int[n+m-1];
    fill(dig, dig+n+m-1, 0);
    for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) dig[i+j]++;

    auto valid = [&](int x, int y){
        return x >= 0 && x < n && y >= 0 && y < m;
    };
    auto is_blocked = [&](int x, int y){
        return !valid(x, y) || blocked[x][y];
    };
    auto is_nice = [&](int x, int y){
        return valid(x, y) && nice[x][y];
    };

    function<void(int, int)> make_unnice = [&](int x, int y){
        if(!nice[x][y]) return;
        nice[x][y] = 0;
        dig[x+y]--;
        if(is_nice(x-1, y) && !is_nice(x-1, y+1)) make_unnice(x-1, y);
        if(is_nice(x, y-1) && !is_nice(x+1, y-1)) make_unnice(x, y-1);
    };
    function<void(int, int)> block = [&](int x, int y){
        blocked[x][y] = 1;
        make_unnice(x, y);
        if(!is_blocked(x+1, y) && is_blocked(x+1, y-1)) block(x+1, y);
        if(!is_blocked(x, y+1) && is_blocked(x-1, y+1)) block(x, y+1);
    };

    auto add = [&](int x, int y){
        if(nice[x][y] && dig[x+y] == 1) return 0;
        block(x, y);
        return 1;
    };

    for(int i = 0; i < n; i++) for(int j = 0; j < m; j++){
        bool x; cin >> x;
        if(x) add(i, j);
    }

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