Poj 2777 Count Color (segment update + interval sum)
Count Color
| Time Limit:1000 MS |
|
Memory Limit:65536 K |
| Total Submissions:36646 |
|
Accepted:11053 |
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 previusly.
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
The meaning is to find a wall color category, because the color category can be up to 30, and the total number of colors can be expressed in binary.
# Include
# Include
# Include
# Include
# Define deusing namespace std; # define ll _ int64 # define N 100005 struct node {int l, r; int s, v, f; // second-level representation of color types, interval color, whether downward update required} f [N * 3]; void creat (int t, int l, int r) {f [t]. l = l; f [t]. r = r; f [t]. v = 1; f [t]. f = 0; f [t]. s = 1; // The starting color is 1, which can be expressed in binary if (l = r) {return;} int tmp = t <1, mid = (l + r)> 1; creat (tmp, l, mid); creat (tmp | 1, mid + 1, r);} void update (int t, int l, int r, int v) {int tmp = t <1, mid = (f [t]. l + f [t]. r)> 1; if (f [t]. l = l & f [t]. r = r) {f [t]. v = v; f [t]. s = (1 <
Mid) update (tmp | 1, l, r, v); else {update (tmp, l, mid, v); update (tmp | 1, mid + 1, r, v);} f [t]. f = 0; // sum up f [t]. s = f [tmp]. s | f [tmp | 1]. s;} int query (int t, int l, int r) {if (f [t]. l = l & f [t]. r = r) {return f [t]. s;} int tmp = t <1, mid = (f [t]. l + f [t]. r)> 1; if (f [t]. f) {f [tmp]. f = f [tmp | 1]. f = 1; f [tmp]. v = f [tmp | 1]. v = f [t]. v; f [tmp]. s = f [tmp | 1]. s = (1 <
Mid) return query (tmp | 1, l, r); else {return query (tmp, l, mid) | query (tmp | 1, mid + 1, r) ;}} int main () {int n, t, q, l, r, c; char ch; while (~ Scanf ("% d", & n, & t, & q) {creat (1, 1, n); while (q --) {getchar (); scanf ("% c", & ch); if (ch = 'C') {scanf ("% d", & l, & r, & c); if (l> r) swap (l, r); update (1, l, r, C-1);} else {scanf ("% d ", & l, & r); if (l> r) swap (l, r); int tmp = query (1, l, r), ans = 0; while (tmp) {if (tmp & 1) ans ++; tmp >>=1 ;}printf ("% d \ n", ans) ;}} return 0 ;}