线段树(区间更新以及统计片段颜色)
2017-08-12 15:23
288 查看
ZOJ - 1610
题目:
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
代码:
题目:
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> using namespace std; const int N=8010; #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) struct node { int l,r; int flag; //flag µÈÓÚ -1 ±íʾʲôɫ¶¼Ã»ÓÐ }tree[N*3]; int color ; //¼Ç¼ÿ¿é°åÉϵÄÊÇʲôÑÕÉ« void build(int l,int r,int rt){ tree[rt].l=l; tree[rt].r=r; tree[rt].flag=-1; if(l==r) return ; int mid=(l+r)>>1; build(l,mid,L(rt)); build(mid+1,r,R(rt)); } void PushDown(int rt){ tree[L(rt)].flag=tree[rt].flag; tree[R(rt)].flag=tree[rt].flag; tree[rt].flag=-1; } void update(int val,int l,int r,int rt){ if(tree[rt].flag==val) return ; if(l<=tree[rt].l && tree[rt].r<=r) { tree[rt].flag=val; return ; } if(tree[rt].flag!=-1) //ÑÓ³Ù¸²¸Ç ²»È»¿ÉÄÜ»áTLE£¨×î½ü×öÒ»Ö±¶¼ÊÇÑÓ³Ù¸²¸Ç£© PushDown(rt); int mid=(tree[rt].l+tree[rt].r)>>1; if(r<=mid) update(val,l,r,L(rt)); else if(l>=mid+1) update(val,l,r,R(rt)); else{ update(val,l,r,L(rt)); update(val,l,r,R(rt)); } if((tree[L(rt)].flag==tree[R(rt)].flag) && (tree[L(rt)].flag!=-1)) //µÝ¹é»ØÀ´Ð޸ĸ¸½áµãÑÕÉ« tree[rt].flag=tree[L(rt)].flag; } void query(int rt) { if(tree[rt].flag!=-1) { for(int i=tree[rt].l;i<=tree[rt].r;i++) color[i]=tree[rt].flag; return ; } if(tree[rt].l==tree[rt].r) //ÕâÊDz»¼ÓÕâ¸ö¾¹È»»áÔËÐв»ÆðÀ´ return ; query(L(rt)); query(R(rt)); } void Solve(){ int res ; memset(res,0,sizeof(res)); int pre=-1; for(int i=0;i<N;i++) if(pre!=color[i]){ //ÑÕÉ«¿é²»ÏàÁ¬¾Í++ pre=color[i]; res[pre]++; } for(int i=0;i<N;i++) if(res[i]!=0) printf("%d %d\n",i,res[i]); printf("\n"); } int main(){ int n; while(~scanf("%d",&n)) { memset(color,-1,sizeof(color)); build(0,N,1); int x,y,val; while(n--) { scanf("%d%d%d",&x,&y,&val); if(x==y) continue; update(val,x,y-1,1); //ºóÃæÒ»¸ö²»Í¿¾ÍÄܱÜÃâÄÇÖÖÇé¿ö } query(1); Solve(); } return 0; }
相关文章推荐
- HDU 1698 线段树区间更新以及lazy思想
- poj 2528【线段树-区间更新,区间统计】
- 线段树判一段里面有几段不同的颜色以及更新操作
- C - Just a Hook(线段树)(区间一般更新以及求总和)
- hdu----(5023)A Corrupt Mayor's Performance Art(线段树区间更新以及区间查询)
- poj-----(2528)Mayor's posters(线段树区间更新及区间统计+离散化)
- hdu 5023(线段树区间染色,统计区间内颜色个数)
- 用线段树求逆序数。。。 突然发现自己的单点更新以及自己区间查询还是有点问题。,。
- 线段树区间更新,区间统计+离散化 POJ 2528 Mayor's posters
- 线段树区间更新,区间统计+离散化 POJ 2528 Mayor's posters
- 线段树 --- 单点修改 以及 区间更新中的单点更新
- 线段树之建树,单点更新以及区间查询
- 线段树 区间更新 区间求和以及最值
- 线段树区间更新,区间统计 poj 2777 Count Color
- poj 3468 线段树区间更新
- HDU 1698 Just a Hook(线段树区间更新)
- hdu 5239 区间平方 线段树区间更新
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
- hdu1166 敌兵布阵 线段树单点更新+区间求和
- hdu1754 I Hate It(线段树,单点更新,区间最值)