Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 53757 Accepted: 16183
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:
- “C A B C” Color the board from segment A to segment B with color C.
- “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
问题描述
输入L,T,O,分别表示从左到右的长度,颜色最多的种数和操作的个数。然后输入O行操作,如果是“C a b c” 意味着对每个下标从a到b的地方刷成颜色c。如果是“P a b”意味着输出下标从a到b的颜色种数。
问题分析
利用线段树区间访问和区间修改的功能可以解决。因为颜色的最大种数为30,所以可以用整型数转化为二进制对颜色的序号进行记录。如:整数5对应的二进制是0…0101,可以表示1和3都在,其它数都不在里面,判断元素在不在里面就要用到掩码,位运算与和左移操作,所以这种记录方法比用数组记录可以节省很多空间,也很方便。
一开始写的时候,最大的问题是不知道懒标记该怎么用,因为不像以前的那种计数叠加,一直放在那,直到要用的时候下传就完事了。这次是直接覆盖那部分颜色,理清关系以后好像变得简单了点。尽管是直接覆盖但还是要用到懒标记,因为懒标记是对两个子区间记录,将记录放在那里,就可以不用遍历完所有的子节点,所以用懒标记省时间就省在这。
要区分的是,哪里该完全覆盖,哪里该更新。如果当前区域完全在要刷的区域里,那么将它直接覆盖;如果当前区域只是部分区域在要刷的区域中,那么要依据它的子区间更新。先让当前节点下传,根据mid在要刷的区域的位置,访问左区间和右区间,等到左右区间处理完回溯后,将该区域的颜色清零,把左右区间的颜色填入其中,这就是更新了。
需要注意的是:
初始颜色为1。
题目中输入的A,B,可能有A>B的情况,这时需要交换。
理清颜色序号和颜色集合的关系。
程序如下
#include<iostream>
#include<cstdio>
using namespace std;
typedef unsigned int ui;
const int N = 1e5;
int len, t, onum;
struct node
{
int L, R, color, laze;
}tree[4 * N + 1];
void down(int k)//下传
{
tree[2 * k].color = tree[2 * k].laze = tree[k].laze;
tree[2 * k + 1].color = tree[2 * k + 1].laze = tree[k].laze;
tree[k].laze = 0;
}
void color_num(int&color, const int&other)//颜色记录
{
if (color == 0)
{
color = other;
return;
}
int mask;
mask = 1;//掩码
for (int i = 1; i <= t; i++)
{
if ((mask&other) && !(mask&color)) color += mask;//填色
mask <<= 1;//掩码左移
}
}
int transfrom(const int obj)//转化为颜色个数
{
int mask = 1, count = 0, i;
for (i = 1; i <= t; i++)
{
if (mask&obj) count++;//如果有就加1
mask <<= 1;
}
return count;
}
void build(int l, int r, int k)//建树
{
tree[k].L = l;
tree[k].R = r;
if (l == r)
{
tree[k].color = 1;//初始颜色
tree[k].laze = 0;
return;
}
int mid = (l + r) / 2;
build(l, mid, 2 * k);
build(mid + 1, r, 2 * k + 1);
tree[k].color = 1;
tree[k].laze = 1;
}
void ask(const int&l, const int&r, int k, int&ans)//求个数
{
if (tree[k].L >= l&&tree[k].R <= r)
{
color_num(ans, tree[k].color);
return;
}
if (tree[k].laze) down(k);//下传
int mid = (tree[k].L + tree[k].R) / 2;
if (mid >= l) ask(l, r, 2 * k, ans);
if (mid < r) ask(l, r, 2 * k + 1, ans);
}
void change(const int&l, const int&r, int k, const int&add)
{
if (tree[k].L >= l&&tree[k].R <= r)
{
int temp = 1;
tree[k].color = temp << (add - 1);//将颜色编号转化对应的集合,颜色直接覆盖
tree[k].laze = tree[k].color;
return;
}
if (tree[k].laze) down(k);
int mid = (tree[k].L + tree[k].R) / 2;
if (mid >= l) change(l, r, 2 * k, add);
if (mid < r) change(l, r, 2 * k + 1, add);
tree[k].color = 0;//颜色更新(从下往上计数)
color_num(tree[k].color, tree[2 * k].color);
color_num(tree[k].color, tree[2 * k + 1].color);
}
int main()
{
char ch;
int l, r, add;
scanf("%d %d %d", &len, &t, &onum);
build(1, len, 1);//建树
while (onum--)
{
getchar();
scanf("%c", &ch);
if (ch == 'C')
{
scanf("%d %d %d", &l, &r, &add);
if (l > r) swap(l, r);//没注意到,题目说A可能比B大
change(l, r, 1, add);
}
else
{
int ans, num;
scanf("%d %d", &l, &r);
if (l > r) swap(l, r);
ask(l, r, 1, ans = 0);
num = transfrom(ans);
printf("%d\n", num);
}
}
return 0;
}