//https://oj.uz/problem/view/JOI20_furniture
#define here cerr<<"===========================================\n"
#include <bits/stdc++.h>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
using namespace std;
#define maxn 1005
ll n,m;
ll a[maxn][maxn];
bool vis[maxn][maxn];
set<pll> s[2*maxn];
void dfs(ll x,ll y){
vis[x][y] = 1;
if(s[x+y].find({x,y})!=s[x+y].end()) s[x+y].erase(s[x+y].find({x,y}));
if(vis[x+1][y-1]){
if(!vis[x+1][y]) dfs(x+1,y);
if(!vis[x][y-1]) dfs(x,y-1);
}
if(vis[x-1][y+1]){
if(!vis[x-1][y]) dfs(x-1,y);
if(!vis[x][y+1]) dfs(x,y+1);
}
return;
}
int main(){
daj_mi_malo_vremena
cin >> n >> m;
for(ll i = 0;i<=n+1;i++) for(ll j = 0;j<=m+1;j++) vis[i][j] = 1;
for(ll i = 1;i<=n;i++){
for(ll j = 1;j<=m;j++){
cin >> a[i][j];
if(a[i][j]==0){
s[i+j].insert({i,j});
vis[i][j] = 0;
}
}
}
for(ll i = 1;i<=n;i++){
for(ll j = 1;j<=m;j++){
if(i+j==2||i+j==n+m) continue;
if(!vis[i][j]&&((vis[i-1][j]&&vis[i][j-1])||(vis[i+1][j]&&vis[i][j+1]))) dfs(i,j);
}
}
ll q; cin >> q;
while(q--){
here;
ll x,y; cin >> x>> y;
for(ll i = 0;i<=n;i++) ceri(vis[i],0,m);
if(vis[x][y]||sz(s[x+y])==1){
cout<<0<<endl;
vis[x][y] = 1;
}else{
cout<<1<<endl;
dfs(x,y);
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5078 ms |
5728 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5078 ms |
5728 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |