临时文档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 .
原文地址:https://www.cnblogs.com/france/p/4808700.html