线段树(区间更新以及统计片段颜色)
来源:互联网 发布:女主是编程天才的小说 编辑:程序博客网 时间:2024/06/08 04:22
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[N]; //¼Ç¼ÿ¿é°åÉϵÄÊÇʲôÑÕÉ«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[N]; 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;}
阅读全文
0 0
- 线段树(区间更新以及统计片段颜色)
- poj 2528【线段树-区间更新,区间统计】
- 线段树区间更新,区间统计 poj 2777 Count Color
- 线段树 区间更新 区间求和以及最值
- 线段树(单点更新+区间更新)
- 线段树区间更新
- 线段树区间更新
- 线段树 区间更新
- 线段树区间更新
- 线段树-区间更新
- 线段树区间更新
- 线段树区间更新
- 线段树区间更新
- 区间更新线段树
- 线段树区间更新
- 线段树 区间更新
- 线段树区间更新
- 线段树之建树,单点更新以及区间查询
- Regex---正则表达式的坑爹例子
- 士兵杀敌(二)(线段树)
- Android大图片压缩工具类
- BZOJ 4145 浅谈状态压缩动态规划背包问题
- 进制之间的转换
- 线段树(区间更新以及统计片段颜色)
- 设计模式_状态模式
- 使用Spring boot + jQuery上传文件(kotlin)
- Python 网络编程(高级教程)
- opencv3 + cuda opencv error: the function/feature is not implemented
- P1036 选数
- 构造函数栈溢出
- HDU-1312 Red and Black
- Jenkins+Python部署完整版