POJ P2777 Count Color——线段树状态压缩
Description
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.
题目大意:
- #include<cstdio>
- using namespace std;
- const int MAXN=;
- int tree[MAXN<<];
- int lz[MAXN<<];
- int n,t,o,L,R;
- void up(int );
- void down(int ,int ,int );
- void col(int ,int ,int ,int );
- int ask(int ,int ,int );
- int ans(int );
- int main()
- {
- int i,j,k;
- char s[];
- scanf("%d%d%d",&n,&t,&o);
- lz[]=;
- tree[]=;
- for(i=;i<=o;i++){
- scanf("%s",s);
- if(s[]=='C'){
- scanf("%d%d%d",&L,&R,&j);
- if(L>=R)
- k=L,L=R,R=k;
- col(,n,,j);
- }
- else{
- scanf("%d%d",&L,&R);
- if(L>=R)
- k=L,L=R,R=k;
- j=ask(,n,);
- printf("%d\n",ans(j));
- }
- }
- }
- void up(int nu){
- tree[nu]=tree[nu<<]|tree[nu<<|];
- }
- void down(int l,int r,int nu){
- if(!lz[nu])return ;
- tree[nu<<]=lz[nu];
- tree[nu<<|]=lz[nu];
- lz[nu<<]=lz[nu<<|]=lz[nu];
- lz[nu]=;
- }
- void col(int l,int r,int nu,int x){
- if(L<=l&&r<=R){
- tree[nu]=lz[nu]=(<<(x-));
- return ;
- }
- down(l,r,nu);
- int mid=(l+r)>>;
- if(L<=mid)
- col(l,mid,nu<<,x);
- if(R>mid)
- col(mid+,r,nu<<|,x);
- up(nu);
- }
- int ask(int l,int r,int nu){
- if(L<=l&&r<=R)
- return tree[nu];
- down(l,r,nu);
- int ans=,mid=(l+r)>>;
- if(L<=mid)
- ans|=ask(l,mid,nu<<);
- if(R>mid)
- ans|=ask(mid+,r,nu<<|);
- return ans;
- }
- int ans(int x){
- int re=;
- while(x){
- if(x&)
- re++;
- x>>=;
- }
- return re;
- }
POJ P2777 Count Color——线段树状态压缩的更多相关文章
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- HDU 6183 Color it cdq分治 + 线段树 + 状态压缩
Color it Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Pro ...
- Count Color(线段树+位运算 POJ2777)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...
- POJ训练计划2777_Count Color(线段树/成段更新/区间染色)
解题报告 题意: 对线段染色.询问线段区间的颜色种数. 思路: 本来直接在线段树上染色,lz标记颜色.每次查询的话訪问线段树,求出颜色种数.结果超时了,最坏的情况下,染色能够染到叶子节点. 换成存下区 ...
随机推荐
- Linux之E: 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它?
解决 ubantu系统中 E: 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它? 的 问题. 1.解决办法: 当我们有的时候在使用apt-get install/update ...
- linux 命令 htop & 重定向 top, bashrc文件
最近在用linux服务器跑程序,有几条linux命令还蛮重要的,总结一下: 1. 直接跑代码: python test.py 2. 若想程序在后台跑,即使本地和服务器断开也能运行: nohup pyt ...
- 关于 unsigned long long 于 long long
long long 最大只有19位 : unsigned long long 最大有20位 ,原因牺牲了符合位来换取更大的记录
- (一)Audio子系统之AudioRecord.getMinBufferSize
在文章<基于Allwinner的Audio子系统分析(Android-5.1)>中已经介绍了Audio的系统架构以及应用层调用的流程,接下来,继续分析AudioRecorder方法中的ge ...
- 蓝桥杯-学霸的迷宫(BFS+记录操作)
算法提高 学霸的迷宫 时间限制:1.0s 内存限制:256.0MB 问题描述 学霸抢走了大家的作业,班长为了帮同学们找回作业,决定去找学霸决斗.但学霸为了不要别人打扰,住在一个城堡 ...
- 基于Cython和内置distutils库,实现python源码加密(非混淆模式)
起因 python本身只能做混淆,不能加密,多年的商业软件开发经验导致有某种"洁癖"欲望,将py编译打包 尝试 pyinstaller原理是freeze打包pyc文件,利用工具可完 ...
- python GIL(Global Interpreter Lock)
一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...
- weiFenLuo.winFormsUI.Docking.dll学习
引用方法: 1.建立一个WinForm工程,默认生成了一个WinForm窗体. 2.引用—>添加引用—>浏览—>weiFenLuo.winFormsUI.Docking.dll. 3 ...
- [Xamarin] 開啟另外一個Activity 並且帶資料 (转帖)
每隻App是透過許多畫面所組成的,當然可能主畫面之外,都會有許多其他的頁面 再Android 設計中畫面會有配合的Activity 當然在這之前,最好事先了解一下,Android 關於生命週期的規劃 ...
- 下载windows版本apache网页服务器
1.进入apache服务器官网http://httpd.apache.org/,这里我们以下载稳定版的httpd 2.4.25为例,点击download. 2.点击链接Files for Micros ...