Count Color
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 46667 | Accepted: 14137 |
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.
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
分析:这题和普通的线段树区间更新一样,只不过在存父亲节点的时候要用一个小技巧。用按位或来存,当然每个子节点对应的也不能是存1-30这样的整数,要存(1<<0)--(1<<29)这样的数,因为数字不是太大,所以这样的数Int就可以存下。最后算每个数二进制有多少个1就是有多少不同的颜色。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=101000;
int tr[4*N];
int add[4*N];
void PushUp(int i)
{
tr[i]=tr[2*i]|tr[2*i+1];
}
void PushDown(int i)
{
if(add[i])
{
add[i<<1]=add[i<<1|1]=add[i];
tr[i<<1]=tr[i<<1|1]=1<<(add[i]-1);
add[i]=0;
}
}
void build(int i,int l,int r)
{
tr[i]=1;
add[i]=0;
if(l==r)
{
return;
}
int mid=(l+r)/2;
build(2*i,l,mid);
build(2*i+1,mid+1,r);
PushUp(i);
}
void update(int i,int l,int r,int x,int y,int c)
{
if(x<=l&&r<=y)
{
tr[i]=(1<<(c-1));
add[i]=c;
return;
}
PushDown(i);
int mid=(l+r)/2;
if(x<=mid) update(2*i,l,mid,x,y,c);
if(y>mid) update(2*i+1,mid+1,r,x,y,c);
PushUp(i);
}
int query(int i,int l,int r,int x,int y)
{
if(x<=l&&r<=y)
{
return tr[i];
}
PushDown(i);
int ans=0;
int mid=(l+r)/2;
if(x<=mid) ans|=query(2*i,l,mid,x,y);
if(y>mid) ans|=query(2*i+1,mid+1,r,x,y);
return ans;
}
int cal(int num)
{
int ans=0;
while(num)
{
if(num%2==1)
ans++;
num/=2;
}
return ans;
}
int main()
{
int l,t,o,x,y,c;
while(~scanf("%d%d%d",&l,&t,&o))
{
build(1,1,l);
for(int i=1;i<=o;i++)
{
char a[5];
scanf("%s",a);
if(a[0]=='C')
{
scanf("%d%d%d",&x,&y,&c);
if(x>y) swap(x,y);
update(1,1,l,x,y,c);
}
else
{
scanf("%d%d",&x,&y);
if(x>y) swap(x,y);
int num=query(1,1,l,x,y);
int ans=cal(num);
printf("%d\n",ans);
}
}
}
return 0;
}
本文介绍了一种使用线段树进行区间更新与不同颜色数量查询的算法实现。通过按位或操作存储节点信息,并利用二进制位计数统计不同颜色的数量,解决了长板上颜色变化及查询的问题。
580

被折叠的 条评论
为什么被折叠?



