2021牛客暑期多校训练营8 F-Robots(bitset优化dp)

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值