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 ...
随机推荐
- Python input()和raw_input()的区别
当输入为数字的时候,input()获得的是数字,而后者获得的是str,可以用int(raw_input())来转换. i = input() print i+1 j = raw_input() pri ...
- 轻量级应用开发之(04)UIScrollView-1
本文是我在学习OC中的一些经验总结,在学习中总结了常用的Mac技巧,欢迎群友对本文提出意见,如有问题请联系我. 一 什么是UIScrollView 1)移动设备的屏幕大小是极其有限的,因此直接展示在用 ...
- Socket 入门- 客户端回射程序
结果输出:------------------------------------------------------客户端:xx@xxxxxx:~/Public/C$ ./postBackCli.o ...
- Docker系列之(三):Docker微容器Alpine Linux
1. 前言 使用Docker创建容器时,基础镜像通常选择Ubuntu或Centos,不管哪个镜像的大小都在100MB以上. Alpine Linux是一个面向安全的轻型的Linux发行版. Alpin ...
- JS 循环遍历JSON数据
JSON数据如:{"options":"[{/"text/":/"王家湾/",/"value/":/" ...
- 不用插件直接同步wordpress文章日志到新浪微博
社会化媒体营销可以为网站带来流量,如果你的社会化媒体账号的粉丝技术够大的话!社会化媒体营销国内做得比较好的算新浪微博了.那么我们要怎样同步wordpress文章日志到新浪微博呢?当然你可以使用插件来实 ...
- editplus快捷键大全之editplus文件快捷键
editplus快捷键大全之editplus文件快捷键 新建普通文本 Ctrl+N 新建普通的文本文档 新建浏览器窗口 Ctrl+Shift+B 新建浏览器窗口 新建 HTML 页 Ctrl+Shif ...
- linux下vim命令详解 转自: zhanglong0426
高级一些的编辑器,都会包含宏功能,vim当然不能缺少了,在vim中使用宏是非常方便的: :qx 开始记录宏,并将结果存入寄存器xq 退出记录模式@x 播放记录在x寄存器中的 ...
- PHP快速抓取快递信息
<?php header("Content-type:text/html;charset=utf-8"); /** * Express.class.php 快递查询类 * @ ...
- 查看一些特定sql需求的书写
user表,5个人abcde, content表10篇文章,一个人对应两篇文章,有 time字段,查询出五个人的最新文章. select a.id,a.SName,a.ClsNo,a.Scorefr ...