CF439E:The Untended Antiquity - 哈希 + 二维树状数组

Magic Door

题目大意

有一个n*m的网格,支持三中操作:
1.在x1,y1,x2,y2为顶点的矩形周围围上栅栏
2.将x1,y1,x2,y2为顶点的矩形周围的栅栏拆掉
3.询问x1,y1,x2,y2两点是否联通
保证栅栏矩形不相交

题目分析

因为栅栏的矩形互不相交,所以两点不连通时一定在不同的地域里。
因此可以将栅栏附上一个hash值,用二维树状数组维护前缀和,如果点查到的值相同则代表在同一个地域里。

code

#include<bits/stdc++.h>

using namespace std;

const int N = 2505;
int n, m, q;
typedef unsigned long long uint;
typedef pair<int, int> P;
map<P, uint> Map;

struct IO{
    inline int rint(){
        int i = 0, f = 1; char ch = getchar();
        for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
        if(ch == '-') f = -1, ch = getchar();
        for(; ch >= '0' && ch <= '9'; ch = getchar())
            i = (i << 3) + (i << 1) + (ch - '0');
        return i * f;
    }
    inline void wstr(string s){
        int len = s.length();
        for(int i = 0; i < len; i++) putchar(s[i]);
        putchar('\n');
    }
}io;

struct BIT{
    uint tree[N][N];
    inline void add(int x, int y, uint val){
        for(int i = x; i <= n; i += (i & -i))
            for(int j = y; j <= m; j += (j & -j))
                tree[i][j] += val;
    }
    inline uint query(int x, int y){
        uint ret = 0;
        for(int i = x; i; i -= (i & -i))
            for(int j = y; j; j -= (j & -j))
                ret += tree[i][j];
        return ret;
    }
}bit;

inline uint RandHash(){
    static uint RAND_VAL = 1388593021;
    return RAND_VAL += RAND_VAL << 2 | 1;
}

int main(){
    n = io.rint(), m = io.rint(), q = io.rint();
    for(int i = 1; i <= q; i++){
        int op = io.rint();
        int x1 = io.rint(), y1 = io.rint();
        int x2 = io.rint(), y2 = io.rint();
        switch(op){
            case 1:{
                uint hashVal = RandHash();
                bit.add(x1, y1, hashVal), bit.add(x1, y2 + 1, -hashVal), bit.add(x2 + 1, y1, -hashVal), bit.add(x2 + 1, y2 + 1, hashVal);
                Map[P((x1 - 1) * n + y1, (x2 - 1) * n + y2)] = hashVal;
                break;
            }
            case 2:{
                uint hashVal = Map[P((x1 - 1) * n + y1, (x2 - 1) * n + y2)];
                bit.add(x1, y1, -hashVal), bit.add(x1, y2 + 1, hashVal), bit.add(x2 + 1, y1, hashVal), bit.add(x2 + 1, y2 + 1, -hashVal);
                break;
            }
            case 3:{
                if(bit.query(x1, y1) == bit.query(x2, y2)) io.wstr("Yes");
                else io.wstr("No");
                break;
            }
        }
    }
    system("pause");
}
posted @ 2017-10-08 21:53  CzYoL  阅读(562)  评论(0编辑  收藏  举报