poj 2777
来源:互联网 发布:淘宝 瞄准镜 编辑:程序博客网 时间:2024/05/30 23:33
Count Color
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 35280 Accepted: 10633
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 4C 1 1 2P 1 2C 2 2 2P 1 2
Sample Output
21
Source
POJ Monthly--2006.03.26,dodo
直接上代码了,应该看的懂吧。
AC代码:
#include<iostream>#include<stdio.h>using namespace std;struct Node{ int left,right; int num;}a[400010];int sum;void built(int cur,int x,int y){ a[cur].left=x; a[cur].right=y; a[cur].num=1; if(x==y) return ; int mid=(x+y)>>1; built(cur<<1,x,mid); built(cur<<1|1,mid+1,y);}void update(int cur,int x,int y,int val){ if(a[cur].left==x && a[cur].right==y){ a[cur].num=val; return ; } if(a[cur].num){ //当前位置全覆盖,所以要向下更新,并把本节点置0 a[cur<<1].num=a[cur<<1|1].num=a[cur].num; a[cur].num=0; } int mid=(a[cur].left+a[cur].right)>>1; if(y<=mid) update(cur<<1,x,y,val); else if(x>mid) update(cur<<1|1,x,y,val); else{ update(cur<<1,x,mid,val); update(cur<<1|1,mid+1,y,val); }}void query(int cur,int x,int y){ if(a[cur].num && a[cur].left<=x && y<=a[cur].right){ sum|=a[cur].num; return ; } int mid=(a[cur].left+a[cur].right)>>1; if(y<=mid) query(cur<<1,x,y); else if(x>mid) query(cur<<1|1,x,y); else{ query(cur<<1,x,mid); query(cur<<1|1,mid+1,y); }}int main(){ int n,t,m; while(scanf("%d%d%d",&n,&t,&m)!=EOF){ built(1,1,n); while(m--){ char s[2]; scanf("%s",s); if(s[0]=='C'){ int x,y,k; scanf("%d%d%d",&x,&y,&k); if(x>y){ int tmp=x; x=y; y=tmp; } update(1,x,y,1<<(k-1)); //因为颜色较少,所以用二进制表示颜色,方便求种类(这样种类只要取或运算得到之后看里面有多少个1就行了) } else{ int x,y; scanf("%d%d",&x,&y); if(x>y){ int tmp=x; x=y; y=tmp; } sum=0; query(1,x,y); int ans=0; while(sum){ //取sum里面1的个数 if(sum&1) ans++; sum>>=1; } cout<<ans<<endl; } } } return 0;}
0 0
- poj 2777
- poj 2777
- poj 2777
- poj 2777
- poj 2777
- POJ 2777
- POJ 2777
- poj-2777
- poj 2777
- poj 2777
- POJ 2777
- poj 2777
- poj 2777
- poj 2777
- Poj 2777
- poj 2777
- poj 2777
- POJ-2777
- 【Kali_014】Kconsole虚拟终端设置半透明
- 内存溢出之PermGen OOM深入分析
- Servlet的简单总结
- C++异常机制知识点
- amaomiaoww的专栏
- poj 2777
- SAP传输请求的操作步骤
- 关注社区微信得下载分
- 日语交流
- One usage of recurison: the tower of Hanoi
- 有趣的机器学习:最简明入门指南
- maven+springMVC+mybatis+junit详细搭建过程
- linux命令 统计一篇文章中每个字符串的个数
- Android中visibility属性VISIBLE、INVISIBLE、GONE的区别