zoj 1610 Count the Colors 线段树区间更新——染色问题
2015-09-28 20:04
381 查看
Count the Colors
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
题目大意:给了每一线段的颜色,存在颜色覆盖,求表面上看能看到的颜色种类和各种颜色的段数;
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
代码:
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
题目大意:给了每一线段的颜色,存在颜色覆盖,求表面上看能看到的颜色种类和各种颜色的段数;
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
代码:
#include <iostream> #include <stdio.h> #include <string.h> #define N 8005 using namespace std; int temp,col ; struct Tree { int l,r; int color; }tree[N*4]; void build(int l,int r,int root) { tree[root].l=l; tree[root].r=r; tree[root].color=-1; if(l+1==r) return; int mid=(l+r)>>1; build(l,mid,root<<1); build(mid,r,(root<<1|1)); } void insert(int root,int l,int r,int c) { if(l==r) return; if(tree[root].color==c) return; if(tree[root].l>=l&&tree[root].r<=r) { tree[root].color=c; return; } if(tree[root].color>=0) { tree[root<<1].color=tree[root<<1|1].color=tree[root].color; tree[root].color=-2; } int mid=(tree[root].l+tree[root].r)/2; if(mid<=l) insert(root<<1|1,l,r,c); else if(mid>=r) insert(root<<1,l,r,c); else { insert(root<<1,l,mid,c); insert(root<<1|1,mid,r,c); } tree[root].color=-2; } void search(int root) { if(tree[root].color==-1) { temp=-1; return; } if(tree[root].color!=-2) { if(temp!=tree[root].color) { col[tree[root].color]++; temp=tree[root].color; } return; } if(tree[root].l+1!=tree[root].r) { search(root<<1); search(root<<1|1); } } int main() { int n,a,b,c; while(scanf("%d",&n)!=EOF) { build(0,8000,1); int maxn=0; for(int i=1;i<=n;i++) { scanf("%d%d%d",&a,&b,&c); insert(1,a,b,c); if(maxn<c) maxn=c; } memset(col,0,sizeof(col)); search(1); for(int i=0;i<=maxn;i++) { if(col[i]) printf("%d %d\n",i,col[i]); } printf("\n"); } return 0; }
相关文章推荐
- Jmeter分布式测试
- hdu 5491(位运算)
- hdu 5491(位运算)
- 获取ios设备型号
- hdu 5492(dp)
- 第四周项目一 建立单链表
- 图片封装下载器--自己的代码库--可以直接使用
- hdu5475__An easy problem(线段树单点更新)
- How to Write an Effective Design Document
- 3 B. Lorry
- ZOJ第1240题 灰常简单的题目
- canvas鼠标点击散播图片
- 代码调试中偶然错误的处理方法
- 文本框 UITextView
- 【读书笔记】iOS-KVC
- 固定列表标题栏
- POJ 1845 Sumdiv (数学,乘法逆元)
- poj3320 尺取法~~
- Python进阶 - HTML获取与解析
- 高等数学公式---从数学公式到物理模型