#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
const int MAXN = 1013;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
int N, M, Q;
bitset<MAXN> grid[MAXN];
struct dsu
{
pii dsu[MAXN][MAXN];
void init()
{
FOR(i, 0, N)
{
FOR(j, 0, M)
{
dsu[i][j] = {i, j};
}
}
}
pii get(pii u)
{
return (u == dsu[u.fi][u.se] ? u : dsu[u.fi][u.se] = get(dsu[u.fi][u.se]));
}
bool bad(pii a, pii b)
{
a = get(a);
b = get(b);
if (a == get({0, 0}) && b == get({N - 1, M - 1})) return true;
if (b == get({0, 0}) && a == get({N - 1, M - 1})) return true;
return false;
}
void merge(pii a, pii b)
{
if (a == MP(-1, -1)) return;
if (b == MP(-1, -1)) return;
a = get(a);
b = get(b);
if (a == b) return;
dsu[a.fi][a.se] = b;
return;
}
};
set<int> xs[MAXN], ys[MAXN];
dsu hor, ver;
pii hors[2], vers[2];
bool ins(int i, int j)
{
if (i == 0)
{
hors[0] = {0, 0};
}
else
{
//merge it w guy on previous row.
auto it = xs[i - 1].UB(j + 1); //shoudl be <= j+1
if (it != xs[i - 1].begin()) hors[0] = {i - 1, *prev(it)};
else hors[0] = {-1, -1};
}
if (i == N - 1)
{
hors[1] = {N - 1, M - 1};
}
else
{
//merge w guy on next row.
auto it = xs[i + 1].LB(j - 1); //should be >= j-1
if (it != xs[i + 1].end()) hors[1] = {i + 1, *it};
}
if (j == 0)
{
vers[0] = {0, 0};
}
else
{
auto it = ys[j - 1].UB(i + 1); //shoudl be <= j+1
if (it != xs[j - 1].begin()) vers[0] = {*prev(it), j - 1};
else vers[0] = {-1, -1};
}
if (j == M - 1)
{
vers[1] = {N - 1, M - 1};
}
else
{
auto it = ys[j + 1].LB(i - 1); //should be >= j-1
if (it != ys[j + 1].end()) vers[1] = {*it, j + 1};
else vers[1] = {-1, - 1};
}
//now check if any conflict!
if (hors[0] != MP(-1, -1) && hors[1] != MP(-1, -1) && hor.bad(hors[0], hors[1])) return false;
if (vers[0] != MP(-1, -1) && vers[1] != MP(-1, -1) && ver.bad(vers[0], vers[1])) return false;
hor.merge({i, j}, hors[0]);
hor.merge({i, j}, hors[1]);
ver.merge({i, j}, vers[0]);
ver.merge({i, j}, vers[1]);
xs[i].insert(j);
ys[j].insert(i);
return true;
}
int32_t main()
{
cout << fixed << setprecision(12);
cerr << fixed << setprecision(4);
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> M;
hor.init();
ver.init();
FOR(i, 0, N)
{
FOR(j, 0, M)
{
bool b; cin >> b;
if (b) ins(i, j);
}
}
cin >> Q;
while(Q--)
{
int x, y;
cin >> x >> y; x--; y--;
cout << ins(x, y) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
2028 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4 ms |
2028 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |