ZOJ1610 Count the Colors
来源:互联网 发布:招银网络 编辑:程序博客网 时间:2024/05/18 13:24
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
貌似线段树的区间染色是一个非常常见的题型吧,但是不得不说。。我昨天刚刚碰到,而且统计每一种颜色占了多少个不连续区间的时候着实不会写,表示前面把树建出来和更新的步骤都是纯我自己写的,就是查找颜色个数那一步。。。不得不参考了,最后弄明白了。
如果一个区间没有染色或者有多种颜色,都会有标记,如果有了标记,就可以不用考虑这个结点了,直接找它的两个儿子就行了。如果证实某一个区间被染过色而且里面仅仅有一个颜色,那么就可以与其相邻子树比较然后再更新结果值(要求的是每种颜色占的不连续区间数量,相邻子树代表的是连续区间,所以就得这么比较),然后直接退出这个状态(都说了里面只有一个颜色,就没有找它儿子的必要了)。。
#include <cstdio>#include <cstring>#define lson l, m, rt << 1#define rson m, r, rt << 1 | 1const int maxn = 8111;int col[maxn << 2], tmp, num[maxn];void PushDown(int rt){ if (col[rt] >= 0){ col[rt << 1] = col[rt]; col[rt << 1 | 1] = col[rt]; col[rt] = -2; }}void build(int l, int r, int rt){ col[rt] = -1; if (l == r - 1) return; int m = (l + r) >> 1; build(lson); build(rson);}void update(int ll, int rr, int c, int l, int r, int rt){ if (col[rt] == c) return; if (ll <= l && rr >= r){ col[rt] = c; return; } PushDown(rt); int m = (l + r) >> 1; if (ll < m) update(ll, rr, c, lson); if (rr > m) update(ll, rr, c, rson);}void query(int l, int r, int rt){ if (col[rt] != -1 && col[rt] != -2){ if (col[rt] != tmp) num[col[rt]] += 1; tmp = col[rt]; return; } if (l == r - 1){ tmp = -1; return; } int m = (l + r) >> 1; query(lson); query(rson);}int main(){ //freopen("in.txt", "r", stdin); int n; while(~scanf("%d", &n)){ build(0, 8000, 1); memset(num, 0, sizeof(num)); while(n--){ int x, y, z; scanf("%d%d%d", &x, &y, &z); update(x, y, z, 0, 8000, 1); } tmp = -1; query(0, 8000, 1); for (int i = 0; i <= 8000; i++) if (num[i]) printf("%d %d\n", i, num[i]); printf("\n"); } return 0;}
- ZOJ1610-Count the Colors
- zoj1610-Count the Colors
- ZOJ1610 Count the Colors
- ZOJ1610-Count the Colors
- zoj1610:count the colors
- ZOJ1610 Count the Colors,线段树
- [Virtual Judge]ZOJ1610:Count the Colors
- ZOJ1610——Count the Colors
- zoj1610 Count the Colors(线段树)
- 线段树之ZOJ1610 Count the Colors
- zoj1610( 线段树)(Count the Colors)
- zoj1610 Count the Colors(线段树)
- ZOJ1610 Count the Colors(线段树+思路)
- zoj1610——Count the Colors
- ZOJ1610 Count the Colors 经典线段树染色问题
- 线段树专题—ZOJ1610 Count the Colors
- ZOJ1610 Count the Colors(线段树区间更新)
- ZOJ1610 Count the Colors(线段树区间染色,成段更新)
- TestNG官方文档中文版(05)—— 测试方法、测试类和测试组
- 小知识点日志2012-11-22 至 2013-1-9
- 几种函数调用方式
- 僵尸进程,孤儿进程,wait,exit,execl等函数使用要点
- 日志分析器
- ZOJ1610 Count the Colors
- ASP.NET MVC3 系列教程 - Razor视图引擎基础语法
- TestNG官方文档中文版(06)—— 测试结果
- 使用mdev在/dev/下创建子目录放置设备节点
- C语言产生的泊松过程流
- 给ExtJS的Grid增加两行tbar
- 导航栏目与内容对应的java解决
- [STL基础]multimap多级映射
- STL中Multimap的一个例子