Poj 2777 Count Color (line segment tree + dyeing problem)
Count Color
Time Limit:1000 MS |
|
Memory Limit:65536 K |
Total Submissions:40402 |
|
Accepted:12186 |
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
Source
POJ Monthly -- 2006.03.26, dodo
An interval with a length of L can have a maximum of T colors and O operations, followed by o rows.
There are two operations in total: 1. C a B c: indicates that the range [a, B] is colored c. 2. P a B: It indicates the number of colors in the interval [a, B.
Solution: You must note that you cannot update the question to the bottom of the topic until it is updated to the correct interval. Otherwise, the problem will time out.
For details, see the code.
# Include
# Include
# Include
# Include
# Include
Using namespace std; struct node {int l, r; int num;} s [400010]; int vis [35]; void InitTree (int l, int r, int k) {s [k]. l = l; s [k]. r = r; s [k]. num = 1; int mid = (l + r)/2; if (l = r) return; InitTree (l, mid, 2 * k ); initTree (mid + 1, r, 2 * k + 1);} void UpdataTree (int l, int r, int c, int k) {if (s [k]. l = l & s [k]. r = r) {s [k]. num = c; return;} if (s [k]. num = c) return; if (s [k]. num! =-1) // If the queried range is not multiple colors {s [2 * k]. num = s [k]. num; // update the color of the range s [2 * k + 1]. num = s [k]. num; s [k]. num =-1; //-1 indicates that the interval has multiple colors} int mid = (s [k]. l + s [k]. r)/2; if (l> mid) UpdataTree (l, r, c, 2 * k + 1); else if (r <= mid) UpdataTree (l, r, c, 2 * k); else {UpdataTree (l, mid, c, 2 * k); UpdataTree (mid + 1, r, c, 2 * k + 1) ;}} void SearchTree (int l, int r, int k) {if (s [k]. num! =-1) {vis [s [k]. num] = 1; return;} int mid = (s [k]. l + s [k]. r)/2; if (r <= mid) SearchTree (l, r, 2 * k); else if (l> mid) SearchTree (l, r, 2 * k + 1); else {SearchTree (l, mid, 2 * k); SearchTree (mid + 1, r, 2 * k + 1 );}} int main () {int l, t, o, ans; while (~ Scanf ("% d", & l, & t, & o) {InitTree (1, l, 1); while (o --) {char ch; int a, B, c; getchar (); scanf ("% c", & ch); if (ch = 'C ') {scanf ("% d", & a, & B, & c); if (a> B) swap (a, B); UpdataTree (, b, c, 1) ;}else {scanf ("% d", & a, & B); if (a> B) swap (a, B ); memset (vis, 0, sizeof (vis); SearchTree (a, B, 1); ans = 0; for (int I = 1; I <= t; I ++) if (vis [I] = 1) ans ++; printf ("% d \ n", ans) ;}} return 0 ;}