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));
        }
    }

    这里写图片描述

  • 相关阅读:
    [Java] [Exception]
    [Go back to REDIS]
    [Java] [内存泄露]
    [ZK] [Related Materials]
    [Scala] [Coursera]
    <zk在大型分布式系统中的应用>
    [Java] [Lock] [Synchronized VS ReentrantLock]
    [Data Structure] Tree
    投影矩阵的计算过程
    SQL Server 2012
  • 原文地址:https://www.cnblogs.com/SiriusRen/p/6532257.html
Copyright © 2011-2022 走看看