码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ 1610 Count the Colors(线段树区间更新)

时间:2015-03-21 17:10:10      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:线段树

Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.

Your task is counting the segments of different colors you can see at last.


Input

The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.

Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:

x1 x2 c

x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.

All the numbers are in the range [0, 8000], and they are all integers.

Input may contain several data set, process to the end of file.


Output

Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.

If some color can‘t be seen, you shouldn‘t print it.

Print a blank line after every dataset.


Sample Input

5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1


Sample Output

1 1
2 1
3 1

1 1

0 2
1 1


题意: n个操作,每次把一段区间更新,最后统计每一种颜色的段数




#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
//typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 8005

struct stud{
int le,ri;
int va;
}f[N*4];

int n,k;
int color[N];

struct studd{
int le,ri;
int va;
}ans[N];

inline void pushdown(int pos)
{
     f[L(pos)].va=f[R(pos)].va=f[pos].va;
     f[pos].va=-1;
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].va=-1;
	if(le==ri) return ;

	int mid=MID(le,ri);

	build(L(pos),le,mid);
	build(R(pos),mid+1,ri);
}

void update(int pos,int le,int ri,int va)
{

   if(f[pos].le==le&&f[pos].ri==ri)
   {
      f[pos].va=va;
   	  return ;
   }

   if(f[pos].va!=-1)
	  pushdown(pos);

   int mid=MID(f[pos].le,f[pos].ri);

   if(mid>=ri)
	update(L(pos),le,ri,va);
   else
	if(mid<le)
	 update(R(pos),le,ri,va);
   else
   {
   	 update(L(pos),le,mid,va);
   	 update(R(pos),mid+1,ri,va);
   }

	if(f[L(pos)].va==f[R(pos)].va&&f[L(pos)].va!=-1)
		f[pos].va=f[pos].va;
}

void query(int pos)
{
	if(f[pos].va!=-1)
	{
		ans[k].le=f[pos].le;
		ans[k].ri=f[pos].ri;
		ans[k++].va=f[pos].va;
		return ;
	}

    if(f[pos].le==f[pos].ri) return ;

    query(L(pos));
    query(R(pos));
}

int main()
{
	int i,j;
	while(~sf(n))
	{

	   build(1,0,N);
       int le,ri,va;

       fre(i,0,n)
       {
       	  sfff(le,ri,va);
       	  update(1,le,ri-1,va);
       }
	k=0;

	query(1);

    mem(color,0);

    color[ans[0].va]++;

    fre(i,1,k)
      if(ans[i].va!=ans[i-1].va||ans[i-1].ri+1<ans[i].le)
		color[ans[i].va]++;

	fre(i,0,N)
	 if(color[i])
		pf("%d %d\n",i,color[i]);

	pf("\n");
	}
	return 0;
}




ZOJ 1610 Count the Colors(线段树区间更新)

标签:线段树

原文地址:http://blog.csdn.net/u014737310/article/details/44516541

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!