Codeforces 869E The Untended Antiquity

题意:给定一个网格图,三种操作:1.在(r1,c1,r2,c2)处建围墙。2.删除(r1,c1,r2,c2)处的围墙。3.询问两点是否可达

思路比较巧妙,将围墙内的点赋加一个权值,询问的时候判断两个点的权是否相等即可。显然可以用二维树状数组实现。

#include<bits/stdc++.h>
using namespace std;
#define MAXN 2500+10
typedef long long LL;
const int seed=2333;
LL sum[MAXN][MAXN],n,m,q;
int lowbit(int x){return  x&(-x);}
void add(int x,int y,LL val){
    for(int i=x;i <= n;i+=lowbit(i))
        for(int j=y;j <= m;j+=lowbit(j))
            sum[i][j]+=val;
}
void update(int x1,int y1,int x2,int y2,LL val){
    add(x1,y1,val);
    add(x1,y2+1,-val);
    add(x2+1,y1,-val);
    add(x2+1,y2+1,val);
}
LL query(int x,int y){
    LL tot=0;
    for(int i=x;i;i-=lowbit(i))
        for(int j=y;j;j-=lowbit(j))
            tot+=sum[i][j];
    return tot;
}
int main(){
    scanf("%d%d%d",&n,&m,&q);
    while(q--){
        int opt,x1,y1,x2,y2;
        scanf("%d%d%d%d%d",&opt,&x1,&y1,&x2,&y2);
        LL val=x1;
        val=val*seed+y1;
        val=val*seed+x2;
        val=val*seed+y2;
        if(opt==1)update(x1,y1,x2,y2,val);
        else if(opt==2)update(x1,y1,x2,y2,-val);
        else{
            LL tmp1=query(x1,y1);
            LL tmp2=query(x2,y2);
            puts(tmp1==tmp2?"Yes":"No");
        } 
    }
    return 0;
}
原文地址:https://www.cnblogs.com/NINGLONG/p/7674181.html