ZOJ 1610Count the Colors

题意:给定一些操作,不断更新一些区间的颜色,求最后间断每种颜色的个数,直接线段树处理,每次更新,加上lazy标记,注意如何处理区间的端点是这题的关键;

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


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