Count the Colors 数颜色 离散化+线段树
来源:互联网 发布:军训 知乎 编辑:程序博客网 时间:2024/06/03 17:04
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
给定n个颜色区间 可能会产生覆盖。让你最后统计有多少种颜色,有多少块。
正确姿势:离散化+线段树
#include <algorithm> #include <iostream> #include <string.h> #include <cstdio> #include <set> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 using namespace std; const int maxn = 10005; int col[maxn<<4], lazy[maxn<<4]; int inp[maxn][2], chg[maxn<<2]; set<int> hash; //记录访问过与否 void pushDown(int rt) { if(lazy[rt]) { lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt]; col[rt<<1] = col[rt<<1|1] = lazy[rt]; lazy[rt] = 0; } } void build(int l, int r, int rt) { lazy[rt] = 0; col[rt] = 0; if(l == r) return; int m = (l+r) >> 1; build(lson); build(rson); } void update(int k, int L, int R, int l, int r, int rt) { if(L <= l && R >= r) { col[rt] = k; lazy[rt] = k; return; } pushDown(rt); int m = (l+r) >> 1; if(L <= m) update(k, L, R, lson); if(R > m) update(k, L, R, rson); } int query(int l, int r, int rt) { if(l == r) { if(col[rt] && !hash.count(col[rt])) { hash.insert(col[rt]); return 1; } return 0; } pushDown(rt); int m = (l+r) >> 1; return query(lson) + query(rson); } int main() { int t, n, l, r, cnt; scanf("%d", &t); for(int i = 1; i <= t; ++i) { hash.clear(); cnt = 0; scanf("%d", &n); for(int j = 1; j <= n; ++j) { scanf("%d %d", &inp[j][0], &inp[j][1]); chg[cnt++] = inp[j][0]; chg[cnt++] = inp[j][1]; } sort(chg, chg+cnt); cnt = unique(chg, chg+cnt) - chg; int tmp = cnt; for(int j = 1; j < tmp; ++j) { if(chg[j] - chg[j-1] > 1) chg[cnt++] = chg[j-1]+1; } sort(chg, chg+cnt); build(1, cnt, 1); for(int j = 1; j <= n; ++j) { int a = lower_bound(chg, chg+cnt, inp[j][0]) - chg; int b = lower_bound(chg, chg+cnt, inp[j][1]) - chg; ++a, ++b; update(j, a, b, 1, cnt, 1); } printf("%d\n", query(1, cnt, 1)); } return 0; }
大小为1~8000 存区间 查询的时候如果区间有laze标记(col),然后再用一个数组存真实区间 vis每次查询的时候对于有标记的区间,填满vis就好,最后统计一下
#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int N = 8115;int col[N<<2],ll[N<<2],rr[N<<2];int ans[N<<2];int vis[N<<2];void pushdown(int rt){ if(col[rt]!=-1) { col[rt<<1]=col[rt<<1|1]=col[rt]; col[rt]=-1; }}void build(int l,int r,int rt){ ll[rt]=l; rr[rt]=r; if(l==r) { col[rt]=-1; vis[rt]=-1; return ; } int m=(l+r)>>1; build(l,m,rt<<1); build(m+1,r,rt<<1|1);}void update(int l,int r,int c,int rt){ if(l<=ll[rt]&&r>=rr[rt]) { col[rt]=c; return ; } if(col[rt]==c) return ; if(col[rt]!=-1) pushdown(rt); int m=(ll[rt]+rr[rt])>>1; if(r<=m) update(l,r,c,rt<<1); else if(l>m) update(l,r,c,rt<<1|1); else { update(l,m,c,rt<<1); update(m+1,r,c,rt<<1|1); }}void query(int rt){ if(col[rt]>=0) { for(int i=ll[rt];i<=rr[rt];i++) vis[i]=col[rt]; return ; } if(col[rt]==-1&&ll[rt]!=rr[rt]) { query(rt<<1); query(rt<<1|1); }}int main(){ int n; while(scanf("%d",&n)!=EOF) { memset(vis,0,sizeof(vis)); memset(col,-1,sizeof(col)); build(1,8010,1); for(int i=1;i<=n;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); update(a+1,b,c,1); } memset(vis,-1,sizeof(vis)); memset(ans,0,sizeof(ans)); query(1); int index=1; while(index<=8010) { if(vis[index]!=-1) { int sum=0 ; int color=vis[index]; while(vis[index]!=-1&&vis[index]==color&&index<=8010) index++; ans[color]++; index--; } index++; } for(int i=0;i<=8010;i++) { if(ans[i]) { printf("%d %d\n",i,ans[i] ); } } puts(""); }}
- Count the Colors 数颜色 离散化+线段树
- ZOJ 1610 Count the Colors(线段覆盖着色:离散化)
- ZOJ-1610 Count the Colors (离散+线段树)
- ZOJ1610 Count the Colors,线段树
- ZOJ 1610Count the Colors//线段树
- zoj 1610 Count the Colors【线段树】
- zoj 1610 Count the Colors[线段树]
- 【线段树】Count the Colors (zoj 1610)
- zoj 1610 Count the Colors(线段树)
- zoj1610 Count the Colors(线段树)
- zoj 1610 Count the Colors 线段树
- 线段树之ZOJ1610 Count the Colors
- ZOJ - 1610 Count the Colors(线段树)
- ZOJ 1610 Count the Colors【线段树】
- Count the Colors(线段树染色)
- ZOJ----1610-Count the Colors(线段树)
- zoj1610( 线段树)(Count the Colors)
- ZJU 1610 Count the Colors 线段树
- caffe 预处理,图片转为LMDB
- 如何改变谷歌浏览器chrome的页面检查工具的位置
- putExtras
- 七牛云存储
- 转载:iOS 保持界面流畅的技巧
- Count the Colors 数颜色 离散化+线段树
- 100. Same Tree判断二叉树相等
- java的几种基本数据类型
- Postman 详解
- 关于首屏性能优化的总结【原创】
- 字符串去掉重复元素
- 性能测试知识体系
- 使用HTML5在网页中嵌入音频和视频播放的基本方法
- HTTP响应头和请求头信息对照表