/*
ID: varunra2
LANG: C++
TASK: furniture
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "lib/debug.h"
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define debug_arr(...) \
cerr << "[" << #__VA_ARGS__ << "]:", debug_arr(__VA_ARGS__)
#pragma GCC diagnostic ignored "-Wsign-compare"
//#pragma GCC diagnostic ignored "-Wunused-parameter"
//#pragma GCC diagnostic ignored "-Wunused-variable"
#else
#define debug(...) 42
#endif
#define EPS 1e-9
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define x first
#define y second
const double PI = acos(-1.0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef vector<int> VI;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto& a : x)
#define sz(x) (int)(x).size()
typedef pair<int, int> pii;
typedef vector<int> vi;
#pragma GCC diagnostic ignored "-Wsign-compare"
// util functions
int n, m;
vector<vector<bool>> grid, fromstart, fromend;
void init() {
grid.resize(n);
trav(x, grid) x.assign(m, false);
fromstart.resize(n);
trav(x, fromstart) { x.assign(m, true); }
fromend = fromstart;
}
bool inbnds(int x, int y) { return x >= 0 and y >= 0 and x < n and y < m; }
bool addable(int x, int y) {
bool ret = false;
if (inbnds(0, y + 1)) {
for (int i = 0; i < x; i++) {
if (fromend[i][y + 1] and fromstart[i][y + 1]) {
ret = true;
break;
}
}
}
if (!ret and inbnds(n - 1, y - 1)) {
for (int i = x + 1; i < n; i++) {
if (fromend[i][y - 1] and fromstart[i][y - 1]) {
ret = true;
break;
}
}
}
return ret;
}
void addend(int x, int y) {
fromend[x][y] = false;
// so if we are coming from the end, we need to look at (x - 1, y), (x, y - 1)
if (inbnds(x - 1, y) and fromend[x - 1][y]) {
// we need to check if we can make it false, and then remove it
bool ok = false;
if (inbnds(x - 1, y + 1)) {
ok = fromend[x - 1][y + 1];
}
if (!ok) {
addend(x - 1, y);
}
}
if (inbnds(x, y - 1) and fromend[x][y - 1]) {
bool ok = false;
if (inbnds(x + 1, y - 1)) {
ok = fromend[x + 1][y - 1];
}
if (!ok) {
addend(x, y - 1);
}
}
}
void addfront(int x, int y) {
fromstart[x][y] = false;
if (inbnds(x + 1, y) and fromstart[x + 1][y]) {
bool ok = false;
if (inbnds(x + 1, y - 1)) {
ok = fromstart[x + 1][y - 1];
}
if (!ok) {
addfront(x + 1, y);
}
}
if (inbnds(x, y + 1) and fromstart[x][y + 1]) {
bool ok = false;
if (inbnds(x - 1, y + 1)) {
ok = fromstart[x - 1][y + 1];
}
if (!ok) {
addfront(x, y + 1);
}
}
}
bool add(int x, int y) {
bool ok = addable(x, y);
if (!ok) return false;
// we checked if we are allowed to add it
addfront(x, y);
addend(x, y);
return true;
}
void pr() {
debug("fromend");
trav(x, fromend) debug(x);
debug("fromstart");
trav(x, fromstart) debug(x);
}
int main() {
// #ifndef ONLINE_JUDGE
// freopen("furniture.in", "r", stdin);
// freopen("furniture.out", "w", stdout);
// #endif
cin.sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
init();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int val;
cin >> val;
if (val) {
assert(add(i, j));
}
}
}
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
x--, y--;
if (add(x, y))
cout << '1';
else
cout << '0';
cout << '\n';
}
return 0;
}
Compilation message
furniture.cpp: In function 'void pr()':
furniture.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
furniture.cpp:145:3: note: in expansion of macro 'debug'
145 | debug("fromend");
| ^~~~~
furniture.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
furniture.cpp:146:20: note: in expansion of macro 'debug'
146 | trav(x, fromend) debug(x);
| ^~~~~
furniture.cpp:31:11: warning: unused variable 'first' [-Wunused-variable]
31 | #define x first
| ^~~~~
furniture.cpp:48:31: note: in definition of macro 'trav'
48 | #define trav(a, x) for (auto& a : x)
| ^
furniture.cpp:146:8: note: in expansion of macro 'x'
146 | trav(x, fromend) debug(x);
| ^
furniture.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
furniture.cpp:147:3: note: in expansion of macro 'debug'
147 | debug("fromstart");
| ^~~~~
furniture.cpp:19:20: warning: statement has no effect [-Wunused-value]
19 | #define debug(...) 42
| ^~
furniture.cpp:148:22: note: in expansion of macro 'debug'
148 | trav(x, fromstart) debug(x);
| ^~~~~
furniture.cpp:31:11: warning: unused variable 'first' [-Wunused-variable]
31 | #define x first
| ^~~~~
furniture.cpp:48:31: note: in definition of macro 'trav'
48 | #define trav(a, x) for (auto& a : x)
| ^
furniture.cpp:148:8: note: in expansion of macro 'x'
148 | trav(x, fromstart) debug(x);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
364 KB |
Output is correct |
3 |
Correct |
3 ms |
364 KB |
Output is correct |
4 |
Correct |
4 ms |
492 KB |
Output is correct |
5 |
Correct |
4 ms |
492 KB |
Output is correct |
6 |
Correct |
5 ms |
492 KB |
Output is correct |
7 |
Correct |
5 ms |
492 KB |
Output is correct |
8 |
Correct |
4 ms |
492 KB |
Output is correct |
9 |
Correct |
4 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
3 ms |
364 KB |
Output is correct |
3 |
Correct |
3 ms |
364 KB |
Output is correct |
4 |
Correct |
4 ms |
492 KB |
Output is correct |
5 |
Correct |
4 ms |
492 KB |
Output is correct |
6 |
Correct |
5 ms |
492 KB |
Output is correct |
7 |
Correct |
5 ms |
492 KB |
Output is correct |
8 |
Correct |
4 ms |
492 KB |
Output is correct |
9 |
Correct |
4 ms |
492 KB |
Output is correct |
10 |
Correct |
15 ms |
748 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
12 |
Correct |
366 ms |
5708 KB |
Output is correct |
13 |
Correct |
107 ms |
2668 KB |
Output is correct |
14 |
Correct |
927 ms |
10536 KB |
Output is correct |
15 |
Correct |
992 ms |
10716 KB |
Output is correct |
16 |
Correct |
1408 ms |
11628 KB |
Output is correct |
17 |
Correct |
1239 ms |
12232 KB |
Output is correct |
18 |
Correct |
1008 ms |
11928 KB |
Output is correct |
19 |
Correct |
1918 ms |
12572 KB |
Output is correct |
20 |
Correct |
1904 ms |
12592 KB |
Output is correct |
21 |
Correct |
1223 ms |
12632 KB |
Output is correct |