Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit Status Practice ZOJ 1610
Description
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
题意是区间涂色,最后问你能看见的有颜色,以及他们的段数,
真的是一道很不错的题, 这里最小的是长度为一的区间,而不是一个点,还有再次用到延迟标记,
统计段数就是对树的遍历,同时tmp记录下当前节点的先驱结点的颜色,用于判断,颜色的段数
//#include<bits/stdc++.h> #include<iostream> #include<cstring> #include<string> #include<cstdlib> #include<cstdio> #include<map> #include<algorithm> using namespace std; const int maxn=8011; const int inf=999999999; #define lson (rt<<1),L,M #define rson (rt<<1|1),M,R #define M ((L+R)>>1) #define For(i,n) for(int i=0;i<(n);i++) typedef long long LL; struct node { int color;//-1 表示区间没有涂色, -2表示区间涂的是混合色 } p[maxn<<3]; void build(int rt,int L,int R) { p[rt].color=-1; if(R-L==1)return ; build(lson); build(rson); } int cnt[maxn]; void update(int rt,int L,int R,int x,int y,int color) { if(L==R||p[rt].color==color)return ; if(L==x&&y==R) { p[rt].color=color; return ; } if(p[rt].color>=0) { p[rt<<1].color=p[rt<<1|1].color=p[rt].color; p[rt].color=-2; } if(L<=x&&y<=M) update(lson,x,y,color); else if(x>=M&&y<=R) update(rson,x,y,color); else { update(lson,x,M,color); update(rson,M,y,color); } p[rt].color=-2; } void solve(int rt,int L,int R,int& tmp) { if(p[rt].color==-1) { tmp=-1; return ; } if(p[rt].color>=0) { if(tmp!=p[rt].color) { tmp=p[rt].color; cnt[p[rt].color]++; } return ; } if(L+1!=R) { solve(lson,tmp); solve(rson,tmp); } } int main() { int n; // freopen("in.txt","r",stdin); while(~scanf("%d",&n)) { build(1,0,8000); int M1=0; For(i,n) { int left,right,color; scanf("%d%d%d",&left,&right,&color); update(1,0,8000,left,right,color); M1=max(M1,color); } memset(cnt,0,sizeof(cnt)); int tmp=-1; solve(1,0,8000,tmp); for(int i=0; i<=M1; i++)if(cnt[i]) printf("%d %d\n",i,cnt[i]); printf("\n"); } return 0; }