/* In the name of God */
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <list>
#include <map>
#include <numeric>
#include <limits>
#include <limits.h>
#include <unordered_map>
#include <unordered_set>
#include <chrono>
#include <random>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef map<int, int> MPII;
typedef vector<int> VI;
typedef vector<ll> VL;
#define PB push_back
#define POP pop_back
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define endl '\n'
#define dbg(x) cerr << '[' << #x << ": " << x << "]\n"
#define dbg2(x, y) cerr << '[' << #x << ": " << x << ", " << #y << ": " << y << "]\n"
#define Yes cout << "Yes\n"
#define YES cout << "YES\n"
#define No cout << "No\n"
#define NO cout << "NO\n"
const ll INF = (ll)2e18 + 1386;
const ld eps = 0.000000000000001;
const int MOD = 1e9 + 7;
inline int mod_(int a){ int res = a + MOD; return (res >= MOD? res - MOD : res); }
inline int mod_add(int a, int b){ int res = a + b; return (res >= MOD? res - MOD : res); }
inline int mod_neg(int a, int b){ int res = (abs(a - b) < MOD? a - b : (a - b) % MOD); return (res < 0? res + MOD : res); }
inline int mod_mlt(int a, int b){ return (1ll * a * b % MOD); }
inline string intToString(ll a){ char x[100]; sprintf(x, "%lld", a); string s = x; return s; }
inline ll stringToInt(string s){ ll res; char x[100]; strcpy(x, s.c_str()); sscanf(x, "%lld", &res); return res; }
inline void fileIO(string i, string o){ freopen(i.c_str(), "r", stdin); freopen(o.c_str(), "w", stdout); }
const int MAXN = 1002;
int n, m, deg1[MAXN][MAXN], degn[MAXN][MAXN], cnt[2 * MAXN];
bool a[MAXN][MAXN];
void upd(int x, int y){
queue<PII> q;
if (deg1[x][y]){
deg1[x][y] = 0;
q.push({x, y});
}
while (!q.empty()){
auto v = q.front();
int xx = v.first, yy = v.second;
q.pop();
if (degn[xx][yy] != 0) cnt[xx + yy]--;
deg1[xx + 1][yy]--, deg1[xx][yy + 1]--;
if (deg1[xx + 1][yy] == 0 && xx + 1 <= n) q.push({xx + 1, yy});
if (deg1[xx][yy + 1] == 0 && yy + 1 <= m) q.push({xx, yy + 1});
}
if (degn[x][y]){
degn[x][y] = 0;
q.push({x, y});
}
while (!q.empty()){
auto v = q.front();
int xx = v.first, yy = v.second;
q.pop();
if (deg1[xx][yy] != 0) cnt[xx + yy]--;
degn[xx - 1][yy]--, degn[xx][yy - 1]--;
if (degn[xx - 1][yy] == 0 && xx - 1 >= 1) q.push({xx - 1, yy});
if (degn[xx][yy - 1] == 0 && yy - 1 >= 1) q.push({xx, yy - 1});
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
deg1[i][j] = degn[i][j] = 2;
cnt[i + j]++;
}
deg1[i][1] = 1;
degn[i][m] = 1;
}
fill(deg1[1], deg1[1] + m + 1, 1), fill(degn[n], degn[n] + m + 1, 1);
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> a[i][j];
if (a[i][j] == 1) upd(i, j);
}
}
int q; cin >> q;
while (q--){
int x, y;
cin >> x >> y;
bool another = (cnt[x + y] - !(deg1[x][y] == 0 || degn[x][y] == 0) > 0);
cout << another << endl;
if (another) upd(x, y);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
2 ms |
1108 KB |
Output is correct |
3 |
Correct |
3 ms |
1108 KB |
Output is correct |
4 |
Correct |
6 ms |
1108 KB |
Output is correct |
5 |
Correct |
4 ms |
1236 KB |
Output is correct |
6 |
Correct |
5 ms |
1236 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
5 ms |
1236 KB |
Output is correct |
9 |
Correct |
7 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
980 KB |
Output is correct |
2 |
Correct |
2 ms |
1108 KB |
Output is correct |
3 |
Correct |
3 ms |
1108 KB |
Output is correct |
4 |
Correct |
6 ms |
1108 KB |
Output is correct |
5 |
Correct |
4 ms |
1236 KB |
Output is correct |
6 |
Correct |
5 ms |
1236 KB |
Output is correct |
7 |
Correct |
4 ms |
1236 KB |
Output is correct |
8 |
Correct |
5 ms |
1236 KB |
Output is correct |
9 |
Correct |
7 ms |
1236 KB |
Output is correct |
10 |
Correct |
14 ms |
852 KB |
Output is correct |
11 |
Correct |
3 ms |
792 KB |
Output is correct |
12 |
Correct |
229 ms |
9632 KB |
Output is correct |
13 |
Correct |
82 ms |
8684 KB |
Output is correct |
14 |
Correct |
407 ms |
10124 KB |
Output is correct |
15 |
Correct |
458 ms |
10236 KB |
Output is correct |
16 |
Correct |
498 ms |
10524 KB |
Output is correct |
17 |
Correct |
398 ms |
11016 KB |
Output is correct |
18 |
Correct |
458 ms |
10700 KB |
Output is correct |
19 |
Correct |
433 ms |
11048 KB |
Output is correct |
20 |
Correct |
438 ms |
11080 KB |
Output is correct |
21 |
Correct |
503 ms |
11148 KB |
Output is correct |