Time Limit: 2 Seconds Memory Limit: 65536 KB
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,3]涂颜色1,[4,7]涂颜色1。正确答案是1 2。如果是对区间内的点涂色,则会显示两个区间之间没有间隔,答案会是1 1,如下图所示:
如果是对区间线段涂色,则会显示两个区间之间有间隔,答案会是1 2,如下图所示:
所以下面在调用updata函数时 updata(a+1,b,c,1,8000,1) 其中要a+1。
这道题其实和另一道张贴海报的题目有些类似,但是这道题数据不大,不需要离散化。
这是那道题:https://blog.youkuaiyun.com/sinat_41233888/article/details/81508132
#include <iostream>
#include <stdio.h>
#include <string.h>
#define maxn 8000+10
using namespace std;
int vis[maxn<<2],num[maxn<<2],last;
void pushdown(int rt)
{
if(vis[rt]!=-1)
{
vis[rt<<1]=vis[rt<<1|1]=vis[rt];
vis[rt]=-1;
}
}
void updata(int L,int R,int C,int l,int r,int rt)
{
if(L<=l&&R>=r){
vis[rt]=C;
return ;
}
pushdown(rt);
int m=(l+r)>>1;
if(L<=m)updata(L,R,C,l,m,rt<<1);
if(R>m)updata(L,R,C,m+1,r,rt<<1|1);
}
void query(int l,int r,int rt)
{
if(l==r){
if(vis[rt]!=-1&&vis[rt]!=last)
num[vis[rt]]++;
last=vis[rt];
return;
}
pushdown(rt);
int m=(l+r)>>1;
query(l,m,rt<<1);
query(m+1,r,rt<<1|1);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int a,b,c;
memset(vis,-1,sizeof(vis));
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(a<b)updata(a+1,b,c,1,8000,1);
}
int last=-1;
query(1,8000,1);
for(int i=0;i<=8000;i++)
if(num[i])printf("%d %d\n",i,num[i]);
printf("\n");
}
return 0;
}