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 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
一道線段樹的入門題,詳見Mayor's Posters。
Accode:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <bitset>
using std::bitset;
const char fi[] = "poj2777.in";
const char fo[] = "poj2777.out";
const int maxN = 100010;
const int maxC = 40;
const int MAX = 0x3fffff00;
const int MIN = -MAX;
struct SegTree{int L, R, lc, rc, col; };
SegTree tree[maxN << 2];
bitset <maxC> marked;
int n, m, tot, c;
void init_file()
{
freopen(fi, "r", stdin);
freopen(fo, "w", stdout);
}
void Build(int L, int R)
{
int Now = ++tot;
tree[Now].L = L;
tree[Now].R = R;
tree[Now].col = 1; //最開始時顏色為1!
int Mid = (L + R) >> 1;
if (L < R)
{
tree[Now].lc = tot + 1;
Build(L, Mid);
tree[Now].rc = tot + 1;
Build(Mid + 1, R);
}
}
void insert(int Now, int L, int R, int col)
{
if (L <= tree[Now].L && R >= tree[Now].R)
{tree[Now].col = col; return; }
if (tree[Now].col > -1)
{
tree[tree[Now].lc].col =
tree[tree[Now].rc].col =
tree[Now].col;
tree[Now].col = -1;
}
int Mid = (tree[Now].L + tree[Now].R) >> 1;
if (L <= Mid) insert(tree[Now].lc, L, R, col);
if (Mid < R) insert(tree[Now].rc, L, R, col);
}
void count(int Now, int L, int R)
{
if (tree[Now].col > -1)
{marked.set(tree[Now].col); return; }
int Mid = (tree[Now].L + tree[Now].R) >> 1;
if (L <= Mid) count(tree[Now].lc, L, R);
if (Mid < R) count(tree[Now].rc, L, R);
}
void work()
{
scanf("%d%d%d", &n, &c, &m);
tot = 0;
Build(1, n);
for (; m; --m)
{
int x, y, z;
switch (getchar(), getchar())
{
case 'P' :
{
scanf("%d%d", &x, &y);
if (x > y) std::swap(x, y);
marked.reset();
count(1, x, y);
int cnt = 0;
for (int i = 1; i < c + 1; ++i)
if (marked.test(i)) ++cnt;
printf("%d\n", cnt);
break;
}
case 'C' :
{
scanf("%d%d%d", &x, &y, &z);
if (x > y) std::swap(x, y);
insert(1, x, y, z);
break;
}
}
}
}
int main()
{
init_file();
work();
exit(0);
}