首页 > 代码库 > POJ 1610 Count the Colors
POJ 1610 Count the Colors
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
线段树区间更新,一段一段更新,并不是点
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath> #include <vector> #include <algorithm> using namespace std; #define lowbit(x) (x&(-x)) #define max(x,y) (x>y?x:y) #define min(x,y) (x<y?x:y) #define mem(a) (memset(a,0,sizeof(a))) typedef long long ll; int vis[80006]; int li,ri,w; int pos[80006]; int ans[40006]; void pushdown(int node) { if(vis[node]!=-1) { vis[node<<1]=vis[node]; vis[node<<1|1]=vis[node]; vis[node]=-1; } } void update(int ll,int rr,int val,int l,int r,int node) { if(ll<=l && rr>=r) { vis[node]=val; return ; } pushdown(node); int mid=(l+r)>>1; if(ll<=mid) update(ll,rr,val,l,mid,node<<1); if(rr>mid) update(ll,rr,val,mid+1,r,node<<1|1); } void query(int l,int r,int node) { if(vis[node]>=0) { for(int i=l;i<=r;i++) ans[i]=vis[node]; return ; } if(vis[node]==-1 && l!=r) { int mid=(l+r)>>1; query(l,mid,node<<1); query(mid+1,r,node<<1|1); } } int main() { int m; while(scanf("%d",&m)!=EOF) { memset(vis,-1,sizeof(vis)); memset(pos,0,sizeof(pos)); memset(ans,-1,sizeof(ans)); for(int i=0;i<m;i++) { scanf("%d%d%d",&li,&ri,&w); if(li>=ri) continue; update(li+1,ri,w,1,8000,1); } query(1,8000,1); int i=1; while(i<8001) { int coo=ans[i],j=i+1; if(coo==-1){i++;continue;} while(j<8001 && ans[j]==coo && ans[j]!=-1) { j++; } pos[coo]++; i=j; } for(int i=0;i<8001;i++) { if(pos[i]) printf("%d %d\n",i,pos[i]); } printf("\n"); } return 0; }
POJ 1610 Count the Colors