zoukankan      html  css  js  c++  java
  • P2846 [USACO08NOV]Light Switching G(动态开点写法)

    Archie

    这叫啥啊,区间异或+区间求和

    我用动态开点写的

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #define ll long long
    using namespace std;
    int n,m;
    int x,y,z;
    int lazy[4000001];
    int tr[4000001];
    int ls[4000001];
    int rs[400001];
    int ct;
    void pushdown(int x,int l,int r){
    	if(lazy[x]){
    		if(!ls[x]) ls[x]=++ct;
    		if(!rs[x]) rs[x]=++ct; 
    		lazy[ls[x]]^=1;
    		lazy[rs[x]]^=1;
    		int mid=(l+r)>>1;
    		tr[ls[x]]=(mid-l+1-tr[ls[x]]);
    		tr[rs[x]]=(r-mid-tr[rs[x]]);
    		lazy[x]^=1;
    	}
    }
    void pushup(int r){
    	tr[r]=tr[ls[r]]+tr[rs[r]];
    }
    void chan(int &root,int L,int R,int l,int r){
    	if(!root) root=++ct;
    	if(l<=L&&R<=r){
    		lazy[root]^=1;
    		tr[root]=(R-L+1-tr[root]);
    		return ;
    	}
    	int mid=(L+R)>>1;
    	pushdown(root,L,R);
    	if(l<=mid) chan(ls[root],L,mid,l,r);
    	if(r>mid)  chan(rs[root],mid+1,R,l,r);
    	pushup(root);
    }
    int  query(int root,int L,int R,int l,int r){
    	if(!root) return 0;
    	if(l<=L&&R<=r){
    		return tr[root];
    	}
    	pushdown(root,L,R);
    	int mid=(L+R)>>1;
    	ll ans=0;
    	if(l<=mid) ans+=query(ls[root],L,mid,l,r);
    	if(r>mid) ans+=query(rs[root],mid+1,R,l,r) ;
    	return ans;
    }
    int root=0;
    int main(){
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=m;++i){
    		scanf("%d%d%d",&x,&y,&z);
    		if(x==0){
    			chan(root,1,n,y,z);
    		}else{
    			printf("%d
    ",query(1,1,n,y,z));
    		}
    	}
    	return 0;
    } 
    
  • 相关阅读:
    Nginx--sub_filter模块
    Scala学习5 objext
    Scala学习4 Class
    Scala学习3 Map
    Scala学习2 Array、ArrayBuffer
    Scala学习1 基本数据类型,条件,循环
    flink1.9新特性:维表Join解读
    Blink源码编译
    Flink Maven项目兼容多版本Kafka
    Ant build.xml程序简单说明
  • 原文地址:https://www.cnblogs.com/For-Miku/p/15026902.html
Copyright © 2011-2022 走看看