Count Color(poj 2777)
题意:
给一个固定长度为L的画板
有两个操作:
C A B C:区间AB内涂上颜色C。
P A B:查询区间AB内颜色种类数。
分析:显然是要用线段树来操作的,设定一个sum[]来维护一个区间内的颜色,若为-1,则说明不是叶子节点,还应在往底下找;
同时在查询时维护一个vis[]来记录在这个区间内哪个颜色被使用过了。
#include<cstdio>
#include<iostream>
#include<cstring>
#define lson l,m,now*2
#define rson m+1,r,now*2+1
#define M 400010
#define N 35
using namespace std;
int vis[N],sum[M];//当sum[i]=-1是,说明不是底下至少有2种颜色,否则sum[i]是i的颜色
void build(int l,int r,int now)
{
sum[now]=;
if(l==r)return;
int m=(l+r)/;
build(lson);
build(rson);
}
void push_down(int now)
{
sum[now*]=sum[now];
sum[now*+]=sum[now];
sum[now]=-;
}
void change(int x,int y,int v,int l,int r,int now)
{
if(l>=x&&r<=y)
{
sum[now]=v;
return;
}
if(sum[now]==v)return;
if(sum[now]!=-)push_down(now);
int m=(l+r)/;
if(x<=m)change(x,y,v,lson);
if(y>m)change(x,y,v,rson);
}
void query(int x,int y,int l,int r,int now)
{
if(sum[now]!=-)
{
vis[sum[now]]=;
return;
}
int m=(l+r)/;
if(x<=m)query(x,y,lson);
if(y>m)query(x,y,rson);
}
int main()
{
int n,m,p;
scanf("%d%d%d",&n,&m,&p);
build(,n,);
for(int i=;i<=p;i++)
{
char c;
cin>>c;
if(c=='C')
{
int a,b,v;
scanf("%d%d%d",&a,&b,&v);
if(a>b)swap(a,b);
change(a,b,v,,n,);
}
else
{
memset(vis,,sizeof(vis));
int a,b,tot=;
scanf("%d%d",&a,&b);
if(a>b)swap(a,b);
query(a,b,,n,);
for(int i=;i<=;i++)
if(vis[i])tot++;
printf("%d\n",tot);
}
}
return ;
}
Count Color(poj 2777)的更多相关文章
- [POJ2777]Count Color(线段树)
题目链接:http://poj.org/problem?id=2777 给你一个长为L想线段,向上面染色,颜色不超过30种,一共有O次操作,操作有两种: C a b c 在[a,b]上染上c颜色 P ...
- 【POJ2777】Count Color(线段树)
以下是题目大意: 有水平方向上很多块板子拼成的墙,一开始每一块都被涂成了颜色1,有C和P两个操作,代表的意思是:C X Y Z —— 从X到Y将板子涂成颜色ZP X Y —— 查询X到Y的板子共 ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- PAT 甲级 1054 The Dominant Color (20 分)
1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...
- pat 1054 The Dominant Color(20 分)
1054 The Dominant Color(20 分) Behind the scenes in the computer's memory, color is always talked abo ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
- POJ - 2777——Count Color(懒标记线段树二进制)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53639 Accepted: 16153 Des ...
- POJ 2777 Count Color(线段树 + 染色问题)
传送门:Count Color Description Chosen Problem Solving and Program design as an optional course, you are ...
- POJ-2777 Count Color(线段树,区间染色问题)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40510 Accepted: 12215 Descrip ...
随机推荐
- Java初学(七)
一.内部类 1.内部类概述:把类定义在其他类内部,这个类被称为内部类(内部类可以使用static修饰,外部类不可) 2.内部类访问特点:内部类可以直接访问外部类成员,包括私有的 外部类要访问内 ...
- javascript 的button onclick事件不起作用的解决方法
在项目中遇到个问题:servlet向前端返回如下按钮,当course_ID为数字是onclick事件正常,但当course_ID含有字母时onclick事件就不起作用.网上找了很多方法都不管用,最后自 ...
- sed 指令
sed -e 's/:/ /g' 将待处理文本行中:替换为空格, s/A/B/g 是sed中的替换命令, 将A替换为B, 其中,A可以是正则表达式. g表示全部替换. sed 指令 瀏覽數 : 6,5 ...
- C#表达式树的初步了解
在很早以前就听说过表达式树了,但并没有去了解它.虽然自己用过linq to sql和linq to entity,但也就用着就用着,并没有去深究c#代码怎么会生成sql代码而不是IL.废话不多说了,开 ...
- Sixth scrum meeting - 2015/10/31
概述 今天是周末,我们小组由于之前拖延的比较久,所以今天仍然在努力的开发…… 目前开发已经到了中期阶段,今天遇到了一个问题就是,由于小组的某些同学对git的使用不太熟悉,导致在git push的时候遇 ...
- js矩阵菜单或3D立体预览图片效果
js矩阵菜单或3D立体预览图片效果 下载地址: http://files.cnblogs.com/elves/js%E7%9F%A9%E9%98%B5%E8%8F%9C%E5%8D%95%E6%88% ...
- MyBatis 3源码分析
Mybatis3.2源码分析: 一.加载配置文件. 使用SAX解析配置文件.读取xml配置文件后,调用XMLConfigBuilder.parse()方法,在parse方法中再调用parseC ...
- 淘宝(阿里百川)手机客户端开发日记第六篇 Service详解(二)
DEMO1:当我们点击启动服务时和点击停止服务的时候,观察服务的运行状态,布局由于简单,只是两个普通的Button按钮,在此我只上截图. java代码部分 第一步:我们需要实现一个服务类,继承自ser ...
- QEMU-KVM中的多线程压缩迁移技术
导读 目前的迁移技术,都是通过向QEMUFILE中直接写入裸内存数据来达到传送虚拟机的目的端,这种情况下,发送的数据量大,从而会导致更高的迁移时间(total time)和黑宕时间(downtime) ...
- [BZOJ1177][Apio2009]Oil
[BZOJ1177][Apio2009]Oil 试题描述 采油区域 Siruseri政府决定将石油资源丰富的Navalur省的土地拍卖给私人承包商以建立油井.被拍卖的整块土地为一个矩形区域,被划分为M ...