poj 2777 Count Color
http://poj.org/problem?id=2777
简单线段树,【成段更新,成段查询】!
1 #include <cstdio> 2 #include <cstring> 3 #include <cstdlib> 4 #include <algorithm> 5 6 using namespace std; 7 8 #define lson l, m, rt << 1 9 #define rson m + 1, r, rt << 1 | 1 10 11 const int maxn = 100001; 12 int color[maxn << 2], late[maxn << 2]; 13 14 void up(int rt) { 15 color[rt] = color[rt << 1] | color[rt << 1 | 1]; 16 } 17 18 void down(int rt) { 19 int l = rt << 1, r = rt << 1 | 1; 20 21 if (late[rt]) { 22 late[l] = late[r] = late[rt]; 23 color[l] = color[r] = late[rt]; 24 late[rt] = 0; 25 } 26 } 27 28 void build(int l, int r, int rt) { 29 late[rt] = 0; 30 if (l == r) { 31 color[rt] = 2; 32 33 return ; 34 } 35 int m = (l + r) >> 1; 36 37 build(lson); 38 build(rson); 39 up(rt); 40 } 41 42 void update(int key, int L, int R, int l, int r, int rt) { 43 if (L <= l && r <= R) { 44 color[rt] = late[rt] = 1 << key; 45 46 return ; 47 } 48 int m = (l + r) >> 1; 49 50 down(rt); 51 if (L <= m) update(key, L, R, lson); 52 if (m < R) update(key, L, R, rson); 53 up(rt); 54 } 55 56 int query(int L, int R, int l, int r, int rt) { 57 if (L <= l && r <= R) { 58 return color[rt]; 59 } 60 int m = (l + r) >> 1; 61 int ret = 0; 62 63 down(rt); 64 if (L <= m) ret |= query(L, R, lson); 65 if (m < R) ret |= query(L, R, rson); 66 up(rt); 67 68 return ret; 69 } 70 71 int main() { 72 int l, t, o; 73 int a, b, c; 74 char op[3]; 75 76 freopen("in", "r", stdin); 77 while (~scanf("%d%d%d", &l, &t, &o)) { 78 build(1, l, 1); 79 while (o--) { 80 scanf("%s", op); 81 switch (op[0]) { 82 case 'C': 83 scanf("%d%d%d", &a, &b, &c); 84 if (a > b) { 85 a ^= b; 86 b ^= a; 87 a ^= b; 88 } 89 update(c, a, b, 1, l, 1); 90 91 break; 92 case 'P': 93 scanf("%d%d", &a, &b); 94 if (a > b) { 95 a ^= b; 96 b ^= a; 97 a ^= b; 98 } 99 c = query(a, b, 1, l, 1); 100 101 int cnt = 0; 102 103 while (c) { 104 cnt += c & 1; 105 c >>= 1; 106 } 107 printf("%d\n", cnt); 108 109 break; 110 } 111 } 112 } 113 114 return 0; 115 }
——written by Lyon
转载于:https://www.cnblogs.com/LyonLys/archive/2012/10/05/poj_2777_Lyon.html
poj 2777 Count Color相关推荐
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- POJ 2777 Count Color (线段树区间修改 + 状态压缩)
题目链接:POJ 2777 Count Color [题目大意] 给你 n 块板子, 编号1--n , 板子的颜色最多30种, 初始时 板子的颜色都是 1: 有两种操作 1 .把给定区间的板子染成一 ...
- POJ 2777 - Count Color(线段树区间更新+状态压缩)
题目链接 https://cn.vjudge.net/problem/POJ-2777 [题意] 有一个长度为 LLL 的区间 [1,L][1,L][1,L] ,有 TTT 种颜色可以涂,有 QQQ ...
- poj 2777 AND hdu 5316 线段树
区间染色问题,用线段树可以解决.颜色总类不多,故考虑用二进制数的每一位表示一种颜色,然后父节点的颜色就是孩子节点颜色"或"起来,加上lazy标记,轻松AC. poj 2777: 1 ...
- 线段树区间染色 浮水法 学习小记 Poj 2777 + Poj 2528
复习过的东西必须时常拿来练练,虽说都是水题.... Poj 2777 典型的区间染色问题,貌似我的写法为了不数组越界必须多开一倍的数组空间.....还没有想到解决方法,有机会参考一下别人的写法. Po ...
- Count Color poj2777 线段树
Count Color poj2777 线段树 题意 有一个长木板,现在往上面在一定区间内刷颜色,后来刷的颜色会掩盖掉前面刷的颜色,问每次一定区间内可以看到多少种颜色. 解题思路 这里使用线段树,因为 ...
- 数据结构二之线段树Ⅰ——Count Color,Hotel,Transformation,Tree Generator™
普通的下标线段树 Count Color Hotel Transformation Tree Generator™ Count Color POJ2777 查询区间内颜色种类数,观察到颜色种类数只有3 ...
- Count Color(poj 2777)
题意: 给一个固定长度为L的画板 有两个操作: C A B C:区间AB内涂上颜色C. P A B:查询区间AB内颜色种类数. 分析:显然是要用线段树来操作的,设定一个sum[]来维护一个区间内的颜色 ...
- hdu 5023 poj 2777(线段染色)2014 ACM/ICPC Asia Regional 广州 Online
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5023 http://poj.org/problem?id=2777 题意:给出一个长度为N的线段,分 ...
最新文章
- python的类型化_显式类型化的Python版本?
- element 0 of tensors does not require grad and does not have a grad_fn
- Makefile_02:程序的编译和链接
- Math工具类常用API使用案例
- 计算机语言发展及未来方向
- CKEditor 4编辑器已与Vue.js集成
- 向量点积(Dot Product)
- 只身单车游山东(一)
- 如何将txt、excel文档里面的电话号码快速转换为vcf格式的电话簿导入手机
- pwnable之bof
- 测试分析报告(GB8567——88)基于协同的在线表格forture-sheet
- mybatis lazyload
- margin外边距合并问题以及解决方式
- scrapy中的Request的用法
- Android uevent进程源码分析
- 外观模式-简化子系统的复杂性
- Beta产品测试报告:那周余嘉熊掌将得队、为了交项目干杯队
- 零技巧的电饭锅懒人菜
- 和ts一般怎么玩_TS夺冠后马上卖席位?微博电竞欲加盟,以后场场热搜安排上...
- endnote能自动翻译吗_把你的中文论文翻译成SCI paper能发表么?
热门文章
- 微信小程序开发流程介绍
- 前端计划——面试题总结-CSS篇
- PHP运算符与表达式
- Elasticsearch搜索类型讲解(QUERY_THEN_FETCH,QUERY_AND_FEATCH,DFS_QUERY_THEN_FEATCH和DFS_QUERY_AND_FEATCH)...
- iOS 模仿微信的照片选择器
- java课程第七天,匿名内部类以及异常处理
- Junit 3 与 Junit 4写法
- .Net 引用命名空间
- 为SharePoint顶部链接开发自定义数据源
- [inside]MySQL 5.7 并行复制实现原理与调优