段树:延迟标志+暴力更新
我记得刚学段树做的时候这个话题WA一个版本。。。。。如今,每分钟获得。。。。
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
Author: Standlove
Source: ZOJ Monthly, May 2003
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=8200; int color[maxn],cov[maxn<<2],ans[maxn]; void init() { memset(color,-1,sizeof(color)); memset(ans,0,sizeof(ans)); memset(cov,-1,sizeof(cov)); } void push_down(int l,int r,int rt) { if(cov[rt]!=-1) { cov[rt<<1]=cov[rt<<1|1]=cov[rt]; if(l==r) { color[l]=cov[rt]; } cov[rt]=-1; } } void update(int L,int R,int c,int l,int r,int rt) { if(L<=l&&R>=r) { cov[rt]=c; return ; } push_down(l,r,rt); int m=(l+r)/2; if(L<=m) update(L,R,c,lson); if(R>m) update(L,R,c,rson); } void debug(int l,int r,int rt) { cout<<rt<<": "<<l<<" <---> "<<r<<" color: "<<color[rt]<<" cov: "<<cov[rt]<<endl; if(l==r) return ; int m=(l+r)/2; debug(lson); debug(rson); } void over_tree(int l,int r,int rt) { push_down(l,r,rt); if(l==r) return ; int m=(l+r)/2; over_tree(lson); over_tree(rson); } int n; int main() { while(scanf("%d",&n)!=EOF) { init(); while(n--) { int a,b,c; scanf("%d%d%d",&a,&b,&c); update(a,b-1,c,0,8100,1); } over_tree(0,8100,1); int last=-1; for(int i=0;i<8100;i++) { if(last==color[i]) continue; else { last=color[i]; if(last!=-1) { ans[last]++; } } } for(int i=0;i<=8000;i++) { if(ans[i]) { printf("%d %d ",i,ans[i]); } } putchar(10); } return 0; }
版权声明:来自: 代码代码猿猿AC路 http://blog.csdn.net/ck_boss