poj 2777 Count Color(线段树 区间更新)
2015-10-07 00:59
447 查看
Language: Default Count Color
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 |
难点在于如果表示一个区间内的气球颜色个数
在这里 因为总的气球颜色较少 可以用int整数来表示区间内气球染色的情况 换成2进制 如果改为为1 则这位上有这个颜色的气球 添加一种颜色的时候 使用位运算|
最后就可以求得这个区间内气球染色的情况 判断这个整数中共有几个1 就有几个颜色了
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 100010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char c = getchar();
while (c < '0' || c > '9') c = getchar();
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + c - '0';
c = getchar();
}
return x;
}
void Print(int a)
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
struct node
{
int l,r;
int col,same;
};
node no[MAXN*4];
void build(int l,int r,int k)
{
no[k].l=l;
no[k].r=r;
no[k].col=1;
no[k].same=1;//用来标记这个区间里面的颜色是不是一样的
if(l==r)
return;
int mid=(l+r)/2;
build(l,mid,k*2);
build(mid+1,r,k*2+1);
}
void pushdown(int k)
{
if(no[k].same)
{
no[k*2].same=no[k*2+1].same=1;
no[k*2].col=no[k*2+1].col=no[k].col;
no[k].same=0;
return;
}
}
void update(int l,int r,int k,int col)
{
if(l<=no[k].l&&no[k].r<=r)
{
no[k].col=col;
no[k].same=1;
return;
}
pushdown(k);
int mid=(no[k].l+no[k].r)/2;
if(r<=mid) update(l,r,k*2,col);
else if(l>mid) update(l,r,k*2+1,col);
else
{
update(l,mid,k*2,col);
update(mid+1,r,k*2+1,col);
}
no[k].col=no[2*k].col|no[k*2+1].col;
}
int ans;
void query(int l,int r,int k)
{
if(no[k].l>=l&&no[k].r<=r)
{
ans|=no[k].col;
return;
}
pushdown(k);
int mid=(no[k].l+no[k].r)/2;
if(r<=mid) query(l,r,k*2);
else if(l>mid) query(l,r,k*2+1);
else
{
query(l,mid,k*2);
query(mid+1,r,k*2+1);
}
}
int getcnt()
{
int cnt=0;
while(ans)
{
if(ans&1)
cnt++;
ans>>=1;
}
return cnt;
}
int main()
{
// fread;
int n,t,q;
while(scanf("%d%d%d",&n,&t,&q)!=EOF)
{
build(1,n,1);
while(q--)
{
int l,r,num;
char ch[10];
scanf("%s",ch);
if(ch[0]=='C')
{
scanf("%d%d%d",&l,&r,&num);
if(l>r) swap(l,r);
update(l,r,1,1<<(num-1));
}
else
{
scanf("%d%d",&l,&r);
ans=0;
if(l>r) swap(l,r);
query(l,r,1);
printf("%d\n",getcnt());
}
}
}
return 0;
}
相关文章推荐
- 架构师内练基础
- 电商项目系列文档(一)
- 华为oj_DNA序列
- 流和文件的区别
- heredoc和nowdoc的区别
- BTrace 使用总结
- LeetCode---Linked List Cycle II
- Linux守护进程的编程实现
- [iOS开发项目-8] 喜马拉雅电台
- 13. 查看数据库对象间的依赖关系
- UVA 1589 Xiangqi
- 斐波那契数列
- 关于web移动端返回到顶部的解决方案
- 闭合浮动方法
- 微软必应词典客户端的案例分析
- BZOJ1807 : [Ioi2007]Pairs 彼此能听得见的动物对数
- 浅谈HTTP中Get与Post的区别
- RTP、RTCP及媒体流同步
- Java中数据类型的转换
- 自制手机指南针