您的位置:首页 > 其它

ZOJ----1610-Count the Colors(线段树)

2015-11-10 18:55 471 查看
C - Count the Colors
Crawling in process...
Crawling failed
Time Limit:2000MS
Memory Limit:65536KB 64bit IO Format:%lld & %llu

Submit

Status

Description
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

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define SIZE 8005
int temp;
int sum_c[SIZE];
struct Node
{
int a,b;
int col;
} node[SIZE<<2];
void in_sert(int a,int b,int c,int i)
{
if(a==b)return;
if(node[i].col==c)return;
if(node[i].a>=a && node[i].b<=b)
{
node[i].col=c;
return;
}
if(node[i].col>=0)
{
node[i<<1].col=node[i].col;
node[i<<1|1].col=node[i].col;
}
node[i].col=-2;
int mid=(node[i].a+node[i].b)>>1;
if(a>=mid)in_sert(a,b,c,i<<1|1);
else if(b<=mid)in_sert(a,b,c,i<<1);
else
{
in_sert(a,mid,c,i<<1);
in_sert(mid,b,c,i<<1|1);
}
}
void build_tree(int a,int b,int i)
{
node[i].a=a;
node[i].b=b;
node[i].col=-1;
if(a+1==b)return;
int mid=(a+b)>>1;
build_tree(a,mid,i<<1);
build_tree(mid,b,i<<1|1);
}
void query(int i)
{
if(node[i].col==-1)
{
temp=-1;
}
else if(node[i].col>=0)
{
if(node[i].col!=temp)
{
sum_c[ node[i].col ]++;
temp=node[i].col;
}
}
else if(node[i].a+1 != node[i].b)
{
query(i<<1);
query(i<<1|1);
}
return;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
build_tree(0,8000,1);
int i,j,la,lb,c;
for(i=0; i<n; ++i)
{
scanf("%d%d%d",&la,&lb,&c);
in_sert(la,lb,c,1);
}
temp=-1;
memset(sum_c,0,sizeof(sum_c));
query(1);
for(j=0; j<=SIZE; ++j)
{
if(sum_c[j]>0)
printf("%d %d\n",j,sum_c[j]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: