POJ训练计划2777_Count Color(线段树/成段更新/区间染色)
解题报告
题意:
对线段染色。询问线段区间的颜色种数。
思路:
本来直接在线段树上染色,lz标记颜色。每次查询的话訪问线段树,求出颜色种数。结果超时了,最坏的情况下,染色能够染到叶子节点。
换成存下区间的颜色种数,这样每次查询就不用找到叶子节点了。用按位或来处理颜色种数。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int sum[500000],lz[500000],ans;
void push_down(int rt,int l,int r)
{
if(lz[rt])
{
lz[rt<<1]=lz[rt<<1|1]=lz[rt];
sum[rt<<1]=lz[rt];
sum[rt<<1|1]=lz[rt];
lz[rt]=0;
}
}
void push_up(int rt,int l,int r)
{
sum[rt]=sum[rt<<1]|sum[rt<<1|1];
}
void cbtree(int rt,int l,int r)
{
if(l==r)
{
sum[rt]=1;
return ;
}
int mid=(l+r)>>1;
cbtree(rt<<1,l,mid);
cbtree(rt<<1|1,mid+1,r);
push_up(rt,l,r);
}
void update(int rt,int l,int r,int ql,int qr,int v)
{
if(ql>r||qr<l)return ;
if(ql<=l&&r<=qr)
{
lz[rt]=v;
sum[rt]=v;
return ;
}
int mid=(l+r)>>1;
push_down(rt,l,r);
update(rt<<1,l,mid,ql,qr,v);
update(rt<<1|1,mid+1,r,ql,qr,v);
push_up(rt,l,r);
}
int _q(int rt,int l,int r,int ql,int qr)
{
if(ql>r||qr<l)return 0;
if(ql<=l&&r<=qr)
{
return sum[rt];
}
push_down(rt,l,r);
int mid=(l+r)>>1;
return _q(rt<<1,l,mid,ql,qr) | _q(rt<<1|1,mid+1,r,ql,qr); }
int main()
{
int n,t,q,ql,qr,i,j,a,k;
char str[10];
scanf("%d%d%d",&n,&t,&q);
cbtree(1,1,n);
while(q--)
{
scanf("%s",str);
if(str[0]=='C')
{
scanf("%d%d%d",&ql,&qr,&a);
if(ql>qr)swap(ql,qr);
update(1,1,n,ql,qr,1<<(a-1));
}
else
{
scanf("%d%d",&ql,&qr);
if(ql>qr)swap(ql,qr);
ans=_q(1,1,n,ql,qr);
int cnt=0;
while(ans)
{
if(ans%2)
cnt++;
ans/=2;
}
printf("%d\n",cnt);
}
}
return 0;
}
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 35143 | Accepted: 10591 |
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.
Input
be larger than B) as an operation defined previously.
Output
Sample Input
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
Sample Output
2
1
POJ训练计划2777_Count Color(线段树/成段更新/区间染色)的更多相关文章
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- poj 3669 线段树成段更新+区间合并
添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- poj 3648 线段树成段更新
线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...
- ZOJ 1610 Count the Colors (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
随机推荐
- android 点击桌面图标,打开手机浏览器进入对应的站点
做一个假的adnroid app.要实现点击桌面图标.打开手机浏览器进入对应的站点,实现方法非常easy import android.app.Activity; import android.con ...
- 不可视对象的自己主动实例化BUG
PB有个隐藏BUG会占用内存.影响效率. 先来做个样例吧 (1)创建一个不可视对象n_base,勾选Autolnstantiate属性 初始化事件constructor里面写messagebox('c ...
- 可失败构造器(Failable Initializers)
Xcode6.1中Swift的最新版本是1.1,在该版本中引入了一个新的特性:可失败构造器.通过构造器初始化实际上是给class或struct的每一个存储属性(参数)提供初始值,进行对象实例化的过程. ...
- oracle 双机热备,oracle dataguard 和oracle rac的区别和联系(转)
Data Guard 是Oracle的远程复制技术,它有物理和逻辑之分,但是总的来说,它需要在异地有一套独立的系统,这是两套硬件配置可以不同的系统,但是这两套系统的软件结构保持一致,包括软件的版本,目 ...
- java基础之代理
代理的定义,代理的应用,代理的特性
- .NET基础拾遗(4)委托和事件2
事件 事件是对象发送的消息,以发信号通知操作的发生.操作可能是由用户交互(例如鼠标单击)引起的,也可能是由某些其他的程序逻辑触发的. 引发事件的对象称为事件发送方.捕获事件并对其作出响应的对象叫做事件 ...
- avalon学习笔记一 列表及条件过滤
好长时间都没有更新博客了,不是因为没有学习新的东西,而是到了新的单位每天玩命加班实在是太累了!经过一年的努力吧,终于可以轻松一下了.废话少说,直接干货吧! 由于是学习阶段,就直接拿了公司的二级页面做了 ...
- EF中加载实体的方式
EF中的查询执行时机:1. foreach进行枚举2. ToArray.ToList.ToDictionary3. Linq的一些操作,如First.Any4. DbSet上的Load操作.DbEnt ...
- redis的备份和恢复
Redis 数据备份与恢复 数据备份 语法 redis Save 命令基本语法如下: redis > SAVE 实例 redis > SAVE OK 该命令将在 redis 备份目录中创建 ...
- MfC基础--绘图基础--win32
1.vc使用的控件分为三类: windows标准控件--MFC对这些进行了再封装 ActiveX 控件 其他MFC控件类 2.CWind是所有窗口的基类 3.GDI也属于一种API,主要用于绘图,(G ...