Count the Colors-ZOJ1610(线段树区间求)

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

题目大意:

在一条线上花颜色,求出最后看到的颜色种类

分析:

因为从0开始,所以应该用区间

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
using namespace std;
#define N 8005
#define Lson r<<1
#define Rson r<<1|1
int sum[N],temp; struct node
{
int L,R,e;
int mid()
{
return (L+R)/;
}
int len()
{
return R-L+;
}
}a[N*]; void BuildTree(int r,int L,int R)
{
a[r].L=L;
a[r].R=R;
a[r].e=-;
if(L==R-)
{
return;
}
BuildTree(Lson,L,a[r].mid());
BuildTree(Rson,a[r].mid(),R);
}
void Qurry(int r)
{
if(a[r].e!=-)
{
if(a[r].e!=temp)
{//temp表示这个区间的颜色没有被加过
sum[a[r].e]++;
}
temp=a[r].e;
return;
}
if(a[r].L==a[r].R-)
{
temp=-;
return;
}
Qurry(Lson);
Qurry(Rson);
}
void Update(int r,int L,int R,int e)
{ if(a[r].L==L && a[r].R==R)
{
a[r].e=e;
return;
}
if(a[r].e!=-)
{
a[Lson].e=a[Rson].e=a[r].e;
a[r].e=-;
}
if(R<=a[r].mid())
Update(Lson,L,R,e);
else if(L>=a[r].mid())
Update(Rson,L,R,e);
else
{
Update(Lson,L,a[r].mid(),e);
Update(Rson,a[r].mid(),R,e);
}
}
int main()
{
int n,x,y,e;
while(scanf("%d",&n)!=EOF)
{
memset(sum,,sizeof(sum));
BuildTree(,,N);
int Max=;
for(int i=;i<n;i++)
{
scanf("%d %d %d",&x,&y,&e);
Max=max(Max,e);
Update(,x,y,e); }
temp=-;
Qurry();
for(int i=;i<=Max;i++)
{
if(sum[i])
{
printf("%d %d\n",i,sum[i]);
}
}
printf("\n");
}
return ;
}
上一篇:hdu 1754 I Hate It(线段树区间求最值)


下一篇:C语言笔记(二维数组与数值指针)