POJ 2777 Count Color (线段树区间更新 位压缩)
来源:互联网 发布:删除json数组指定元素 编辑:程序博客网 时间:2024/06/04 19:32
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 40258 Accepted: 12152
Description
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.
There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:
1. "C A B C" Color the board from segment A to segment B with color C.
2. "P A B" Output the number of different colors painted between segment A and segment B (including).
In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.
There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board:
1. "C A B C" Color the board from segment A to segment B with color C.
2. "P A B" Output the number of different colors painted between segment A and segment B (including).
In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your.
Input
First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.
Output
Ouput results of the output operation in order, each line contains a number.
Sample Input
2 2 4C 1 1 2P 1 2C 2 2 2P 1 2
Sample Output
21
Source
POJ Monthly--2006.03.26,dodo
题目链接:http://poj.org/problem?id=2777
题目大意:区间上色,然后查询区间颜色数
题目分析:由于颜色最多30种,可以考虑把颜色状态压缩,然后就是普通的区间更新
#include <cstdio>#include <cstring>#include <algorithm>#define lp rt << 1#define rp rt << 1 | 1#define lson l, mid, lp#define rson mid + 1, r, rpusing namespace std;int const MAX = 1e5 + 5;int col[MAX << 2], lazy[MAX << 2];int L, T, O;void PushUp(int rt){col[rt] = col[lp] | col[rp];}void PushDown(int rt){if(lazy[rt]){col[lp] = col[rt];col[rp] = col[rt];lazy[lp] = 1;lazy[rp] = 1;lazy[rt] = 0;}}void Build(int l, int r, int rt){lazy[rt] = 0;if(l == r){col[rt] |= 1;return;}int mid = (l + r) >> 1;Build(lson);Build(rson);PushUp(rt);return;}void Update(int L, int R, int c, int l, int r, int rt){if(L <= l && r <= R){col[rt] = 0;col[rt] |= (1 << c);lazy[rt] = 1;return;}int mid = (l + r) >> 1;PushDown(rt);if(L <= mid)Update(L, R, c, lson);if(mid < R)Update(L, R, c, rson);PushUp(rt);return;}int Query(int L, int R, int l, int r, int rt){if(L <= l && r <= R)return col[rt];int mid = (l + r) >> 1;PushDown(rt);int ans = 0;if(L <= mid)ans |= Query(L, R, lson);if(mid < R)ans |= Query(L, R, rson);return ans;}int main(){scanf("%d %d %d", &L, &T, &O);Build(1, L, 1);while(O --){char s[2];scanf("%s", s);if(s[0] == 'C'){int l, r, c;scanf("%d %d %d", &l, &r, &c);if(l > r)swap(l, r);Update(l, r, c - 1, 1, L, 1);}else {int l, r;scanf("%d %d", &l, &r);if(l > r)swap(l, r);int ans = 0, sta = Query(l, r, 1, L, 1);for(int i = 0; i < 31; i++)if(sta & (1 << i))ans ++;printf("%d\n", ans);}}}
0 0
- POJ 2777 Count Color (线段树区间更新 位压缩)
- POJ 2777Count Color (线段树区间更新 + 位运算压缩)
- POJ 2777 Count Color 线段树区间更新位运算
- 线段树区间更新,区间统计 poj 2777 Count Color
- POJ 2777 Count Color——区间更新的线段树+状态压缩
- POJ 2777 Count Color(位运算+线段树+lazy+区间更新)
- POJ 2777 Count Color 【线段树 区间更新 按位或运算】
- poj 2777 Count Color(线段树区间更新+位运算)
- poj 2777 Count Color(位运算+线段树区间更新 可用bitset记录)
- POJ 2777 Count Color (线段树区间更新)
- poj 2777 Count Color (线段树区间更新)
- [POJ 2777]Count Color[线段树区间更新查询]
- poj 2777 Count Color(线段树 区间更新)
- poj 2777 Count Color (线段树 区间更新 染色)
- POJ 2777 Count Color 线段树 区间更新
- poj 2777 Count Color 线段树区间更新
- POJ 2777 Count Color (线段树区间更新)
- POJ 2777 Count Color(线段树区间更新+状压)
- WPF的5种绑定模式(mode)
- 自定义标题栏
- CString与 char *之间的转换
- 小谈JAVA异常
- Vitamio中文API文档(3)—— MediaController
- POJ 2777 Count Color (线段树区间更新 位压缩)
- 监理工程师岁月--杀人事件
- UIWindow探究
- C++中delete和delete[]的区别
- LeetCode 110 Balanced Binary Tree
- java调用kettle工具类
- 一个Demo看URL和URI的区别
- 各种排序算法的分析及java实现
- SpringMVC 注解