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次涂色,涂色区间为
l
l
到r,让你按编号输出不连续颜色块的数量,没有这个编号就不输出
121是1有2个,2有1个
112是1有1个,2有1个
题解:
这道题需要按边存,那么我们就可以假设0-1是0,1-2是1,那么更新的范围就是到
r−1
r
−
1
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
#define maxn 8005
int num[maxn*4],flag[maxn*4];
void pushdown(int root)
{
if(flag[root]==-1)
return ;
num[root<<1]=flag[root];
num[root<<1|1]=flag[root];
flag[root<<1]=flag[root];
flag[root<<1|1]=flag[root];
flag[root]=-1;
}
void update(int l,int r,int root,int ql,int qr,int val)
{
if(l>=ql&&r<=qr)
{
num[root]=flag[root]=val;
return ;
}
pushdown(root);
int mid=l+r>>1;
if(mid>=ql)
update(l,mid,root<<1,ql,qr,val);
if(mid<qr)
update(mid+1,r,root<<1|1,ql,qr,val);
}
int ans[8005],link[8005];
void query(int l,int r,int root)
{
if(l==r)
{
if(num[root]!=-1)
link[l]=num[root];
return ;
}
pushdown(root);
int mid=l+r>>1;
query(l,mid,root<<1);
query(mid+1,r,root<<1|1);
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(link,-1,sizeof(link));
memset(flag,-1,sizeof(flag));
memset(num,-1,sizeof(num));
memset(ans,0,sizeof(ans));
int l,r,val;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&l,&r,&val);
update(0,8000,1,l,r-1,val);
}
query(0,8000,1);
/*for(int i=0;i<=n;i++)
cout<<link[i]<<" ";*/
if(link[0]!=-1)
ans[link[0]]++;
for(int i=1;i<=8000;i++)
if(link[i]!=link[i-1])
ans[link[i]]++;
for(int i=0;i<=8000;i++)
{
if(!ans[i])
continue;
printf("%d %d\n",i,ans[i]);
}
printf("\n");
}
return 0;
}