ZOJ----1610-Count the Colors(线段树)
2015-11-10 18:55
471 查看
C - Count the Colors
Crawling in process...
Crawling failed
Time Limit:2000MS
Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit
Status
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
Crawling in process...
Crawling failed
Time Limit:2000MS
Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit
Status
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
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; #define SIZE 8005 int temp; int sum_c[SIZE]; struct Node { int a,b; int col; } node[SIZE<<2]; void in_sert(int a,int b,int c,int i) { if(a==b)return; if(node[i].col==c)return; if(node[i].a>=a && node[i].b<=b) { node[i].col=c; return; } if(node[i].col>=0) { node[i<<1].col=node[i].col; node[i<<1|1].col=node[i].col; } node[i].col=-2; int mid=(node[i].a+node[i].b)>>1; if(a>=mid)in_sert(a,b,c,i<<1|1); else if(b<=mid)in_sert(a,b,c,i<<1); else { in_sert(a,mid,c,i<<1); in_sert(mid,b,c,i<<1|1); } } void build_tree(int a,int b,int i) { node[i].a=a; node[i].b=b; node[i].col=-1; if(a+1==b)return; int mid=(a+b)>>1; build_tree(a,mid,i<<1); build_tree(mid,b,i<<1|1); } void query(int i) { if(node[i].col==-1) { temp=-1; } else if(node[i].col>=0) { if(node[i].col!=temp) { sum_c[ node[i].col ]++; temp=node[i].col; } } else if(node[i].a+1 != node[i].b) { query(i<<1); query(i<<1|1); } return; } int main() { int n; while(scanf("%d",&n)!=EOF) { build_tree(0,8000,1); int i,j,la,lb,c; for(i=0; i<n; ++i) { scanf("%d%d%d",&la,&lb,&c); in_sert(la,lb,c,1); } temp=-1; memset(sum_c,0,sizeof(sum_c)); query(1); for(j=0; j<=SIZE; ++j) { if(sum_c[j]>0) printf("%d %d\n",j,sum_c[j]); } printf("\n"); } return 0; }
相关文章推荐
- Java依据文件头获取文件类型
- Gradle对Maven的支持(Android开发)
- iOS开发网络篇—NSURLConnection基本使用
- java基础问题(1)
- pl/sql 的 put和put_line区别
- 求助帖 编译通过,链接不行
- 让VIM显示行号的方法
- 乘法口决表的 "减行和减列" 红线划掉的部分去掉
- boost::function用法详解
- boost::function用法详解
- 多态与异常处理
- 延展性,实现方法的私有化
- for语句中加入时间判断
- 水仙花数问题
- grunt学习笔记九:新建project
- C语言学习——怪异的事情
- mybtais xml文件中单个where参数问题
- Xcode报错:Undefined symbols for architecture armv7s:"_OBJC_CLASS_$_AMapView", referenced from:objc-c
- 在C语言中利用PCRE实现正则表达式
- loadrunner 接口测试实战