博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2777 Count Color (线段树)
阅读量:6540 次
发布时间:2019-06-24

本文共 3464 字,大约阅读时间需要 11 分钟。

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

2 2 4

C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output

2

1

题意:

C操作就是给一排板子涂上一种颜色,P操作就是查询区间内不同颜色的数量。

题解:

由于颜色很少,只有30种,所以可以用二进制数模拟当前颜色数量的状态。

有意思的是,刚好卡在1000ms但是AC了。

1157228-20171008115001074-2109226078.png

#include
#include
#include
#include
using namespace std;const int maxn=1e5+5;#define lson L,mid,rt<<1#define rson mid+1,R,rt<<1|1int seg[maxn<<2];bool lazy[maxn<<2];void build(int L,int R,int rt){ seg[rt]=1; if(L==R) return ; int mid=(L+R)>>1; build(lson); build(rson);}void push_up(int rt){ seg[rt]=seg[rt<<1]|seg[rt<<1|1];}void push_down(int rt)//lazy操作{ seg[rt<<1]=seg[rt<<1|1]=seg[rt]; lazy[rt<<1]=lazy[rt<<1|1]=true; lazy[rt]=false;}void updata(int L,int R,int rt,int l,int r,int val){ if(L>=l&&R<=r) { seg[rt]=val; lazy[rt]=true; return ; } if(seg[rt]==val)//剪枝。已经是同种颜色,不需要再涂了 return ; if(lazy[rt]) push_down(rt); int mid=(L+R)>>1; if(l>mid) updata(rson,l,r,val); else if(r<=mid) updata(lson,l,r,val); else updata(lson,l,r,val),updata(rson,l,r,val); push_up(rt);//更新}int sum;void query(int L,int R,int rt,int l,int r){ if(L>=l&&R<=r) { sum|=seg[rt]; return ; } if(lazy[rt])//剪枝。当前区间被一种颜色所覆盖,不用再向下分解区间 { sum|=seg[rt]; return ; } int mid=(L+R)>>1; if(l>mid) query(rson,l,r); else if(r<=mid) query(lson,l,r); else query(lson,l,r),query(rson,l,r);}int cal(int num)//计算有多少种颜色{ int ans=0; while(num) { if(num&1) ans++; num>>=1; } return ans;}int main(){ ios::sync_with_stdio(false); int n,t,q; cin>>n>>t>>q; build(1,n,1); while(q--) { char op; cin>>op; int x,y,c; if(x>y) swap(x,y); if(op=='C') { cin>>x>>y>>c; updata(1,n,1,x,y,1<<(c-1)); } else { cin>>x>>y; sum=0; query(1,n,1,x,y); cout<
<

转载于:https://www.cnblogs.com/orion7/p/7636952.html

你可能感兴趣的文章
ubuntu 12.04系统托盘不显示ibus输入法图标的解决方法
查看>>
WSDP
查看>>
Memory Management
查看>>
The Packaging Process in Yocto/OE
查看>>
JQUERY 对 表格中的数据重排序
查看>>
程序员常用借口指南
查看>>
关于PXE网络安装linux系统中碰到的个别问题
查看>>
awk 常用方法
查看>>
Android网络框架实现之【Retrofit+RxJava】
查看>>
Android文件的加密与解密
查看>>
SOAP webserivce 和 RESTful webservice 对比及区别
查看>>
【原】记录一句话
查看>>
Android标题栏,状态栏
查看>>
三数中值快速排序(长度小于3的数组转插入排序)
查看>>
Windows下安装Memcached for PHP
查看>>
hdu 1040 As Easy As A+B
查看>>
java笔记:SpringSecurity应用(二)
查看>>
vim命令
查看>>
php记录代码执行时间
查看>>
【C】strcpy()需谨慎使用;
查看>>