#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll,ll> ii;
typedef vector<ii> vii;
#define F first
#define S second
ll min(ll x, ll y){
return ((x < y) ? x : y);
}
ll max(ll x, ll y){
return ((x > y) ? x : y);
}
class Grid{
private:
ll w,h;
vvi c;
vii upper_path; // minimize y value
vii left_path; // minimize x value
bool check (ll x, ll y){
return (h > x && x >= 0 && w > y && y >= 0);
}
bool check (ii p){
return (h > p.F && p.F >= 0 && w > p.S && p.S >= 0);
}
void modify_path(ll x, ll y, vii& path, ll dx, ll dy){
while (c[path[x+y].F][path[x+y].S] == 1){
path[x+y].F += dx;
path[x+y].S += dy;
}
ll bdx = max(dx,0), sdx = min(dx,0);
ll bdy = max(dy,0), sdy = min(dy,0);
vii for_mod;
for (ll i = x+y-1; i >= 0; i--){
ll diff = abs(path[i+1].F-path[i].F) + abs(path[i+1].S-path[i].S);
//cout << "diff: " << diff << endl;
if (diff == 1) break;
ii cd = path[i+1];
cd.F -= bdx; cd.S -= bdy;
//cout << cd.F << " " << cd.S << endl;
if (c[cd.F][cd.S] == 1) for_mod.push_back(cd);
path[i] = cd;
}
for (ll i = x+y+1; w+h-2 >= i; i++){
ll diff = abs(path[i-1].F-path[i].F) + abs(path[i-1].S-path[i].S);
//cout << "diff: " << diff << endl;
if (diff == 1) break;
ii cd = path[i-1];
cd.F -= sdx; cd.S -= sdy;
//cout << cd.F << " " << cd.S << endl;
if (c[cd.F][cd.S] == 1) for_mod.push_back(cd);
path[i] = cd;
}
//cout << "reached\n";
for (auto i: for_mod){
add(i.F,i.S);
}
}
public:
Grid(ll _h, ll _w){
w = _w; h = _h;
c.assign(h,vi(w,0));
upper_path.resize(w+h-1);
left_path.resize(w+h-1);
for (ll i = 0; w+h-1 > i; i++){
upper_path[i] = ii(max(0,i-w+1),min(i,w-1));
left_path[i] = ii(min(i,h-1),max(0,i-h+1));
}
}
ll add (ll x, ll y){
//cout << "called: " << x << " " << y << endl;
if (left_path[x+y] == ii(x,y) && upper_path[x+y] == ii(x,y)) return 0;
c[x][y] = 1;
if (upper_path[x+y] == ii(x,y)) modify_path(x,y,upper_path, 1, -1);
if (left_path[x+y] == ii(x,y)) modify_path(x,y,left_path, -1, 1);
return 1;
}
void print(){
cout << "upper_path: \n";
for (auto i: upper_path){
cout << "(" << i.F << " , " << i.S << ") -> ";
}
cout << endl;
cout << "left_path:\n";
for (auto i: left_path){
cout << "(" << i.F << " , " << i.S << ") -> ";
}
cout << endl;
cout << "c:\n";
for (ll i = 0; h > i; i++){
for (ll j = 0; w > j; j++){
if (upper_path[i+j] == ii(i,j) || left_path[i+j] == ii(i,j)) cout << "* ";
else cout << c[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
};
int main(){
ll h,w;
cin >> h >> w;
Grid grid(h,w);
//grid.print();
for (ll i = 0; h > i; i++){
for (ll j = 0; w > j; j++){
ll x; cin >> x;
//cout << "added: " << i << " " << j << endl;
if (x) grid.add(i,j);
}
}
ll q; cin >> q;
//grid.print();
while (q--){
ll x, y; cin >> x >> y;
x--; y--;
cout << grid.add(x,y) << '\n';
//grid.print();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
6 ms |
300 KB |
Output is correct |
3 |
Correct |
8 ms |
304 KB |
Output is correct |
4 |
Correct |
15 ms |
428 KB |
Output is correct |
5 |
Correct |
15 ms |
432 KB |
Output is correct |
6 |
Correct |
17 ms |
468 KB |
Output is correct |
7 |
Correct |
17 ms |
484 KB |
Output is correct |
8 |
Correct |
17 ms |
460 KB |
Output is correct |
9 |
Correct |
16 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
6 ms |
300 KB |
Output is correct |
3 |
Correct |
8 ms |
304 KB |
Output is correct |
4 |
Correct |
15 ms |
428 KB |
Output is correct |
5 |
Correct |
15 ms |
432 KB |
Output is correct |
6 |
Correct |
17 ms |
468 KB |
Output is correct |
7 |
Correct |
17 ms |
484 KB |
Output is correct |
8 |
Correct |
17 ms |
460 KB |
Output is correct |
9 |
Correct |
16 ms |
460 KB |
Output is correct |
10 |
Correct |
45 ms |
980 KB |
Output is correct |
11 |
Correct |
11 ms |
332 KB |
Output is correct |
12 |
Correct |
589 ms |
12752 KB |
Output is correct |
13 |
Correct |
146 ms |
8992 KB |
Output is correct |
14 |
Correct |
1431 ms |
17304 KB |
Output is correct |
15 |
Correct |
1501 ms |
16964 KB |
Output is correct |
16 |
Correct |
1617 ms |
18232 KB |
Output is correct |
17 |
Correct |
1696 ms |
19136 KB |
Output is correct |
18 |
Correct |
1650 ms |
19008 KB |
Output is correct |
19 |
Correct |
1758 ms |
19752 KB |
Output is correct |
20 |
Correct |
1706 ms |
20012 KB |
Output is correct |
21 |
Correct |
1779 ms |
19900 KB |
Output is correct |