ZOJ1610_Count the Colors _线段树的树结构
来源:互联网 发布:电子琴与电钢琴 知乎 编辑:程序博客网 时间:2024/06/03 15:00
题意
往一个线段上涂色,后涂的会覆盖先前涂的颜色。问涂完后,每一种颜色能看到几段。
思路
用线段树来做。
涂色的过程就是线段树的区间修改。
计数的过程就是单点查询,用 dfs 遍历查看每一个叶子节点的颜色。
计数过程运用了线段树的树形结构的dfs序。
超级坑点
如果两段同色线段之间有一段没涂色的,那么这应该算两段。更新 pre 的时机要注意。WA了n多个小时。
题目链接
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
AC代码
#include<cstdio>#include<iostream>#include<cstring>using namespace std;const int maxn = 8e3 + 10;int N = 8000;int n;int x, y, z;int node[maxn << 2];int ans[maxn];int pre;void Push_down(int p){ node[p<<1] = node[p<<1|1] = node[p]; node[p] = -1;}void Add(int p, int l, int r, int x, int y, int z){ if(x <= l && r <= y) { node[p] = z; return; } if(node[p] != -1) Push_down(p); int mid = (l + r) >> 1; if(x <= mid) Add(p<<1, l, mid, x, y, z); if(y > mid) Add(p<<1|1, mid+1, r, x, y, z);}void dfs(int p, int l, int r){ if(l == r) { if(node[p] != -1 && node[p] != pre) ans[node[p]] ++; pre = node[p];//就是这里!!! return; } if(node[p] != -1) Push_down(p); int mid = (l + r) >> 1; dfs(p<<1, l, mid); dfs(p<<1|1, mid+1, r);}int main(){ while(scanf("%d", &n) != EOF) { memset(node, -1, sizeof node); memset(ans, 0, sizeof ans); for(int i= 1; i<= n; i++) { scanf("%d %d %d", &x, &y, &z); x ++; Add(1, 1, N, x, y, z); } pre = -1; dfs(1, 1, N); for(int i= 0; i<= N; i++) if(ans[i] > 0) printf("%d %d\n", i, ans[i]); printf("\n"); } return 0;}
阅读全文
0 0
- ZOJ1610_Count the Colors _线段树的树结构
- ZOJ1610_Count the Colors(线段树/成段更新)
- ZOJ 1610Count the Colors 线段树_区间更新
- 非结构体线段树版 ZJU 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【线段树】
- zoj1610Count the Colors【暴力 or 线段树】
- Count the Colors(线段树染色)
- ZOJ----1610-Count the Colors(线段树)
- Material Design 中文版
- 解决方案:java.lang.IllegalArgumentException: The observer is null
- Rescue
- 编写第一个maven插件(含完整项目)
- 成为优秀工程师的五个方法
- ZOJ1610_Count the Colors _线段树的树结构
- 在salesforce中如何查询队列Queues中的User的Id
- Android简单了解自定义控件和自定义属性如何使用
- matlab学习笔记 bsxfun函数
- Vue 学习笔记(一)
- Spring AOP四种实现方式
- 工作一些经验分享
- 欢迎使用CSDN-markdown编辑器
- 故事板(Storyboard)