/*#pragma GCC optimize("Ofast")
#pragma GCC optimization("unroll-loops, no-stack-protector")
#pragma GCC target("avx,avx2,fma")*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
#define For(i,a,b) for (ll i=a;i<=b;i++)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
const ll maxn=1e3+200;
const ll mod=1e9+7 ;
const ll base=3e18;
/// you will be the best but now you just are trash
/// goal 3/7
ll cnt[maxn][maxn];
ll cnt1[maxn][maxn];
bool dd[maxn][maxn];
ll ax[5]= {0,1,0,-1};
ll ay[5]= {1,0,-1,0};
ll fwt[maxn][maxn][2];
ll a[maxn][maxn];
ll n, m;
void update(ll x,ll y,ll t,ll diff)
{
ll len=(t==0?m:n);
for (int i=y; i<=len; i+= i&(-i))
{
fwt[x][i][t]+=diff;
}
}
ll get(ll x,ll l,ll r,ll t)
{
ll ans=0;
for (int i=r;i;i-= i&(-i))
{
ans+=fwt[x][i][t];
}
for (int i=l-1;i;i-= i&(-i))
{
ans-=fwt[x][i][t];
}
return ans;
}
void add(ll x,ll y)
{
if (dd[x][y]) return ;
queue<pll> q;
q.push(make_pair(x,y));
dd[x][y]=1;
while (!q.empty())
{
auto p=q.front();
q.pop();
ll x=p.ff;
ll y=p.ss;
update(x,y,0,-1);
update(y,x,1,-1);
bool chk=(make_pair(x,y)==make_pair(1ll,3ll));
for (int t=0; t<4; t++)
{
ll x1=x+ax[t];
ll y1=y+ay[t];
if (x1>=1&&x1<=n&&y1>=1&&y1<=m&&!dd[x1][y1])
{
if (t<=1)
{
cnt[x1][y1]--;
if (!cnt[x1][y1])
{
dd[x1][y1]=true;
q.push(make_pair(x1,y1));
}
}
else
{
cnt1[x1][y1]--;
if (!cnt1[x1][y1])
{
dd[x1][y1]=true;
q.push(make_pair(x1,y1));
}
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen("t.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
cin>> n>> m;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
cin>>a[i][j];
for (int t=0; t<4; t++)
{
ll x1=i+ax[t];
ll y1=j+ay[t];
if (x1>=1&&x1<=n&&y1>=1&&y1<=m)
{
if (t<=1)
{
cnt1[i][j]++;
}
else
{
cnt[i][j]++;
}
}
}
update(i,j,0,1);
update(j,i,1,1);
}
}
for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
if (a[i][j]==1)
{
add(i,j);
}
}
}
/* for (int i=1; i<=n; i++)
{
for (int j=1; j<=m; j++)
{
cout <<dd[i][j]<<" ";
}
cout <<endl;
}*/
ll q;
cin>> q;
while (q--)
{
ll x, y;
cin>>x>> y;
bool chk=false;
if (x+1<=n&&y>=2&&get(x+1,1,y-1,0))
{
chk=true;
}
if (y+1<=m&&x>=2&&get(y+1,1,x-1,1))
{
chk=true;
}
if (chk)
{
add(x,y);
}
cout <<chk<<endl;
}
}
Compilation message
furniture.cpp: In function 'void add(long long int, long long int)':
furniture.cpp:69:14: warning: unused variable 'chk' [-Wunused-variable]
69 | bool chk=(make_pair(x,y)==make_pair(1ll,3ll));
| ^~~
furniture.cpp: In function 'int main()':
furniture.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
furniture.cpp:106:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
106 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1872 KB |
Output is correct |
2 |
Correct |
2 ms |
2260 KB |
Output is correct |
3 |
Correct |
3 ms |
2252 KB |
Output is correct |
4 |
Correct |
4 ms |
2384 KB |
Output is correct |
5 |
Correct |
5 ms |
2512 KB |
Output is correct |
6 |
Correct |
7 ms |
2576 KB |
Output is correct |
7 |
Correct |
4 ms |
2508 KB |
Output is correct |
8 |
Correct |
5 ms |
2512 KB |
Output is correct |
9 |
Correct |
6 ms |
2508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1872 KB |
Output is correct |
2 |
Correct |
2 ms |
2260 KB |
Output is correct |
3 |
Correct |
3 ms |
2252 KB |
Output is correct |
4 |
Correct |
4 ms |
2384 KB |
Output is correct |
5 |
Correct |
5 ms |
2512 KB |
Output is correct |
6 |
Correct |
7 ms |
2576 KB |
Output is correct |
7 |
Correct |
4 ms |
2508 KB |
Output is correct |
8 |
Correct |
5 ms |
2512 KB |
Output is correct |
9 |
Correct |
6 ms |
2508 KB |
Output is correct |
10 |
Correct |
23 ms |
7740 KB |
Output is correct |
11 |
Correct |
5 ms |
2388 KB |
Output is correct |
12 |
Correct |
326 ms |
52688 KB |
Output is correct |
13 |
Correct |
150 ms |
48076 KB |
Output is correct |
14 |
Correct |
533 ms |
55132 KB |
Output is correct |
15 |
Correct |
510 ms |
54544 KB |
Output is correct |
16 |
Correct |
464 ms |
56956 KB |
Output is correct |
17 |
Correct |
497 ms |
59484 KB |
Output is correct |
18 |
Correct |
476 ms |
57908 KB |
Output is correct |
19 |
Correct |
508 ms |
59980 KB |
Output is correct |
20 |
Correct |
508 ms |
59968 KB |
Output is correct |
21 |
Correct |
482 ms |
60000 KB |
Output is correct |