poj2155-二维线段树(树套树)

//先确定x线段树再确定y线段树

#include
#include
int n,ans,x1,x2,y1,y2;
struct node{
    int l,r;
    struct node1{
        int l,r,s;
    }treey[4010];
}treex[4010];
void home2(int l,int r,int d,int p){
    treex[p].treey[d].l=l;
    treex[p].treey[d].r=r;
    treex[p].treey[d].s=0;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    home2(l,mid,d<<1,p);
    home2(mid+1,r,d<<1|1,p);
}
void home1(int l,int r,int d){
    treex[d].l=l;
    treex[d].r=r;
    home2(1,n,1,d);
    if(l==r)
        return;
    int mid=(l+r)>>1;
    home1(l,mid,d<<1);
    home1(mid+1,r,d<<1|1);
}
void change2(int d,int p){
    
    if(treex[p].treey[d].l>y2 || treex[p].treey[d].r=y1 && treex[p].treey[d].r<=y2){
        treex[p].treey[d].s=1-treex[p].treey[d].s;
        return;
    }
    int mid=(treex[p].treey[d].l+treex[p].treey[d].r)>>1;
    if(y2<=mid)change2(d<<1,p);
    else if(y1>mid)change2(d<<1|1,p);
    else{
        change2(d<<1,p);
        change2(d<<1|1,p);
    }
}
void change1(int d){
    if(treex[d].l>x2 || treex[d].r=x1 && treex[d].r<=x2){
        change2(1,d);
        return;
    }
    int mid=(treex[d].l+treex[d].r)>>1;
    if(x2<=mid)change1(d<<1);
    else if(x1>mid)change1(d<<1|1);
    else{
        change1(d<<1);
        change1(d<<1|1);
    }
}
void find2(int d,int p){
    
    ans+=treex[p].treey[d].s;
    ans%=2;
    if(treex[p].treey[d].l==treex[p].treey[d].r)
        return;
    int mid=(treex[p].treey[d].l+treex[p].treey[d].r)>>1;
    if(y1<=mid)
        find2(d<<1,p);
    else 
        find2(d<<1|1,p);
}
void find1(int d){
    find2(1,d);
    if(treex[d].l==treex[d].r)
        return;
    int mid=(treex[d].l+treex[d].r)>>1;
    if(x1<=mid)
        find1(d<<1);
    else 
        find1(d<<1|1);
}
int main(){
    int t,m;
    char c;
    scanf("%d",&t);
    while(t){
        scanf("%d%d",&n,&m);
        home1(1,n,1);
        getchar();
        while(m){
            scanf("%c",&c);
            getchar();
            if(c=='C'){
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                change1(1);
            }
            else {
                ans=0;
                scanf("%d%d",&x1,&y1);
                find1(1);
                printf("%d\n",ans);
            }
            getchar();
            m--;
        }
        printf("\n");
        t--;
    }
    return 0;
}


你可能感兴趣的:(poj2155-二维线段树(树套树))