题意:

给一个固定长度为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)的更多相关文章

  1. [POJ2777]Count Color(线段树)

    题目链接:http://poj.org/problem?id=2777 给你一个长为L想线段,向上面染色,颜色不超过30种,一共有O次操作,操作有两种: C a b c 在[a,b]上染上c颜色 P ...

  2. 【POJ2777】Count Color(线段树)

    以下是题目大意: 有水平方向上很多块板子拼成的墙,一开始每一块都被涂成了颜色1,有C和P两个操作,代表的意思是:C X Y Z —— 从X到Y将板子涂成颜色ZP X Y    —— 查询X到Y的板子共 ...

  3. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  4. PAT 甲级 1054 The Dominant Color (20 分)

    1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...

  5. pat 1054 The Dominant Color(20 分)

    1054 The Dominant Color(20 分) Behind the scenes in the computer's memory, color is always talked abo ...

  6. POJ 2777 Count Color(线段树染色,二进制优化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42940   Accepted: 13011 Des ...

  7. POJ - 2777——Count Color(懒标记线段树二进制)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53639   Accepted: 16153 Des ...

  8. POJ 2777 Count Color(线段树 + 染色问题)

    传送门:Count Color Description Chosen Problem Solving and Program design as an optional course, you are ...

  9. POJ-2777 Count Color(线段树,区间染色问题)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40510 Accepted: 12215 Descrip ...

随机推荐

  1. phpMyadmin /scripts/setup.php Remote Code Injection && Execution CVE-2009-1151

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 Insufficient output sanitizing when gener ...

  2. schemaLocation value = 'xxxxxxxxxxxx' must have even number of URI's

    这是因为没有加上Spring的版本号,加上就行了,如: http://www.springframework.org/schema/beans/spring-beans.xsd -3.2.2 http ...

  3. APNs详细使用步骤

    1. 什么是推送通知 消息通知分本地通知和远程推送通知,是没有运行在前台的应用程序可以让它们的用户获得相关消息通知的方式.消息通知可能是一条消息,即将发生的日历事件,或远程服务器的新数据.当被操作系统 ...

  4. iOS代码工具箱再续

    if (CGRectContainsPoint(self.menuView.frame, point)) { point =  [self.view convertPoint:point toView ...

  5. zoj3819Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  6. 嵌入式实时操作系统μCOS原理与实践任务控制与时间的解析

    /*************************************************************************************************** ...

  7. html5+监听设备加速度变化信息

    watchAcceleration 监听设备加速度变化信息 Number plus.accelerometer.watchAcceleration( successCB, errorCB, optio ...

  8. 2个比较经典的PHP加密解密函数分享

    项目中有时我们需要使用PHP将特定的信息进行加密,也就是通过加密算法生成一个加密字符串,这个加密后的字符串可以通过解密算法进行解密,便于程序对解密后的信息进行处理. 最常见的应用在用户登录以及一些AP ...

  9. 回顾bidirectional path tracing

    最近因为研究需要,回顾了一下BDPT,主要看VEACH的那篇论文,同时参考了pbrt,mitsuba的实现,自己写了一份新的bdpt实现.以前实现的那一份BDPT不是基于物理的,而且无法处理镜面和透明 ...

  10. 数据库客户端SQLeonardo的使用

    数据库客户端SQLeonardo的使用      这篇文章要介绍SQLeonardo.我使用了一下,挺不错,一个jar包,加载不同的驱动之后,可以连接很多类型的数据库,我只连接了HSQL和Oracle ...