F-Robots
第一种第二种机器人直接 O ( n ) O(n) O(n)判断即可。
第三种机器人暴力dp,用bitset优化。
bitset<250005> b[i][j]
表示从
(
i
,
j
)
(i,j)
(i,j)能到达哪些点,第三种机器人向右向下转移即可。
状态压缩!
时间复杂度 O ( q n + n 4 w ) O(qn+\frac{n^4}{w}) O(qn+wn4)
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template <class T=int> T rd()
{
T res=0;T fg=1;
char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') fg=-1;ch=getchar();}
while( isdigit(ch)) res=(res<<1)+(res<<3)+(ch^48),ch=getchar();
return res*fg;
}
vector<pair<int,int> > q[505][505];
char s[505][505];
int ans[500005];
int n,m;
bitset<250005> b[2][505];
int main()
{
n=rd(),m=rd();
for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
int qc=rd();
for(int k=1;k<=qc;k++)
{
int op=rd(),x=rd(),y=rd(),a=rd(),b=rd();
if(op==1)
{
for(int i=x;i<=n;i++)
{
if(s[i][y]=='1') break;
if(i==a&&y==b) ans[k]=1;
}
}
else if(op==2)
{
for(int j=y;j<=m;j++)
{
if(s[x][j]=='1') break;
if(x==a&&j==b) ans[k]=1;
}
}
else q[x][y].emplace_back(k,(a-1)*m+b);
}
// b[i][j]=(i,j)|b[i+1][j]|b[i][j+1];
for(int i=n;i>=1;i--)
for(int j=m;j>=1;j--)
{
b[i&1][j].reset();
if(s[i][j]=='1') continue;
b[i&1][j]=b[i+1&1][j]|b[i&1][j+1];
b[i&1][j].set((i-1)*m+j);
for(pair<int,int> u:q[i][j]) ans[u.first]=b[i&1][j][u.second];
}
for(int i=1;i<=qc;i++) puts((ans[i])?"yes":"no");
return 0;
}