Count the Colors
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
恩,题目大意就是说,染色,之后染的颜色会掩盖住之前的颜色,然后问最后可以看到多少种颜色,每种颜色可以看到几次(不连续)。这里,每次染色的是区间而不是端点,例如 [ 1 , 2 ] 和 [ 2 , 3 ]这两个区间是有间隔的,我处理的是更新时将前一个端点+1 后一个端点-1也是可以的吧。因为数据范围我改了将近一个小时。。。。题目中给8000然而我写8000并不行, zoj 里的错误提示貌似和其他地方还不太一样。
#include <iostream>
#include<cstdio>
#include<cstring>
#define maxn 10000
using namespace std;
struct lnode
{
int l,r,c;
};
lnode node[maxn<<2];
int cor[maxn],rec[maxn];
void pushup(int o)
{
if(node[o<<1].c==node[o<<1|1].c&&node[o<<1].c!=-1)
node[o].c=node[o<<1].c;
}
void pushdown(int o)
{
if(node[o].c!=-1)
{
node[o<<1].c=node[o<<1|1].c=node[o].c;
node[o].c=-1;
}
}
void build(int o,int l,int r)
{
node[o].l=l;
node[o].r=r;
node[o].c=-1;
if(l==r)
return ;
int mid=(l+r)>>1;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
pushup(o);
}
void update(int o,int l,int r,int d)
{
if(node[o].c==d)
return ;
if(node[o].l==l&&node[o].r==r)
{
node[o].c=d;
return ;
}
pushdown(o);
int mid=(node[o].l+node[o].r)>>1;
if(r<=mid)
update(o<<1,l,r,d);
else if(l>mid)
update(o<<1|1,l,r,d);
else
{
update(o<<1,l,mid,d);
update(o<<1|1,mid+1,r,d);
}
pushup(o);
}
void query(int o)
{
if(node[o].c!=-1)
{
for(int i=node[o].l;i<=node[o].r;++i)
cor[i]=node[o].c;
}
if(node[o].l==node[o].r)
return ;
pushdown(o);
query(o<<1);
query(o<<1|1);
}
void sum()
{
for(int i=0;i<maxn;++i)
{
if(cor[i]!=-1&&cor[i]!=cor[i+1])
rec[cor[i]]++;
}
}
int main()
{
int n,a,b,c;
while(~scanf("%d",&n))
{
memset(cor,-1,sizeof(cor));
memset(rec,0,sizeof(rec));
build(1,1,maxn);
while(n--)
{
scanf("%d%d%d",&a,&b,&c);
update(1,a+1,b,c);
}
query(1);
sum();
for(int i=0;i<maxn;++i)
{
if(rec[i])
printf("%d %d\n",i,rec[i]);
}
printf("\n");
}
return 0;
}