Count Color
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 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
Source
题目类型:线段树(区间染色)
算法分析:这是一个不覆盖的区间染色问题,需要使用Lazy-Tag标记,使用lazy数组表示当前区间是否已经完全被覆盖。由于颜色数量比较小,所以可以使用位运算来压缩空间
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 100000 + 66; int sum[maxn<<2]; bool lazy[maxn<<2]; void PushUp (int rt) { sum[rt] = sum[rt<<1] | sum[rt<<1|1]; } void PushDown (int rt, int l, int m, int r) { if (lazy[rt]) { lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt]; sum[rt<<1] = sum[rt<<1|1] = sum[rt]; lazy[rt] = false; } } void UpDate (int rt, int l, int r, int L, int R, int v) { if (L <= l && r <= R) { sum[rt] = 1 << (v - 1); lazy[rt] = true; return ; } int m = (l + r) >> 1; PushDown(rt, l, m, r); if (L <= m) UpDate (lson, L, R, v); if (R > m) UpDate (rson, L, R, v); PushUp (rt); } long long Query (int rt, int l, int r, int L, int R) { if (L <= l && r <= R) { return sum[rt]; } long long res = 0; int m = (l + r) >> 1; PushDown(rt, l, m, r); if (L <= m) res |= Query (lson, L, R); if (R > m) res |= Query (rson, L, R); return res; } int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); int n, vv, q; while (scanf ("%d%d%d",&n, &vv, &q) != EOF) { memset (lazy, false, sizeof (lazy)); memset (sum, 0, sizeof (sum)); sum[1] = 1; lazy[1] = true; char cmd[6]; for (int i = 1; i <= q; i++) { scanf ("%s", cmd); if (!strcmp (cmd, "P")) { int a, b; scanf ("%d%d", &a, &b); if (a > b) swap (a, b); long long tt = Query (1, 1, n, a, b); int cnt = 0; for (int i = 0; i < vv; i++) if (tt & (1 << i)) cnt++; printf ("%d\n", cnt); } else { int a, b, c; scanf("%d%d%d", &a, &b, &c); if (a > b) swap (a, b); UpDate (1, 1, n, a, b, c); } } } return 0; } |
- « 上一篇:poj2752
- poj2785:下一篇 »