zoukankan      html  css  js  c++  java
  • 【luogu2574】xor的艺术

    一道无聊的线段树题,写着玩玩而已……

    #include<bits/stdc++.h>
    #define N 1000010
    #define lson (o<<1)
    #define rson (o<<1|1)
    using namespace std;
    int n,m,a[N];
    struct Segment_Tree{
        int sumv[N<<2],xorv[N<<2],lenv[N];
        inline void pushup(int o){sumv[o]=sumv[lson]+sumv[rson];}
        inline void pushdown(int o){
            if(!xorv[o])return;
            xorv[o]=0;
            xorv[lson]=!xorv[lson];xorv[rson]=!xorv[rson];
            sumv[lson]=lenv[lson]-sumv[lson];
            sumv[rson]=lenv[rson]-sumv[rson];
        }
        void build(int o,int l,int r){
            lenv[o]=r-l+1;
            if(l==r){sumv[o]=a[l];return;}
            int mid=(l+r)>>1;
            build(lson,l,mid);build(rson,mid+1,r);
            pushup(o);
        }
        void change(int o,int l,int r,int ql,int qr){
            if(ql<=l&&r<=qr){
                xorv[o]=!xorv[o];sumv[o]=lenv[o]-sumv[o];
                return;
            }
            int mid=(l+r)>>1;pushdown(o);
            if(ql<=mid)change(lson,l,mid,ql,qr);
            if(qr>mid)change(rson,mid+1,r,ql,qr);
            pushup(o);
        }
        int querysum(int o,int l,int r,int ql,int qr){
            if(ql<=l&&r<=qr)return sumv[o];
            int mid=(l+r)>>1,ans=0;pushdown(o);
            if(ql<=mid)ans+=querysum(lson,l,mid,ql,qr);
            if(qr>mid)ans+=querysum(rson,mid+1,r,ql,qr);
            return ans;
        }
    }T;
    inline int read(){
        int f=1,x=0;char ch;
        do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
        do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
        return f*x;
    }
    int main(){
        n=read();m=read();
        for(int i=1;i<=n;++i)scanf("%1d",&a[i]);
        T.build(1,1,n);
        while(m--){
            int opt=read(),l=read(),r=read();
            if(opt==0)T.change(1,1,n,l,r);
            else printf("%d
    ",T.querysum(1,1,n,l,r));
        }
        return 0;
    }
     
  • 相关阅读:
    Spring的声明试事务
    spring-AOP-添加日志
    弹窗插件
    工厂设计模式
    smartUpload组件批量下载
    简单的C++委托 —— 用模板类实现类成员函数的回调
    偷Microsoft师学MFC艺:且看C++如何支持反射
    C++中回调(CallBack)的使用方法
    epoll 使用实例
    C++成员函数指针的应用
  • 原文地址:https://www.cnblogs.com/zcysky/p/6853765.html
Copyright © 2011-2022 走看看