Poj2155Matrix二维线段树

题意: c表示矩阵内01 互换,q表示单点查询。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>

using namespace std;

const int maxn = 1000+5;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int n;
char color[maxn<<2][maxn<<2];
 void inbuild(int root,int l,int r,int rt)
 {
     color[root][rt] = 0;
     if(l==r) return ;
     int mid=(l+r)>>1;
     inbuild(root,lson);
     inbuild(root,rson);
 }

 void build(int l,int r,int rt)
 {
     inbuild(rt,1,n,1);
     if(l==r) return ;
     int mid=(l+r)>>1;
     build(lson);
     build(rson);
 }



 void down(int root,int L,int R,int l,int r,int rt)
 {
     if(color[root][rt]){
       // color[root][rt<<1]^=1; color[root][rt<<1|1]^=1;
        color[root<<1][rt]^=1; color[root<<1|1][rt]^=1;
        color[root][rt] =0;
     }
     if(L<=l&&r<=R) return ;
     int mid=(l+r)>>1;
     if(L<=mid) down(root,L,R,lson);
     if(R>mid) down(root,L,R,rson);

 }



 void inupdate(int root,int L,int R,int l,int r,int rt)
 {
     if(L<=l&&r<=R){
        color[root][rt]^=1;return ;
     }
     int mid=(l+r)>>1;
     if(color[root][rt]){
        color[root][rt<<1] ^=1;color[root][rt<<1|1] ^=1;
        color[root][rt] =0;
     }
     if(L<=mid) inupdate(root,L,R,lson);
     if(R>mid) inupdate(root,L,R,rson);
 }


void update(int L,int R,int S,int X,int l,int r,int rt)
{
    if(L<=l&&r<=R){
        inupdate(rt,S,X,1,n,1);return ;
    }
    down(rt,S,X,1,n,1);
    int mid=(l+r)>>1;
    if(L<=mid) update(L,R,S,X,lson);
    if(R>mid) update(L,R,S,X,rson);
}

int inask(int root,int key,int l,int r,int rt)
{
    if(l==r) return color[root][rt];
    int mid=(l+r)>>1;
    if(color[root][rt]){
        color[root][rt<<1] ^=1;
        color[root][rt<<1|1] ^=1;
        color[root][rt] = 0;
    }
    if(key<=mid) return inask(root,key,lson);
    else return inask(root,key,rson);
}

int ask(int L,int S,int l,int r,int rt)
{
    if(l==r){
        return inask(rt,S,1,n,1);
    }
    int mid=(l+r)>>1;
    down(rt,S,S,1,n,1);
    if(L<=mid) return ask(L,S,lson);
    else return ask(L,S,rson);
}

int main()
{
    int T,m;
    char str[10];
    int a,b,c,d;
    cin>>T;
    while(T--){
        cin>>n>>m;
        build(1,n,1);
        for(int i = 0;i<m;i++){
            scanf("%s",str);
            if(str[0]=='C'){
                scanf("%d%d%d%d",&a,&b,&c,&d);
                update(a,c,b,d,1,n,1);
            }
            else {
                scanf("%d%d",&a,&b);
                printf("%d
",ask(a,b,1,n,1));
            }
        }
        if(T) cout<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/yigexigua/p/4162655.html