#include <bits/stdc++.h>
#define fi first
#define se second
#define ssize(x) int(x.size())
#define pn printf("\n");
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define vv vector
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int inf = 2e09; ll infll = 2e18; int mod = 119<<23|1;
void bfs_up(vv<vv<int>> &t, vv<vv<int>> &ed, pii st, vv<set<int>> &s){
queue<pii> q;
if(!t[st.fi-1][st.se]) q.emplace(pii(st.fi-1, st.se));
if(!t[st.fi][st.se-1]) q.emplace(pii(st.fi, st.se-1));
while(!q.empty()){
int i = q.front().fi, j = q.front().se; q.pop();
//~ printf("%d %d\n", i, j);
if(!t[i+1][j] || !t[i][j+1] || t[i][j]) continue; // nie musimy usuwać, bo jest przejście
t[i][j] = 1;
auto it = prev(s[i].upper_bound(j)); // modyfikuję seta, nie sądzę, że muszę sprawdzać czy begin
if(*it == j) s[i].erase(it);
else ed[i][*it] = j-1;
if(!t[i-1][j]) q.emplace(pii(i-1, j));
if(!t[i][j-1]) q.emplace(pii(i, j-1));
}
}
void bfs_down(vv<vv<int>> &t, vv<vv<int>> &ed, pii st, vv<set<int>> &s){
queue<pii> q;
if(!t[st.fi+1][st.se]) q.emplace(pii(st.fi+1, st.se));
if(!t[st.fi][st.se+1]) q.emplace(pii(st.fi, st.se+1));
while(!q.empty()){
int i = q.front().fi, j = q.front().se; q.pop();
//~ printf("%d %d\n", i, j);
if(!t[i-1][j] || !t[i][j-1] || t[i][j]) continue; // nie musimy usuwać, bo jest przejście
t[i][j] = 1;
auto it = s[i].lower_bound(j); // modyfikuję seta
assert(it != s[i].end());
if(ed[i][*it] == j) s[i].erase(it);
else{ int tmp = *it; ed[i][*it+1] = ed[i][*it], ed[i][*it] = 0, s[i].erase(it), s[i].emplace(tmp+1); }
if(!t[i+1][j]) q.emplace(pii(i+1, j));
if(!t[i][j+1]) q.emplace(pii(i, j+1));
}
}
void answer(){
int n, m; scanf("%d%d", &n, &m);
vv<vv<int>> t(n+2, vv<int>(m+2, 0)), ed(n+2, vv<int>(m+2, 0)); // w e trzymam koniec przedziału, ktory jest na secie
vector<pii> red;
for(int i = 1; i <= n; ++i){
for(int j = 1, a; j <= m; ++j){ scanf("%d", &a); if(a) red.emplace_back(i, j); }
}
for(int i = 0; i <= n+1; ++i) t[i][0] = 1, t[i][m+1] = 1; // narożników chyba nie muszę, ale kto wie, zostawiam komentarz
for(int j = 1; j <= m; ++j) t[0][j] = 1, t[n+1][j] = 1;
vv<set<int>> s(n+2);
for(int i = 1; i <= n; ++i) s[i].emplace(1), ed[i][1] = m;
for(pii &u : red){
int i = u.fi, j = u.se;
//~ if((i == 1 && j == 1) || (i == n && j == m)) continue;
if(t[i][j]) continue;
auto it = prev(s[i].upper_bound(j));
if(ed[i][*it] != j) ed[i][j+1] = ed[i][*it], s[i].emplace(j+1);
it = prev(s[i].upper_bound(j));
if(*it != j) ed[i][*it] = j-1;
else s[i].erase(it);
t[i][j] = 1;
//~ printf("a\n");
bfs_up(t, ed, pii(i, j), s);
bfs_down(t, ed, pii(i, j), s);
//~ pn;
//~ for(i = 1; i <= n; ++i){
//~ for(j = 1; j <= m; ++j) printf("%d ", t[i][j]);
//~ pn;
//~ }
//~ for(i = 1; i <= n; ++i){
//~ for(int x : s[i]) printf("%d %d, ", x, ed[i][x]);
//~ pn;
//~ }
}
int q; scanf("%d", &q);
for(++q; --q; ){
int i, j; scanf("%d%d", &i, &j);
if(t[i][j]){ printf("1\n"); continue; }
if(ssize(s[i]) > 1){
printf("1\n");
auto it = prev(s[i].upper_bound(j));
if(ed[i][*it] != j) ed[i][j+1] = ed[i][*it], s[i].emplace(j+1);
it = prev(s[i].upper_bound(j));
if(*it != j) ed[i][*it] = j-1;
else s[i].erase(it);
t[i][j] = 1;
bfs_up(t, ed, pii(i, j), s), bfs_down(t, ed, pii(i, j), s);
continue;
}
bool left = 1, right = 1;
auto it = prev(s[i].upper_bound(j));
int it_beg = *it, it_ed = ed[i][*it];
if(*it == j) left = 0;
if(it_ed == j) right = 0;
if(right){
bool good = 0;
it = s[i-1].lower_bound(j+1);
if(it != s[i-1].end() && *it <= it_ed) good = 1;
if(it != s[i-1].begin() && j+1 <= ed[i-1][*prev(it)]) good = 1;
if(!good) right = 0;
}
if(left){
bool good = 0;
it = s[i+1].lower_bound(it_beg);
if(it != s[i+1].end() && *it <= j-1) good = 1;
if(it != s[i+1].begin() && it_beg <= ed[i+1][*prev(it)]) good = 1;
if(!good) left = 0;
}
if(!left && !right) printf("0\n");
else{
printf("1\n");
it = prev(s[i].upper_bound(j));
if(ed[i][*it] != j) ed[i][j+1] = ed[i][*it], s[i].emplace(j+1);
it = prev(s[i].upper_bound(j));
if(*it != j) ed[i][*it] = j-1;
else s[i].erase(it);
t[i][j] = 1;
bfs_up(t, ed, pii(i, j), s), bfs_down(t, ed, pii(i, j), s);
}
}
}
int main(){
int T = 1;
for(++T; --T; ) answer();
return 0;
}
Compilation message
furniture.cpp: In function 'void answer()':
furniture.cpp:51:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | int n, m; scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
furniture.cpp:55:42: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | for(int j = 1, a; j <= m; ++j){ scanf("%d", &a); if(a) red.emplace_back(i, j); }
| ~~~~~^~~~~~~~~~
furniture.cpp:85:15: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | int q; scanf("%d", &q);
| ~~~~~^~~~~~~~~~
furniture.cpp:87:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | int i, j; scanf("%d%d", &i, &j);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
4 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
564 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
4 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
604 KB |
Output is correct |
8 |
Correct |
3 ms |
564 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
10 ms |
1368 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
274 ms |
16456 KB |
Output is correct |
13 |
Correct |
94 ms |
13260 KB |
Output is correct |
14 |
Correct |
300 ms |
20652 KB |
Output is correct |
15 |
Correct |
311 ms |
20304 KB |
Output is correct |
16 |
Correct |
235 ms |
18264 KB |
Output is correct |
17 |
Correct |
238 ms |
19312 KB |
Output is correct |
18 |
Correct |
301 ms |
21076 KB |
Output is correct |
19 |
Correct |
271 ms |
20044 KB |
Output is correct |
20 |
Correct |
234 ms |
19928 KB |
Output is correct |
21 |
Correct |
258 ms |
20164 KB |
Output is correct |