Your task is counting the segments of different colors you can see at last.
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.
<b< dd="">
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.
<b< dd="">
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
<b< dd="">
1 1
2 1
3 1
1 1
0 2
1 1
題目比較簡單,但是要注意染色是一定要每次都pushdown,還有pushdown每次都要先看是不是-1,上上篇博客做對比吧,都是染色問題
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=8000+5;
int sum[maxn<<2],num[maxn<<2];
int aa;
void pushdown(int rt)
{
if(sum[rt]!=-1)
{
sum[rt<<1]=sum[rt<<1|1]=sum[rt];
sum[rt]=-1;
}
}
void update(int L,int R,int C,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
sum[rt]=C;
return ;
}
int m=(l+r)>>1;
pushdown(rt);
if(L<=m)
update(L,R,C,l,m,rt<<1);
if(R>m)
update(L,R,C,m+1,r,rt<<1|1);
}
void query(int l,int r,int rt)
{
if(l==r)
{
if(sum[rt]!=-1&&sum[rt]!=aa)
{
// printf("**\n");
num[sum[rt]]++;
}
aa=sum[rt];
return ;
}
int m=(r+l)>>1;
pushdown(rt);
query(l,m,rt<<1);
query(m+1,r,rt<<1|1);
}
int main()
{
int n,L,R,C;
while(~scanf("%d",&n))
{
memset(sum,-1,sizeof(sum));
memset(num,0,sizeof(num));
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&L,&R,&C);
update(L+1,R,C,1,8000,1);
}
aa=-1;
query(1,8000,1);
for(int i=0;i<=8000;i++)
{
if(num[i])
printf("%d %d\n",i,num[i]);
}
printf("\n");
}
}