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.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces: 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.
If some color can't be seen, you shouldn't print it. Print a blank line after every dataset.
1 1 0 2 题意:给出在给的区间里填色,最后计算有多少每种颜色有几个连续的区间; |
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=8005;
int color[N*4];
int ans[N*4];
int n;
int main()
{
while(~scanf("%d",&n))
{
memset(color,-1,sizeof(color));
memset(ans,0,sizeof(ans));
int j,i,a,b,c,temp;
int x=-1,y=-1;//x表示区间最右端,y表示颜色的最大值
for(i=0;i<n;i++)
{
scanf("%d %d %d",&a,&b,&c);
x=max(x,b);
y=max(y,c);
for(j=a+1;j<=b;j++)
color[j]=c;
}
for(i=0;i<x;i++)
{
if(color[i]!=color[i+1]&&color[i]>=0)
ans[color[i]]++;
}
ans[color[x]]++;
for(i=0;i<=y;i++)
{
if(ans[i])
printf("%d %d\n",i,ans[i]);
}
printf("\n");//注意格式,每组加换行
}
return 0;
}