POJ 2155 2维线段树 || 2维BIT

Posted on 2016-06-09 23:11  yyjxx2010xyu  阅读(129)  评论(0编辑  收藏  举报
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stack>
#define mp make_pair
#define pa pair<int,int>
#define pb push_back
#define fi first
#define se second
using namespace std;
inline void Get_Int(int &X)
{
    X=0;  char ch=getchar(); int f=1;
    while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    while (ch>='0' && ch<='9') {X=X*10+ch-'0'; ch=getchar();} X*=f;
}
inline void Put_Int(int X)
{
    char ch[20]; int top=0;
    if (X==0) ch[++top]='0';
    while (X) ch[++top]=X%10+'0',X/=10;
    while (top) putchar(ch[top--]); putchar('\n');
}
//=======================================
const int Maxn=4010;
int x1,x2,y1,y2,Ans;
bool Key[Maxn][Maxn];
int n,Q,Kase;
void Modify_Y(int ox,int oy,int l,int r,int p,int q)
{
    if (l==p && r==q) 
    { 
        Key[ox][oy]^=1;
        return;
    }
    int mid=(l+r)>>1;
    if (q<=mid) Modify_Y(ox,oy<<1,l,mid,p,q);
    if (p>=mid+1) Modify_Y(ox,oy<<1|1,mid+1,r,p,q);
    if (p<=mid && q>=mid+1) Modify_Y(ox,oy<<1,l,mid,p,mid),Modify_Y(ox,oy<<1|1,mid+1,r,mid+1,q);

}
void Modify_X(int ox,int l,int r,int p,int q)
{
    if (l==p && r==q) 
    {
        Modify_Y(ox,1,1,n,y1,y2);
        return;
    }
    int mid=(l+r)>>1;
    if (q<=mid) Modify_X(ox<<1,l,mid,p,q); 
    if (p>=mid+1) Modify_X(ox<<1|1,mid+1,r,p,q); 
    if (p<=mid && q>=mid+1) Modify_X(ox<<1,l,mid,p,mid),Modify_X(ox<<1|1,mid+1,r,mid+1,q);
}

void Query_Y(int ox,int oy,int l,int r)
{
    Ans^=Key[ox][oy];
    if (l==r) return;
    int mid=(l+r)>>1;
    if (y1<=mid) Query_Y(ox,oy<<1,l,mid);
    if (y1>=mid+1) Query_Y(ox,oy<<1|1,mid+1,r);
}
void Query_X(int ox,int l,int r)
{
    Query_Y(ox,1,1,n);
    if (l==r) return;
    int mid=(l+r)>>1;
    if (x1<=mid) Query_X(ox<<1,l,mid);
    if (x1>=mid+1) Query_X(ox<<1|1,mid+1,r);
}
int main()
{
    Get_Int(Kase);
    for (int kase=1;kase<=Kase;kase++)
    {
        Get_Int(n),Get_Int(Q);
        memset(Key,false,sizeof(Key));
        for (int i=1;i<=Q;i++)
        {
            char ch=getchar(); while (ch!='C' && ch!='Q') ch=getchar();
            if (ch=='C') 
            {
                Get_Int(x1),Get_Int(y1),Get_Int(x2),Get_Int(y2);
                Modify_X(1,1,n,x1,x2);
            }
            if (ch=='Q') 
            {
                Get_Int(x1),Get_Int(y1);
                Ans=0;
                Query_X(1,1,n);
                Put_Int(Ans);
            }
        }
        putchar('\n');
    }
    return 0;
}
线段树
 1 #include <cstring>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <iostream>
 5 using namespace std;
 6 const int Maxn=2010;
 7 int n,Q,Kase,c[Maxn][Maxn],x1,x2,y1,y2;
 8 inline int lowbit(int x) {return x&(-x);}
 9 inline void Modify(int x,int y)
10 {
11     for (int i=x;i<=n;i+=lowbit(i))
12         for (int j=y;j<=n;j+=lowbit(j)) c[i][j]^=1;
13 }
14 inline int Query(int x,int y)
15 {
16     int ret=0;
17     for (int i=x;i;i-=lowbit(i))
18         for (int j=y;j;j-=lowbit(j)) ret^=c[i][j];
19     return ret;
20 }
21 int main()
22 {
23     // freopen("c.in","r",stdin);
24     scanf("%d",&Kase);
25     for (int kase=1;kase<=Kase;kase++)
26     {
27         scanf("%d%d",&n,&Q);
28         memset(c,0,sizeof(c));
29         for (int i=1;i<=Q;i++)
30         {
31             char ch=getchar(); while (ch!='C' && ch!='Q') ch=getchar();
32             if (ch=='C') 
33             {
34                 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
35                 Modify(x1,y1),Modify(x1,y2+1),Modify(x2+1,y1),Modify(x2+1,y2+1);
36             }
37             if (ch=='Q') 
38             {
39                 scanf("%d%d",&x1,&y1);
40                 printf("%d\n",Query(x1,y1));
41             }
42         }
43         puts("");
44     }
45     return 0;
46 }
BIT