ZOJ1610Count the Colors(线段树成段更新染色)
来源:互联网 发布:mac系统怎么安装ps 编辑:程序博客网 时间:2024/05/21 16:55
链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
题目大意:
在一条长度为8000的线段上染色,每次把区间[a,b]染成c颜色。显然,后面染上去的颜色会覆盖掉之前的颜色。
求染完之后,每个颜色在线段上有多少个间断的区间。
注意:
因为染色的是区间,是从最左边的向右开始染色,所以更新的时候左端点可以加1。
#include<iostream>#include<algorithm>#include<cstring>#include<cstdio>using namespace std;const int maxn = 8005;int n, col[maxn<<2], vis[maxn<<2], ans[maxn];void inline push_down(int rt){ col[rt<<1] = col[rt<<1|1] = col[rt]; col[rt] = -1;}void update(int rt, int left, int right, int l, int r, int data){ if(l <= left && right <= r) { col[rt] = data; return; } if(col[rt] == data) return; if(col[rt] != -1) push_down(rt); int mid = (left + right) >> 1; if(l <= mid) update(rt<<1, left, mid, l, r, data); if(r > mid) update(rt<<1|1, mid+1, right, l, r, data);}void query(int rt, int left, int right){ if(col[rt] >= 0) { for(int i = left; i <= right; i++) vis[i] = col[rt]; return; } if(left < right && col[rt] == -1) { int mid = (left+right) >> 1; query(rt<<1, left, mid); query(rt<<1|1, mid+1, right); }}int main(){ int u, v, w; while(scanf("%d", &n) != EOF) { memset(col, -1, sizeof(col)); for(int i = 1; i <= n; i++) { scanf("%d%d%d", &u, &v, &w); if(u >= v) continue; update(1, 1, 8000, u+1, v, w); } memset(vis, -1, sizeof(vis)); query(1, 1, 8000); memset(ans, 0, sizeof(ans)); int i = 1; while(i <= 8000) { int color = vis[i], j = i + 1; if(color == -1) { ++i; continue; } while(vis[j] == color && j <= 8000) ++j; ++ans[color]; i = j; } for(int i = 0; i <= 8000; i++) if(ans[i]) cout << i << " " << ans[i] << endl; puts(""); } return 0;}
0 0
- ZOJ1610Count the Colors(线段树成段更新染色)
- zoj1610Count the Colors【暴力 or 线段树】
- Count the Colors+ZOJ+线段树成段更新
- Count the Colors(线段树染色)
- zoj 1610 Count the Colors(线段树,成段更新染色)
- zoj 1610 Count the Colors 线段树区间更新——染色问题
- zoj 1610 Count the Colors 线段树,成段更新染色
- ZOJ 1610 Count the Colors(线段树,成段更新染色)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- zoj 1610 Count the Colors(线段树)(成段更新染色)
- ZOJ1610 Count the Colors(线段树区间染色,成段更新)
- zoj 1610 Count the Colors --线段树成段更新
- ZOJ 1610 Count the Colors 线段树 染色问题
- ZOJ1610 Count the Colors 经典线段树染色问题
- zoj 1610 Count the Colors(线段树染色)
- ZOJ 1610Count the Colors 线段树区间染色问题
- ZOJ 1610 Count the Colors (线段树区间染色)
- ZOJ 1610Count the Colors(线段树+区间染色)
- 表单验证
- Idea 快捷键
- 微信分享注意的BUG
- Nginx+Tomcat搭建负载均衡集群
- Windows编程入门程序详解
- ZOJ1610Count the Colors(线段树成段更新染色)
- 学习记录
- 用jquery ajax实现提交form
- 封装技巧
- Python导入Excel、csv文件进oracle数据库
- lua reload lua脚步重启热更
- Oracle笔记--lag和lead分析函数
- php中static 静态变量和普通变量的区别
- 微信小程序 解析单图上传安卓手机上传失败和wx.request上传失败问题