poj 2777 Count Color - 线段树 - 位运算优化

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 42472   Accepted: 12850

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

C
P
C
P

Sample Output


Source


  讲讲题目大意(反正很多单词我也不懂)

  有一个板子长L厘米,以1cm为单位进行涂色,一共有T种颜色,木板初始的颜色是

编号为1的颜色(颜色编号从1标到T),有o个操作,操作分两种

1. "C A B C"  将[a,b]涂成颜色C(涂色是覆盖)
2. "P A B" 输出[a,b]不同的颜色

  初看这道题觉得可能是用线段树做,于是想出了如下几个问题

    1.每个区间存储什么?

    2.pushUppushDown怎么更新

  第一问比较好解决,就存颜色的信息,如果说存颜色的种数(查完就完了),有一

个很严重的问题,两个区间的颜色可能会有重复,更新的时候还要去判重,显得十分地

麻烦,干脆直接存各种颜色的是否在这个区间存在

  这样就好了,定个bool数组,更新的时候就合并,另外又出现了一个无法避免的问题——

超内存、超时(时间限制是1s!),bool类型是1字节,等于8二进制位,是不是有点过于浪费

内存。那就把它分割一下,分成4个bool类型,这样,究竟用哪一个呢?这样貌似又要浪费代码。

反正T也不大,也就30,于是我想到了int...

  就这样第一问就解决了,那怎么更新呢?总不可能相加,位运算中有一个运算是|(或运算)

这样用起来快,也方便,比如有两个区间它们的涂色情况如下:

//假设有4种颜色
[,]
[,]

  通过or运算后:

[,]    

  都不用考虑重复的问题了

  pushUp就用这个来,将左右子树上的颜色信息相or就得到了父节点的颜色信息

  结合则来考虑一下pushDown ,查询区间的时候,应该是把节点上的区间完全

覆盖的情况下才会更改延时更新,所以这个区间下的“子区间”颜色信息应该是一样的,

并且只有这一种颜色如果延时标记记录颜色的编号的话,那么应该这么向下更新

node->xxx->painted =  << (node->state - )

(xxx表示左子树或者右子树,state是延时标记,painted是颜色信息)

  如果不知道左移运算的可以看看下面

a =     

当 a <<= 2后:

a <<  =     

那如果a<<31会发生什么?

a =     

如果超过了它的二进制位这几位就没有了

  这里左移的意义在于将1(有这种颜色)移动到这种颜色对应的位置上,不然会发生可怕

的事情

  这些问题解决了,这道题也就可以做出来了

  附上算得超级慢的源代码:

 /**
* poj.org
* Problem#2777
* Aceepted
* Time:735ms
* Memory:9628k
*/
#include<iostream>
#include<cstdio>
using namespace std;
typedef class TreeNode{
private:
void init(){ //成员初始化
painted = ;
state = ;
left = NULL;
right = NULL;
}
public:
/**
* 用二进制来表示这一段涂过的颜色,
* 1表示涂了的颜色,0表示没有涂的
* 颜色 。用第i位表示第i种颜色
*/
int painted;
int state; //延时标记,更新第i种颜色
int from;
int end;
TreeNode* left;
TreeNode* right;
TreeNode(){ init(); }
TreeNode(int from, int end){
init();
this->from = from;
this->end = end;
}
}TreeNode;
typedef class Tree{
public:
TreeNode* root;
Tree():root(NULL){}
Tree(int size){
root = build(root, , size);
}
void pushUp(TreeNode* node){
node->painted = node->left->painted | node->right->painted;
}
void pushDown(TreeNode* node){ node->left->state = node->state;
node->left->painted = << (node->state - ); node->right->state = node->state;
node->right->painted = << (node->state - ); node->state = ; }
TreeNode* build(TreeNode *root, int from, int end){
root = new TreeNode(from, end);
if(from == end){
root->painted = 0x01;
return root;
}
int mid = (from + end) >> ;
root->left = build(root->left, from, mid);
root->right = build(root->right, mid + , end);
pushUp(root);
return root;
}
void update(TreeNode* now, int from, int end, int value){
if( from <= now->from && end >= now->end ){
now->state = value;
now->painted = << (value - );
return ;
}
if(now->state != ) pushDown(now);
int mid = (now->from + now->end) >> ;
if(end <= mid) update(now->left, from, end, value);
else if(from > mid) update(now->right, from, end, value);
else{
update(now->left, from, mid, value);
update(now->right, mid + , end, value);
}
pushUp(now);
}
int query(TreeNode* now, int from, int end){
if( from <= now->from && end >= now->end ) return now->painted;
if(now->state != ) pushDown(now);
int mid = (now->from + now->end) >> ;
if(end <= mid) return query(now->left, from, end);
else if(from > mid) return query(now->right, from, end);
else{
return query(now->left, from, mid) | query(now->right, mid + , end);
}
}
}Tree;
Tree board;
int len,color,n;
char ch;
int a,b,c;
void _swap(int& a,int& b){
int t = a;
a = b;
b = t;
}
int getSum(int x){
int result = ;
x &= ( << color) - ;
while(x != ){
result++;
x -= x&(-x);
}
return result;
}
int main(){
scanf("%d%d%d",&len,&color,&n);
board = Tree(len);
for(int i = ;i <= n;i++){
cin>>ch;
if(ch == 'C'){
scanf("%d%d%d",&a,&b,&c);
board.update(board.root, a, b, c);
}else{
scanf("%d%d",&a,&b);
if(a > b) _swap(a,b);
int x = board.query(board.root, a, b);
printf("%d\n",getSum(x));
}
}
return ;
}
上一篇:Mysql 导出数据库和指定表中的数据


下一篇:linux上部署javaWeb项目