Count the Colors
2016-04-20 12:28
337 查看
线段树,对区间建树,维护区间颜色
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
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
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <cmath> #include <string> #include <iostream> #include <vector> #define LL long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=1e5+10; const int SIZE=8888; int temp; int cnt[SIZE]; struct seg{ int sum; }a[SIZE<<2]; void pushup(int rt){ if(a[rt<<1].sum==a[rt<<1|1].sum)a[rt].sum=a[rt<<1].sum; else a[rt].sum=-1; } void pushdown(int rt){ if(a[rt].sum>=0){ a[rt<<1].sum=a[rt<<1|1].sum=a[rt].sum; a[rt].sum=-1; } } void build(int l,int r,int rt){ a[rt].sum=-2; if(l==r)return; int m=(l+r)>>1; build(lson); build(rson); } void update(int L,int R,int c,int l,int r,int rt){ if(L<=l&&r<=R){ a[rt].sum=c; return; } pushdown(rt); int m=(l+r)>>1; if(L<=m)update(L,R,c,lson); if(R>m)update(L,R,c,rson); pushup(rt); } void query(int l,int r,int rt){ if(a[rt].sum>=0){ if(temp==a[rt].sum)return; temp=a[rt].sum; cnt[a[rt].sum]++; return; } if(a[rt].sum==-2){ temp=-1; return; } int m=(l+r)>>1; query(lson); query(rson); } int main() { int n,x,y,c; while(scanf("%d",&n)!=EOF){ memset(cnt,0,sizeof(cnt)); build(0,SIZE,1); for(int i=0;i<n;i++){ scanf("%d%d%d",&x,&y,&c); update(x,y-1,c,0,SIZE,1); } temp=-1; query(1,SIZE,1); for(int i=0;i<SIZE;i++){ if(cnt[i]){ printf("%d %d\n",i,cnt[i]); } } printf("\n"); } return 0; }
相关文章推荐
- ios开发多线程篇——GCD介绍
- Tle格式
- linux用户和用户配置文件(nobody)
- oc
- 如何利用Python生成随机密码
- C#中限制访问器可访问性
- ATL-连接点和接口方法的使用
- 基于邻接矩阵存储的图的深度优先遍历和广度优先遍历
- eclipse-tomcat发布java web项目 解决404
- linux ssh
- java重载-方法重载中的准确性原则
- B树相关操作
- JAVA 对象引用,以及对象赋值
- 基于邻接矩阵存储的图的深度优先遍历和广度优先遍历
- 分页思路
- C# 重载的几种实现
- Android倒计时案例展示
- 快速排序VS堆排序
- cornerstone提示“SQLite-database disk image is malformed”
- log4j 详细讲解(不能再详细了)