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< style="background-color: transparent; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; box-shadow: none; font-family: "Merriweather",serif; font-size: 18px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" 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< style="background-color: transparent; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; box-shadow: none; font-family: "Merriweather",serif; font-size: 18px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" 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< style="background-color: transparent; border-bottom-left-radius: 0px; border-bottom-right-radius: 0px; border-top-left-radius: 0px; border-top-right-radius: 0px; box-shadow: none; font-family: "Merriweather",serif; font-size: 18px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" dd="">
1 1
2 1
3 1
1 1
0 2
1 1
坑点:线段更新,不是点更新,所以更新的时候应该是x+1到y。
#include<algorithm>
#include<string.h>
#include<stdio.h>
#define M 8010
using namespace std;
struct tree
{
int l,r,v,d;
} T[4*M];
int a[M],ans[M];
void down(int k)
{
T[2*k].v=T[k].d;
T[2*k+1].v=T[2*k].d=T[2*k+1].d=T[k].d;
T[k].d=0;
}
void creat_tree(int l,int r,int k)
{
T[k].l=l,T[k].r=r,T[k].d=0,T[k].v=-1;
if(l==r)
{
return ;
}
int mid=(l+r)>>1;
creat_tree(l,mid,2*k);
creat_tree(mid+1,r,2*k+1);
}
void insert_tree(int l,int r,int k,int x)
{
if(l==T[k].l&&r==T[k].r)
{
T[k].d=x;
T[k].v=x;
return ;
}
if(T[k].d) down(k);
int mid=(T[k].l+T[k].r)>>1;
if(r<=mid) insert_tree(l,r,2*k,x);
else if(l>mid) insert_tree(l,r,2*k+1,x);
else
{
insert_tree(l,mid,2*k,x);
insert_tree(mid+1,r,2*k+1,x);
}
if(T[2*k].v!=T[2*k+1].v) T[k].v=0;
else T[k].v=T[2*k].v;
}
void query_tree(int k)
{
if(T[k].v)
{
for(int i=T[k].l; i<=T[k].r; i++)
{
a[i]=T[k].v;
}
return ;
}
if(T[k].d) down(k);
query_tree(2*k);
query_tree(2*k+1);
}
int main()
{
int n,l,r,x;
int m=8005;
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
memset(ans,0,sizeof(ans));
a[0]=-1;
creat_tree(1,m,1);
while(n--)
{
scanf("%d%d%d",&l,&r,&x);
insert_tree(l+1,r,1,x+1);
}
query_tree(1);
for(int i=1; i<=m; i++)
{
if(a[i]!=a[i-1]&&a[i]>0)
ans[a[i]]++;
}
for(int i=1; i<=m; i++)
{
if(ans[i])
printf("%d %d\n",i-1,ans[i]);
}
printf("\n");
}
}