zoukankan      html  css  js  c++  java
  • 临时文档3

    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define lson l , m , rt << 1
    #define rson m + 1 , r , rt << 1 | 1
    #define LL long long
    const int maxn = 111111;
    struct Tree{
    
    	int type;
    	bool po;
    }tree[maxn<<2];
    
    void PushDown(int rt) {
    	if (tree[rt].type) {
    		tree[rt<<1].type += tree[rt].type;
    		tree[rt<<1|1].type += tree[rt].type ;
    		tree[rt].type = 0;
    	}
    	tree[rt].po=true;
    }
    void build(int l,int r,int rt) {
    	tree[rt].po=false;
    	tree[rt].type=0;
    	if (l == r){
    		return ;
    	}
    	int m = (l + r) >> 1;
    	build(lson);
    	build(rson);
    }
    void update(int L,int R,int c,int l,int r,int rt) {//(L,R)是我们要找的范围
    	if (L <= l && r <= R) {//标记1  //更新到这就行,不一定更新到最底端
    		tree[rt].type += c;
    	//	tree[rt].value += c;
    		return ;
    	}
    	PushDown(rt );//延迟标记,这时候后推, (标记2)
    	int m = (l + r) >> 1;
    	if (L <= m) update(L , R , c , lson);
    	if (R > m) update(L , R , c , rson);
    }
    void newupdate(int L,int R,int l,int r,int rt){
    	bool flag=false;
    	if(tree[rt].type>0&&tree[rt].po){
    		if(rt==6)cout<<"-=helloworld"<<endl;
    		PushDown(rt);
    		flag=true;
    	}
    	if(flag){
    		int m = (l + r) >> 1;   // 而如果你是要更新[1,1](必定访问到标记2) 那么先进行标记2 让两儿子的value更新
    		if (L <= m) newupdate(L , R , lson); //记得这时候儿子type被赋值,自己type=0;
    		if (R > m) newupdate(L , R , rson);
    	}
    }
    void query(int L,int R,int l,int r,int rt) {
    	if(tree[rt].type>0){
    		for(int i=l;i<=r;i++){
    			cout<<tree[rt].type<<" ";
    		}
    	}
    	else if(l==r&&tree[rt].type==0)cout<<"0";
    	else{
        int m = (l + r) >> 1;
        if (L <= m) query(L , R , lson);
        if (R > m) query(L , R , rson);
    	}
    }
    int main() {
    	int n;int a,b;
    	while(cin>>n){
    		if(n==0)break;
    		 build( 1, n, 1);
    		for(int i=1;i<=n;i++){
    			 cin>>a>>b;
    			 update(a,b,1,1,n,1);
    
    		 }
    		int i;
    		for( i=1;i<=15;i++)
    			cout<<"第"<<i<<"=="<<tree[i].type<<endl;
    		 newupdate(1,n,1,n,1);
    		 cout<<"-=-=-"<<tree[6].po<<endl;
    		 for( i=1;i<=15;i++)
    		 			cout<<"第"<<i<<"=="<<tree[i].type<<endl;
    		 query(1,n,1,n,1);
    		 for( i=1;i<=15;i++)
    		 			cout<<"第"<<i<<"=="<<tree[i].type<<endl;
    	}
    	return 0;
    }
    /*
     *
     8
    1 3
    4 5
    6 7
    1 7
    2 6
    5 5
    4 7
    1 2
    6
    1 2
    3 4
    1 4
    2 4
    3 5
    2 2
    2 4 4 1 1 0
     */
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    today lazy . tomorrow die .
  • 相关阅读:
    Hash 函数资源链接汇总
    Zookeeper 初体验之——伪分布式安装
    Zookeeper 初体验之——JAVA API 初探
    布隆过滤器(Bloom Filter)详解
    7天学会Maven(第一天——了解 Maven)
    仿中关村在线首页弹出式广告插件(jQuery版)
    介绍几款在线代码编辑器
    兼容浏览 firefox、chrome、ie 的flash(swf) 的代码!
    我的Discuz!X2 、Ucenter 1.6、ASP.NET 应用程序整合经历
    2011年最后一博:仿HAO123的邮箱登录
  • 原文地址:https://www.cnblogs.com/france/p/4808700.html
Copyright © 2011-2022 走看看