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 ...
随机推荐
- windows ubuntu双系统启动
开始安装系统 1.安装系统 首先安装windows8和ubuntu12.04,安装先后顺序可以不用管,只是安装到ubuntu的分区时需要注意device for boot loader install ...
- Ubuntu 12.10安装OpenGL
http://wiki.ubuntu-tw.org/index.php?title=Howto_Install_OpenGL_Development_Environment 前言 OpenGL 是一套 ...
- Applying Eigenvalues to the Fibonacci Problem
http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/ The Fibonacci problem is a ...
- rwsr-sr-x类似权限设置
如何设置suid/guid? 如果希望设置suid,那么就将相应的权限位之前的那一位设置为4:如果希望设置guid,那么就将相应的权限位之前的那一位设置为2:如果希望两者都置位,那么将相应的权限位之前 ...
- linux基本命令(4)-8.Ubuntu-jdk+tomcat+eclipse软件包安装
第一步 安装jdk su - root 切换成root用户 sudo -i 不需要密码直接切换成root 1.进入usr目录 cd /usr 2.在usr目录下建立java安装目录 mkdir jav ...
- iOS-使用Xcode拉伸图片
如果要制作一个类似于QQ消息气泡的图片,该如何制作呢?android中可以使用.9图片指定图片中的某一部分拉伸,那iOS中类似的功能要如何实现呢,Xcode提供了类似的功能.具体步骤如下: 1.选择需 ...
- 寻虫记:BOM头制造的冤案,无故多出空白行
最近在做的一个网站发生了一个很诡异的BUG: 使用IE浏览页面时,一切都挺正常: 而使用Firefox浏览时,发现某些页面元素之间的距离比预期的要宽很多,HTML元素本身的hight.padding和 ...
- jsp 变量和方法的声明 Java程序片 HTML注释 JSP注释
<%!...%> 声明变量和方法 <%!...%>之中的变量为JSP页面的成员变量,当多个线程访问本页面时,多个线程共享此变量. <%@ page contentType ...
- 记Flume-NG一些注意事项(不定时更新,欢迎提供信息)
这里只考虑flume本身的一些东西,对于JVM.HDFS.HBase等得暂不涉及.... 一.关于Source: 1.spool-source:适合静态文件,即文件本身不是动态变化的: 2.avro ...
- codevs1688 求逆序对
题目描述 Description 给定一个序列a1,a2,…,an,如果存在i<j并且ai>aj,那么我们称之为逆序对,求逆序对的数目 数据范围:N<=105.Ai<=105. ...