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. 

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
原创粉丝点击