zoukankan      html  css  js  c++  java
  • BZOJ 1230 Usaco2008 Nov 开关灯 线段树

    思路:
    用线段树模拟题中的操作就好
    (标记异或 长度=区间总长度-当前已开灯的长度)

    //By SiriusRen
    #include <cstdio>
    using namespace std;
    #define N 666666
    int n,m,op,xx,yy,tree[N],lazy[N];
    void push_down(int num,int pos){
        int lson=pos<<1,rson=pos<<1|1;
        tree[lson]=num-(num>>1)-tree[lson];
        tree[rson]=(num>>1)-tree[rson];
        lazy[lson]=!lazy[lson];
        lazy[rson]=!lazy[rson];
        lazy[pos]=0;
    }
    void insert(int l,int r,int pos){
        if(l>=xx&&r<=yy){
            tree[pos]=r-l+1-tree[pos];
            lazy[pos]=!lazy[pos];
            return;
        }
        if(lazy[pos])push_down(r-l+1,pos);
        int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
        if(mid<xx)insert(mid+1,r,rson);
        else if(mid>=yy)insert(l,mid,lson);
        else insert(l,mid,lson),insert(mid+1,r,rson);
        tree[pos]=tree[lson]+tree[rson];
    }
    int query(int l,int r,int pos){
        if(l>=xx&&r<=yy)return tree[pos];
        if(lazy[pos])push_down(r-l+1,pos);
        int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1;
        if(mid<xx)return query(mid+1,r,rson);
        else if(mid>=yy)return query(l,mid,lson);
        else return query(l,mid,lson)+query(mid+1,r,rson);
    }
    int main(){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&op,&xx,&yy);
            if(!op)insert(1,n,1);
            else printf("%d
    ",query(1,n,1));
        }
    }

    这里写图片描述

  • 相关阅读:
    <meta>标签常用内容
    CentOS8 yum方式安装mysql8.0
    xshell上传下载文件
    CentOS8查看防火墙状态,开启/关闭防火墙
    CentOS8 yum方式安装nginx1.8
    Ant下载与配置
    List集合的方法总结
    List集合的三个实现类比较
    List集合遍历的三种方法
    JAVA如何跳出多层循环
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6532257.html
Copyright © 2011-2022 走看看