Count Color
Time Limit: 1000MS Memory Limit: 65536K
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
题目大意:有一块足够长的板子,等长划分为1,2,…L,现在想要对这块板子进行两个操作。“C A B C”就是将A到B这一区间内的板子染色为C类型, “P A B”就是打印出来A到B区间内有多少种不同颜色的板子。颜色就用数字1,2,3…代表(初始化时板子颜色都为1)。输入L T O,L为板子数,T好像没啥用?O为操作数。
本题用到了类似于懒标记的延迟标记,当首次修改颜色时不会将col完全下传,下次查询时才会完全下传标记。每次查询的时候要对vis初始化。
核心代码:
update里的:
if(tree[rt].l >= x && tree[rt].r <= y){
// 找到了这个完全被包围的叶子,在此进行标记
tree[rt].col = colc;
return ;
}
query里的:
if(tree[rt].col > 0){
vis[tree[rt].col] = true; // 为这个数字的颜色标记,同色的不增加
return ;
}
AC代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1e6+10;
struct node{
int l, r;
int col;
}tree[4*maxn];
bool vis[35];
void build(int rt, int l, int r){
tree[rt].l = l, tree[rt].r = r;
if(l == r){
tree[l].col = 1;
return ;
}
int mid = tree[rt].l + tree[rt].r >> 1;
build(rt<<1, l, mid);
build(rt<<1|1, mid + 1, r);
}
// 下传标记
void down(int rt){
if(tree[rt].col){
tree[rt<<1].col = tree[rt].col;
tree[rt<<1|1].col = tree[rt].col;
tree[rt].col = 0;
}
}
void update(int rt, int x, int y, int colc){
if(tree[rt].l >= x && tree[rt].r <= y){
// 找到了这个完全被包围的叶子
tree[rt].col = colc;
return ;
}
else{
down(rt);
int mid = tree[rt].l + tree[rt].r >> 1;
if(mid >= y) update(rt<<1, x, y, colc);
else if(mid < x) update(rt<<1|1, x, y, colc);
else{
update(rt<<1, x, mid, colc);
update(rt<<1|1, mid+1, y, colc);
}
}
}
void query(int rt, int x, int y){
if(tree[rt].col > 0){
vis[tree[rt].col] = true; // 为这个数字的颜色标记,同色的不增加
return ;
}
else{
int mid = tree[rt].l + tree[rt].r >> 1;
if(mid >= y) query(rt<<1, x, y);
else if(mid < x) query(rt<<1|1, x, y);
else{
query(rt<<1, x, mid);
query(rt<<1|1, mid+1, y);
}
}
}
int main(){
int L, T, O, a, b, c;
char ch;
while(scanf("%d%d%d", &L, &T, &O) != EOF){
// tree[1].col = 1;
build(1, 1, L);
while(O--){
cin >> ch;
if(ch == 'C'){
scanf("%d%d%d", &a, &b, &c);
update(1, a, b, c);
}
else{
scanf("%d%d", &a, &b);
memset(vis, 0, sizeof(vis));
query(1, a, b);
int ans = 0;
for(int i = 0; i <= 35; i++)
if(vis[i]) ans++;
printf("%d\n", ans);
}
}
}
return 0;
}