[哈希+差分] Codeforces 869E. The Untended Antiquity

其实很简单的题,然而之前题意读错了……
我们怎么判断是否存在一个矩形,覆盖了一个点而没覆盖另一个点呢?实际上只要做个矩形覆盖就行了,若两个点上覆盖的矩形集合不同,则说明两点不可达。
我们可以对每个矩形,对应一个哈希值,进行矩形加。然后询问时判断两点的值是否相同即可。
矩形加,我们可以差分,然后二维树状数组实现。

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn=2515;
typedef unsigned LL;
typedef unsigned long long uLL;
int n,m,Q,_test;
uLL rdm[maxn];
LL bit[maxn][maxn];
void Updata(int _x,int _y,int val){
    for(int x=_x;x<=n;x+=(x&(-x)))
     for(int y=_y;y<=m;y+=(y&(-y))) bit[x][y]+=val;
}
LL Query(int _x,int _y){
    LL res=0;
    for(int x=_x;x;x-=(x&(-x)))
     for(int y=_y;y;y-=(y&(-y))) res+=bit[x][y];    
    return res;
}
int main(){
    freopen("E.in","r",stdin);
    freopen("E.out","w",stdout);
    srand(2333); for(int i=1;i<=2505;i++) rdm[i]=(rand()<<15)+rand(), rdm[i]+=(rdm[i]<<30)+(rand()<<15)+rand();
    scanf("%d%d%d",&n,&m,&Q); n++; m++;
    while(Q--){
        int _type,x,y,_x,_y; scanf("%d%d%d%d%d",&_type,&x,&y,&_x,&_y);
        int hsh=(rdm[x]*13+rdm[y]*53+rdm[_x]*17+rdm[_y]*15)%1000000007;
        if(_type==1) Updata(x,y,hsh), Updata(x,_y+1,-hsh), Updata(_x+1,y,-hsh), Updata(_x+1,_y+1,hsh); else
        if(_type==2) Updata(x,y,-hsh), Updata(x,_y+1,hsh), Updata(_x+1,y,hsh), Updata(_x+1,_y+1,-hsh); else
        if(Query(x,y)==Query(_x,_y)) printf("Yes\n"); else printf("No\n");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值