Submission #306809

# Submission time Handle Problem Language Result Execution time Memory
306809 2020-09-26T10:08:42 Z MrDomino Furniture (JOI20_furniture) C++14
100 / 100
300 ms 25892 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 2000 + 7;
int n;
int m;
int a[N][N];
bool down[N][N];
bool up[N][N];
bool on[N][N];
int cnt[2 * N];

void init()
{
    for (int i = 1; i <= n + m; i++)
    {
        cnt[i] = 0;
    }
    down[n + 1][m] = 1;
    up[1][0] = 1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (a[i][j] == 0)
            {
                up[i][j] = up[i - 1][j] | up[i][j - 1];
            }
            else
            {
                up[i][j] = 0;
            }
        }
    }
    for (int i = n; i >= 1; i--)
    {
        for (int j = m; j >= 1; j--)
        {
            if (a[i][j] == 0)
            {
                down[i][j] = down[i + 1][j] | down[i][j + 1];
            }
            else
            {
                down[i][j] = 0;
            }
        }
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            if (up[i][j] && down[i][j])
            {
                cnt[i + j]++;
                on[i][j] = 1;
            }
            else
            {
                on[i][j] = 0;
            }
        }
    }
}

bool bdown(int r, int c)
{
    return (on[r][c] == 1 && on[r + 1][c] == 0 && on[r][c + 1] == 0);
}

bool bup(int r, int c)
{
    return (on[r][c] == 1 && on[r - 1][c] == 0 && on[r][c - 1] == 0);
}

void dfs_up(int r, int c)
{
    if (on[r][c])
    {
        on[r][c] = 0;
        cnt[r + c]--;
    }
    if (bdown(r - 1, c))
    {
        dfs_up(r - 1, c);
    }
    if (bdown(r, c - 1))
    {
        dfs_up(r, c - 1);
    }
}

void dfs_down(int r, int c)
{
    if (on[r][c])
    {
        on[r][c] = 0;
        cnt[r + c]--;
    }
    if (bup(r + 1, c))
    {
        dfs_down(r + 1, c);
    }
    if (bup(r, c + 1))
    {
        dfs_down(r, c + 1);
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
        {
            cin >> a[i][j];
        }
    }
    init();
    int q;
    cin >> q;
    while (q--)
    {
        int r, c;
        cin >> r >> c;
        if (on[r][c] == 0)
        {
            cout << "1\n";
            continue;
        }
        if (cnt[r + c] == 1)
        {
            cout << "0\n";
            continue;
        }
        cout << "1\n";
        a[r][c] = 1;
        dfs_up(r, c);
        dfs_down(r, c);
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1024 KB Output is correct
2 Correct 2 ms 1408 KB Output is correct
3 Correct 3 ms 1280 KB Output is correct
4 Correct 3 ms 1280 KB Output is correct
5 Correct 3 ms 1408 KB Output is correct
6 Correct 4 ms 1536 KB Output is correct
7 Correct 4 ms 1408 KB Output is correct
8 Correct 4 ms 1408 KB Output is correct
9 Correct 4 ms 1408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1024 KB Output is correct
2 Correct 2 ms 1408 KB Output is correct
3 Correct 3 ms 1280 KB Output is correct
4 Correct 3 ms 1280 KB Output is correct
5 Correct 3 ms 1408 KB Output is correct
6 Correct 4 ms 1536 KB Output is correct
7 Correct 4 ms 1408 KB Output is correct
8 Correct 4 ms 1408 KB Output is correct
9 Correct 4 ms 1408 KB Output is correct
10 Correct 9 ms 1280 KB Output is correct
11 Correct 3 ms 896 KB Output is correct
12 Correct 159 ms 16632 KB Output is correct
13 Correct 82 ms 15096 KB Output is correct
14 Correct 264 ms 22716 KB Output is correct
15 Correct 275 ms 22776 KB Output is correct
16 Correct 273 ms 24056 KB Output is correct
17 Correct 295 ms 25336 KB Output is correct
18 Correct 288 ms 24572 KB Output is correct
19 Correct 297 ms 25720 KB Output is correct
20 Correct 285 ms 25892 KB Output is correct
21 Correct 300 ms 25720 KB Output is correct