//0 1 1 0 1
//0 1 0 0 1
//1 0 0 1 1
//0 1 1 0 1
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
#define F first
#define S second
#define pb push_back
#define sze size()
#define all(x) x.begin() , x.end()
#define wall__ cout << "--------------------------------------\n";
#define kids int mid = (tl + tr) >> 1, cl = v << 1, cr = v << 1 | 1
#define file_io freopen("input.cpp", "r", stdin); freopen("output.cpp", "w", stdout);
typedef long long ll;
typedef long double dl;
typedef pair < int , int > pii;
typedef pair < int , ll > pil;
typedef pair < ll , int > pli;
typedef pair < ll , ll > pll;
typedef pair < int , pii > piii;
typedef pair < ll, pll > plll;
const ll N = 1e3 + 10;
const ll mod = 1e9 + 7;
const ll inf = 2e16;
const ll rinf = -2e16;
const ll INF = 1e9 + 10;
const ll rINF = -1e9 - 10;
const ll lg = 32;
int n, m;
bitset < N > dp[N], lf[N], rg[N];
bool bl[N][N];
void upd (int x, int y) {
if (dp[x][y] == 0) return;
dp[x][y] = 0;
vector < pii > q; int it = 0; q.pb({x, y});
while (it < q.sze) {
int r = q[it].F, c = q[it].S; ++it;
if (dp[r - 1][c] == 1 && dp[r - 1][c + 1] == 0) {
dp[r - 1][c] = 0; q.pb({r - 1, c});
}
if (dp[r][c - 1] == 1 && dp[r + 1][c - 1] == 0) {
dp[r][c - 1] = 0; q.pb({r, c - 1});
}
}
q.clear(); it = 0; q.pb({x, y});
while (it < q.sze) {
int r = q[it].F, c = q[it].S; ++it;
if (dp[r + 1][c] == 1 && dp[r + 1][c - 1] == 0) {
dp[r + 1][c] = 0; q.pb({r + 1, c});
}
if (dp[r][c + 1] == 1 && dp[r - 1][c + 1] == 0) {
dp[r][c + 1] = 0; q.pb({r, c + 1});
}
}
}
void solve () {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp[i][j] = 1;
cin >> bl[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (bl[i][j]) upd(i, j);
}
}
lf[0][0] = 1;
for (int i = 1; i < N; i++) {
lf[i] = lf[i - 1]; lf[i][i] = 1;
}
rg[N - 1][N - 1] = 1;
for (int i = N - 2; i >= 0; --i) {
rg[i] = rg[i + 1]; rg[i][i] = 1;
}
int q; cin >> q;
while(q--) {
int x, y; cin >> x >> y;
if (dp[x][y] == 0) {
cout << 1 << '\n';
bl[x][y] = 1;
upd(x, y);
} else {
bitset < N > a, b;
a = (dp[x + 1] & lf[y - 1]); b = (dp[x - 1] & rg[y + 1]);
int z = a.count() + b.count();
if (z == 0) cout << 0 << '\n';
else {
cout << 1 << '\n';
bl[x][y] = 1;
upd(x, y);
}
}
}
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int t = 1;
// cin >> t;
while (t--) {solve();}
return 0;
}
/*
4 4
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
4
1 3
2 3
3 2
4 1
*/
//shrek will AC this;
Compilation message
furniture.cpp: In function 'void upd(int, int)':
furniture.cpp:46:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
46 | while (it < q.sze) {
| ^
furniture.cpp:56:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
56 | while (it < q.sze) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
724 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
724 KB |
Output is correct |
6 |
Correct |
3 ms |
728 KB |
Output is correct |
7 |
Correct |
4 ms |
724 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
4 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
2 ms |
724 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
3 ms |
724 KB |
Output is correct |
6 |
Correct |
3 ms |
728 KB |
Output is correct |
7 |
Correct |
4 ms |
724 KB |
Output is correct |
8 |
Correct |
4 ms |
852 KB |
Output is correct |
9 |
Correct |
4 ms |
724 KB |
Output is correct |
10 |
Correct |
9 ms |
980 KB |
Output is correct |
11 |
Correct |
5 ms |
696 KB |
Output is correct |
12 |
Correct |
140 ms |
6456 KB |
Output is correct |
13 |
Correct |
66 ms |
3568 KB |
Output is correct |
14 |
Correct |
259 ms |
11216 KB |
Output is correct |
15 |
Correct |
246 ms |
11504 KB |
Output is correct |
16 |
Correct |
247 ms |
12208 KB |
Output is correct |
17 |
Correct |
255 ms |
12852 KB |
Output is correct |
18 |
Correct |
261 ms |
12756 KB |
Output is correct |
19 |
Correct |
275 ms |
14004 KB |
Output is correct |
20 |
Correct |
268 ms |
15160 KB |
Output is correct |
21 |
Correct |
282 ms |
15260 KB |
Output is correct |