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
用线段树去更新区间(lazy),更新之后去遍历整个树结构,到叶子节点的时候把数值记录下来,然后算一下等值区间是有多少个,输出就可以。
#include<bits/stdc++.h> using namespace std; int const M=8e4+10; int save[M],tree[4*M]; int jl[M]; bool lazy[4*M]; void update(int now,int l,int r,int al,int ar,int val) { if(al>r||ar<l) { return ; } if(al<=l&&ar>=r) { lazy[now]=1; tree[now]=val; return ; } int mid=(l+r)/2; if(lazy[now]) { lazy[now]=0; lazy[now*2+1]=lazy[now*2+2]=1; tree[now*2+1]=tree[now*2+2]=tree[now]; } update(now*2+1,l,mid,al,ar,val); update(now*2+2,mid+1,r,al,ar,val); } void query(int now,int l,int r) { if(l==r) { save[l]=tree[now]; return ; } if(lazy[now]) { lazy[now]=0; lazy[now*2+1]=lazy[now*2+2]=1; tree[now*2+1]=tree[now*2+2]=tree[now]; } int mid=(r+l)/2; query(now*2+1,l,mid); query(now*2+2,mid+1,r); } int main() { int n; while(scanf("%d",&n)!=EOF) { int a,b,c; memset(tree,-1,sizeof(tree)); memset(jl,0,sizeof(jl)); int maxx=-1; int minn=0x3f3f3f3f; int mac=-1; for(int i=0; i<n; i++) { scanf("%d%d%d",&a,&b,&c); b--; update(0,0,8000,a,b,c); if(b>maxx) maxx=b; if(a<minn) minn=a; if(c>mac) mac=c; } query(0,0,8000); save[maxx+1]=-1; for(int i=minn; i<=maxx; i++) { if(save[i]!=save[i+1]&&save[i]!=-1) { jl[save[i]]++; // printf("%d\n",save[i]); } } for(int i=0; i<=mac; i++) { if(jl[i]>0) printf("%d %d\n",i,jl[i]); } putchar('\n'); } } 最后说一下这个题可以暴力水过去- -,毕竟到底输入多少组,而且暴力过时间也就100多ms,水水水水水------- 就不说了,按照他说的去做就行,更新区间直接一个一个更新就可以。