hdu5892 -Resident Evil 二维树状数组 + 状态压缩

来源:互联网 发布:js拆分字符串 编辑:程序博客网 时间:2024/05/18 00:21

题目大意:有50种动物,给你n*n的矩阵,m次操作,P代表加入操作,在左上角 x1,y1 到右下角 x2,y2,的矩形范围内加入种类为x,数量为y的动物。

     Q代表询问操作,在左上角 x1,y1 到右下角 x2,y2,对于1~50种动物,如果数量之和为偶数,输出1,否则输出2。



思路:
把50种东西压缩成一个longlong,每一位代表一种,对于数量,我们只需要知道奇偶,也即该位是0或1.
用二维树状数组每次对一个子矩阵进行异或更新与查询,复杂度4*nlognlogn


#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <set>#include <map>#include <vector>#include <iostream>using namespace std;const double pi=acos(-1.0);#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1typedef long long ll;int n,m;const int maxn=3000+5;int s;struct tree{    ll num[2][2][maxn][maxn];    inline    int lowbit(int x)    {        return x&-x;    }    void add(int x,int y,ll a)    {        int i,j;        for (i=x; i<=s; i+=lowbit(i))            for (j=y; j<=s; j+=lowbit(j))                num[x&1][y&1][i][j]^=a;    }    ll query(int x,int y)    {        ll ans=0,i,j;        for (i=x; i>0; i-=lowbit(i))            for (j=y; j>0; j-=lowbit(j))                ans^=num[x&1][y&1][i][j];        return ans;    }};tree tp;int main(){    char op[5];    int k,A,B;    int l,b,r,t;    ll val;    int n,m;    scanf("%d%d",&n,&m);    s=n;    for (int i=1; i<=m; i++)    {        scanf("%s",op);        scanf("%d%d%d%d",&l,&b,&r,&t);        if (op[0]=='P')        {            scanf("%d",&k);            ll val=0;            while(k--)            {                scanf("%d%d",&A,&B);                A--;                if (B&1)                    val^=1LL<<A;            }            tp.add(l,b,val);            tp.add(r+1,b,val);            tp.add(l,t+1,val);            tp.add(r+1,t+1,val);        }        else        {            ll ret=tp.query(r,t);            ret^=tp.query(l-1,t);            ret^=tp.query(r,b-1);            ret^=tp.query(l-1,b-1);            for (int j=0; j<50; j++)            {                if ((1LL<<j)&ret)                    printf("2 ");                else printf("1 ");            }            printf("\n");        }    }    return 0;}


0 0