[杂题 DP] Codeforces 869C. The Intriguing Obsession

很明显,我们不可能连接到相同颜色的边。
这样的路径的长度:colorA -> colorB -> color C -> colorA,不能超过3,所以我们只需要考虑:
colorA - >颜色B - >颜色A
对于每一点,我们不能把它与相同颜色的另外两个点连起来。
现在它变得简单。A和B、B和C、A和C之间的问题是相同的,相互独立的。
我使用O(n ^ 2)DP方案之间的两种颜色的数量。实际上它可以在O(n)中得到解。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值