Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
2015-07-09 23:13
477 查看
题目大意:
有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分?
解题思路:
这个题目建树的时候有些不同,并不是以点为对象,而是以区间为对象,很明显是对线段树的区间进行操作,更新的时候要以区间为单位,还有就是计算每个区间出现几次的时候可以根据线段树的建树特征对树进行遍历求解。
有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分?
解题思路:
这个题目建树的时候有些不同,并不是以点为对象,而是以区间为对象,很明显是对线段树的区间进行操作,更新的时候要以区间为单位,还有就是计算每个区间出现几次的时候可以根据线段树的建树特征对树进行遍历求解。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 8005; struct node { int l, r, c; int Mid() { return (r + l) / 2; } }; node tree[maxn*4]; int cnt[maxn], temp; void Build (int root, int l, int r) { tree[root].l = l; tree[root].r = r; tree[root].c = -1; if (l == r - 1) return ; Build (2*root+1, l, tree[root].Mid()); Build (2*root+2, tree[root].Mid(), r); } void update (int root, int l, int r, int c) {//区间更新, if (l==tree[root].l && tree[root].r==r) { tree[root].c = c; return ; } if (tree[root].c != -1) { tree[2*root+1].c = tree[2*root+2].c = tree[root].c; tree[root].c = -1; } if (l >= tree[root].Mid()) update (2*root+2, l, r, c); else if (r <= tree[root].Mid()) update (2*root+1, l, r, c); else { update (2*root+1, l, tree[root].Mid(), c); update (2*root+2, tree[root].Mid(), r, c); } } void Count (int root) { if (tree[root].c != -1) {//当前区间裸露在外面 if (tree[root].c != temp)//当前区间没有被统计过 cnt[tree[root].c] ++; temp = tree[root].c; return ; } if (tree[root].l == tree[root].r-1) {//返回未被覆盖的区间,否则会栈溢出 temp = -1; return ; } Count (2*root+1); Count (2*root+2); } int main () { int n, m; while (scanf ("%d", &n) != EOF) { Build (0, 0, maxn); while (n --) { int x, y, c; scanf ("%d %d %d", &x, &y, &c); update (0, x, y, c); } temp = -1; memset (cnt, 0, sizeof(cnt)); Count(0); for (int i=0; i<maxn; i++) if (cnt[i]) printf ("%d %d\n", i, cnt[i]); printf ("\n"); } return 0; }
相关文章推荐
- 弱引用NSTimer对象
- 1985年公布的C++语言1.0版的内容中又添加了一些重要特征
- 关于Linux下上网速度慢的问题
- API其他函数 ,system date,..
- 工具类CTools实现字符编码转换和获取当前路径
- 无主之地1
- C++这个名字
- leetCode 40.Combination Sum II(组合总和II) 解题思路和方法
- 一致性hash算法-php-redis版本
- KMP与扩展KMP
- [UGUI] 用脚本给UI绑定事件2
- 集合(3) Map集合,工具类
- 读陈浩的《C语言结构体里的成员数组和指针》总结,零长度数组
- 2015070903 - 必须依靠自己
- C#递归复制文件夹
- 【HM】第12课:文件上传&文件下载&注解
- SuperRecyclerView——使RecyclerView更加容易使用的Android类库
- 7-9工作日志
- hdu 4781 Assignment For Princess
- [MySQL] - errno:150