Submission #310692

# Submission time Handle Problem Language Result Execution time Memory
310692 2020-10-07T16:08:51 Z georgerapeanu Furniture (JOI20_furniture) C++11
100 / 100
504 ms 9848 KB
#include <cstdio>
#include <cassert>
#include <vector>
#include <algorithm>

using namespace std;

vector<pair<int,int> > fst,snd;

int n,m;
int dp[1005][1005];

void update(int x,int y){
    if(x <= 0 || y <= 0){
        return ;
    }
    dp[x][y] = 0;
    if(dp[x - 1][y]){
        dp[x - 1][y]--;
        if(dp[x - 1][y] == 0){
            update(x - 1,y);
        }
    }
    if(dp[x][y - 1]){
        dp[x][y - 1]--;
        if(dp[x][y - 1] == 0){
            update(x,y - 1);
        }
    }
}

void refresh(vector<pair<int,int> > &v,int pos,int dir){
    if(dp[v[pos].first][v[pos].second] > 0){
        return ;
    }
    while(dp[v[pos].first][v[pos].second] == 0){
        pos--;
    }
    while(true){
        if(dp[v[pos].first + dir][v[pos].second + 1 - dir] > 0){
            pair<int,int> tmp = {v[pos].first + dir,v[pos].second + 1 - dir};   
            if(v[pos + 1] == tmp){
                break;
            }
            v[pos + 1] = tmp;
        }
        else{
            pair<int,int> tmp = {v[pos].first + 1 - dir,v[pos].second + dir};   
            if(v[pos + 1] == tmp){
                break;
            }
            v[pos + 1] = tmp;
        }
        pos++;
    }
}

int main(){

    scanf("%d %d",&n,&m);

    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= m;j++){
            scanf("%d",&dp[i][j]);
            dp[i][j] ^= 1;
        }
    }

    dp[n][m] = 1;

    for(int i = n;i;i--){
        for(int j = m;j;j--){
            if(i == n && j == m){
                continue;
            }
            if(dp[i][j] == 1){
                dp[i][j] = (dp[i + 1][j] > 0) + (dp[i][j + 1] > 0);
            }
        }
    }

    fst = {{1,1}};
    snd = {{1,1}};

    for(int i = 2;i < n + m;i++){
        if(dp[fst.back().first][fst.back().second + 1]){
            fst.push_back({fst.back().first,fst.back().second + 1});
        }
        else{
            fst.push_back({fst.back().first + 1,fst.back().second});
        }
        if(dp[snd.back().first + 1][snd.back().second]){
            snd.push_back({snd.back().first + 1,snd.back().second});
        }
        else{
            snd.push_back({snd.back().first,snd.back().second + 1});
        }
    }

    int q;

    scanf("%d",&q);

    while(q--){
        int x,y;
        scanf("%d %d",&x,&y);
        /* 
        for(auto it:fst)printf("(%d %d), ",it.first,it.second);printf("\n");
        for(auto it:snd)printf("(%d %d), ",it.first,it.second);printf("\n");
        printf("\n");
        */
        if(fst[x + y - 2] != make_pair(x,y) || snd[x + y - 2] != make_pair(x,y)){
            printf("1\n");
            if(dp[x][y] > 0){
                update(x,y);
            }
            if(dp[1][1] == 0){
                while(true);
            }
            refresh(fst,x + y - 2,0);
            refresh(snd,x + y - 2,1);
        }
        else{
            printf("0\n");
        }

    }

    return 0;
}

Compilation message

furniture.cpp: In function 'int main()':
furniture.cpp:60:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   60 |     scanf("%d %d",&n,&m);
      |     ~~~~~^~~~~~~~~~~~~~~
furniture.cpp:64:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   64 |             scanf("%d",&dp[i][j]);
      |             ~~~~~^~~~~~~~~~~~~~~~
furniture.cpp:102:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  102 |     scanf("%d",&q);
      |     ~~~~~^~~~~~~~~
furniture.cpp:106:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  106 |         scanf("%d %d",&x,&y);
      |         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 2 ms 768 KB Output is correct
3 Correct 3 ms 768 KB Output is correct
4 Correct 4 ms 768 KB Output is correct
5 Correct 4 ms 768 KB Output is correct
6 Correct 5 ms 768 KB Output is correct
7 Correct 5 ms 768 KB Output is correct
8 Correct 5 ms 768 KB Output is correct
9 Correct 5 ms 768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 640 KB Output is correct
2 Correct 2 ms 768 KB Output is correct
3 Correct 3 ms 768 KB Output is correct
4 Correct 4 ms 768 KB Output is correct
5 Correct 4 ms 768 KB Output is correct
6 Correct 5 ms 768 KB Output is correct
7 Correct 5 ms 768 KB Output is correct
8 Correct 5 ms 768 KB Output is correct
9 Correct 5 ms 768 KB Output is correct
10 Correct 14 ms 896 KB Output is correct
11 Correct 4 ms 628 KB Output is correct
12 Correct 231 ms 8440 KB Output is correct
13 Correct 113 ms 5880 KB Output is correct
14 Correct 424 ms 9336 KB Output is correct
15 Correct 425 ms 9336 KB Output is correct
16 Correct 462 ms 9464 KB Output is correct
17 Correct 477 ms 9720 KB Output is correct
18 Correct 453 ms 9652 KB Output is correct
19 Correct 504 ms 9848 KB Output is correct
20 Correct 429 ms 9848 KB Output is correct
21 Correct 469 ms 9848 KB Output is correct