HDU 1556Color the ball

题意:线段树水题,区间更新,带上lazy标记即可;

 

#include<iostream>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<stack>
#include<cstring>
#include<cstdio>
#define N 100005
#define INF 0x3f3f3f3f
using namespace std;
typedef struct node{
	int x;int y;int date;
}node;
typedef struct list{
	int x;int y;
}list;
node a[4*N];
list c[N];
int b[N];
void built(int root,int first,int end){
	if(first==end){
		a[root].x=first;a[root].y=end;a[root].date=0;
		return ;
	}
	int mid=(first+end)/2;
	built(root*2,first,mid);
	built(root*2+1,mid+1,end);
	a[root].x=a[root*2].x;a[root].y=a[root*2+1].y;a[root].date=0;
}
void U(int root,int first,int end,int l,int r){
	if(l<=first&&end<=r){
		a[root].date++;
		return ;
	}
	int mid=(first+end)/2;
	if(a[root].date!=0){
		a[root*2].date+=a[root].date;
		a[root*2+1].date+=a[root].date;
		a[root].date=0;
	}
	if(l<=mid)  U(2*root,first,mid,l,r);
	if(r>mid)   U(2*root+1,mid+1,end,l,r);
}
void Q(int root,int first,int end){
	if(first==end){
		b[first]=a[root].date;
		return ;
	}
	if(a[root].date!=0){
		a[root*2].date+=a[root].date;
		a[root*2+1].date+=a[root].date;
		a[root].date=0;
	}
	int mid=(first+end)/2;
	Q(2*root,first,mid);
	Q(2*root+1,mid+1,end);
}
int main(){
	int n;
	while(scanf("%d",&n)==1&&n!=0){
		memset(b,0,sizeof(b));
		for(int i=1;i<=n;i++){
			scanf("%d %d",&c[i].x,&c[i].y);
		}
		built(1,1,n);
		for(int i=1;i<=n;i++){
			U(1,1,n,c[i].x,c[i].y);
		}
		Q(1,1,n);
		for(int i=1;i<=n;i++){
			if(i==1){
				printf("%d",b[i]);
			}
			else{
				printf(" %d",b[i]);
			}
		}
		printf("\n");
	}
	return 0;
}


 

posted @ 2017-10-03 20:25  wang9897  阅读(72)  评论(0编辑  收藏  举报