XOR的艺术

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=2e5+5;
int n,m;

struct E_tree{
    
#define ls(p) p<<1
#define rs(p) p<<1|1
    int sum0[N<<2],sum1[N<<2],lz[N<<2];

    void push_up(int p){
        sum0[p]=sum0[ls(p)]+sum0[rs(p)];
        sum1[p]=sum1[ls(p)]+sum1[rs(p)];
    }
    void ad(int p){
        swap(sum0[p],sum1[p]);
        lz[p]^=1;
    }
    void push_down(int p){
        if(!lz[p])return;
        ad(ls(p));
        ad(rs(p));
        lz[p]=0;
    }
    void build(int p,int l,int r){
        if(l==r){
            int k;
            scanf("%1d",&k);
            if(k)sum1[p]=1;
            else sum0[p]=1;
            return;
        }
        int mid=l+r>>1;
        build(ls(p),l,mid);
        build(rs(p),mid+1,r);
        push_up(p);
    }
    void add(int p,int l,int r,int x,int y){
        if(l>y||r<x)return;
        if(x<=l&&r<=y){
            ad(p);
            return;
        }
        push_down(p);
        int mid=l+r>>1;
        add(ls(p),l,mid,x,y);
        add(rs(p),mid+1,r,x,y);
        push_up(p);
    }
    int ask(int p,int l,int r,int x,int y){
        if(l>y||r<x)return 0;
        if(x<=l&&r<=y)return sum1[p];
        push_down(p);
        int mid=l+r>>1;
        return ask(ls(p),l,mid,x,y)+ask(rs(p),mid+1,r,x,y);
    }
}T;
int main()
{
    
    scanf("%d%d",&n,&m);
    
    T.build(1,1,n);
    
    int o,x,y;
    
    while(m--)
    {
        scanf("%d%d%d",&o,&x,&y);
        if(o==1)
        printf("%d\n",T.ask(1,1,n,x,y));
        else T.add(1,1,n,x,y);
    }
    return 0;
}

 

posted @ 2020-07-05 15:52  头顶凉风  阅读(109)  评论(0编辑  收藏  举报