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,8000]的数轴,有n次操作,每次操作给left到right的区间染成数字代号为C的颜色。最后输出在数轴上出现的颜色,且这种颜色覆盖的区间有几个。
最大值8000,数值很小,可以直接暴力,代码如下:
4103878 | 2015-11-07 21:27:23 | Accepted | 1610 | C++ | 110 | 232 | 纯真 |
#include<cstdio>
#include<cstring>
int colors[8010],a[8010];
int main()
{
int n,i,right,left,cnt;
while(scanf("%d",&n)!=EOF)
{
memset(a,-1,sizeof(a));
memset(colors,0,sizeof(colors));
int max=-1;
int top=-1;
while(n--)
{
scanf("%d%d%d",&right,&left,&cnt);
if(left>max)
max=left;
if(cnt>top)
top=cnt;
for(i=right;i<left;++i)
a[i]=cnt;
}
colors[a[0]]=1;
for(i=1;i<max;++i)
{
if(a[i]!=a[i-1]&&a[i]!=-1)
colors[a[i]]++;
}
for(i=0;i<=top;++i)
{
if(colors[i]!=0)
printf("%d %d\n",i,colors[i]);
}
printf("\n");
}
return 0;
}
很明显也能用线段树解,线段树还是不懂啊,说好的重看还没看,QAQ,接下来又是招新,又是校赛,还感冒了,头昏昏的,难受得要命,进度又慢下来了。小小矫情一下。
代码如下:
#include<cstdio>
#include<cstring>
#define maxn 8010
struct node
{
int left,right,color;
}num[maxn*3];
int colors[maxn];
void build(int left,int right,int cnt)
{
num[cnt].left=left;
num[cnt].right=right;
num[cnt].color=-1;//color 等于 -1 表示什么色都没有
if(left==right)
return ;
int mid=(left+right)>>1;
build(left,mid,cnt*2);
build(mid+1,right,cnt*2+1);
}
void pushdown(int cnt)
{
num[cnt*2].color=num[cnt].color;
num[cnt*2+1].color=num[cnt].color;
num[cnt].color=-1;
}
void update(int left,int right,int cnt,int val)
{
if(num[cnt].color==val)
return ;
if(left<=num[cnt].left&&num[cnt].right<=right)
{
num[cnt].color=val;
return ;
}
if(num[cnt].color!=-1)
pushdown(cnt);
int mid=(num[cnt].left+num[cnt].right)>>1;
if(right<=mid)
update(left,right,cnt*2,val);
else if(left>mid)
update(left,right,cnt*2+1,val);
else
{
update(left,mid,cnt*2,val);
update(mid+1,right,cnt*2+1,val);
}
if(num[cnt*2].color==num[cnt*2+1].color&&(num[cnt*2].color!=-1))//递归回来,若左右相同,修改父结点颜色
num[cnt].color=num[cnt*2].color;
}
void query(int cnt)
{
int i;
if(num[cnt].color!=-1)
{
for(i=num[cnt].left;i<=num[cnt].right;++i)
colors[i]=num[cnt].color;
return ;
}
if(num[cnt].left==num[cnt].right)
return ;
query(cnt*2);
query(cnt*2+1);
}
int main()
{
int n,x,y,val,i;
int ans[maxn];
while(scanf("%d",&n)!=EOF)
{
memset(colors,-1,sizeof(colors));
build(0,maxn,1);
while(n--)
{
scanf("%d%d%d",&x,&y,&val);
if(x==y)
continue;
update(x,y-1,1,val);//注意后面一个不要涂色,是涂区间
}
query(1);
memset(ans,0,sizeof(ans));
if(colors[0]!=-1)
ans[colors[0]]=1;
for(i=1;i<maxn;++i)
{
if(colors[i]!=colors[i-1]&&colors[i]!=-1)
ans[colors[i]]++;
}
for(i=0;i<maxn;++i)
{
if(ans[i]!=0)
printf("%d %d\n",i,ans[i]);
}
printf("\n");
}
return 0;
}