poj 2777 Count Color(线段树 区间更新)
题目:http://poj.org/problem?id=2777
区间更新,比点更新多一点内容, 详见注释, 参考了一下别人的博客。。。。
参考博客:http://www.2cto.com/kf/201402/277917.html
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
const int maxn = + ; bool mark[];
struct node
{
int l, r, kind;
}tr[maxn<<]; void build(int t, int l, int r) //建树
{
tr[t].l = l;
tr[t].r = r;
tr[t].kind = ;
if(l == r) return;
int mid = (l+r)>>;
build(*t, l, mid);
build(*t+, mid+, r);
}
void update(int t, int l, int r, int cover) //区间颜色更新
{
if(tr[t].l == l&& tr[t].r == r) //找到区间改颜色
{
tr[t].kind = cover;
return;
}
if(tr[t].kind != && tr[t].kind != cover) //区间是纯色,而且不是目标颜色
{
tr[*t].kind = tr[t].kind;
tr[*t+].kind = tr[t].kind; //更新子树颜色
tr[t].kind = ; //标记为混合色
}
int mid = (tr[t].l + tr[t].r)>>;
if(r <= mid)
update(*t, l, r, cover);
else if(l > mid)
update(*t+, l, r, cover);
else //左右子树各有一段
{
update(*t, l, mid, cover);
update(*t+, mid+, r, cover);
}
}
void query(int t, int l, int r)
{
if(tr[t].kind > ) //该段为纯色,不用向下搜了
{
mark[tr[t].kind] = true;
return;
}
int mid = (tr[t].l + tr[t].r)>>;
if(r <= mid)
query(*t, l, r);
else if(l > mid)
query(*t+, l, r);
else
{
query(*t, l, mid);
query(*t+, mid+, r);
}
}
int main()
{
int n, color, m, i;
int a, b, c, sum;
char ch[];
scanf("%d%d%d", &n, &color, &m);
build(, , n);
while(m--)
{
scanf("%s",ch);
if(ch[] == 'C')
{
scanf("%d%d%d", &a, &b, &c);
if(a > b)
update(, b, a, c);
else
update(, a, b, c);
}
else
{
scanf("%d%d", &a, &b);
memset(mark, false, sizeof(mark));
if(a > b)
query(, b, a);
else
query(, a, b);
sum = ;
for(i = ; i <= color; i++)
{
if(mark[i])
{
sum += ;
//cout<<i<<endl;
}
}
printf("%d\n", sum);
}
}
return ;
}
poj 2777 Count Color(线段树 区间更新)的更多相关文章
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- POJ2777 Count Color 线段树区间更新
题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...
- ZOJ 1610 Count the Color(线段树区间更新)
描述Painting some colored segments on a line, some previously painted segments may be covered by some ...
- POJ P2777 Count Color——线段树状态压缩
Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...
- POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53312 Accepted: 16050 Des ...
随机推荐
- Nginx + Tomcat 动静分离实现负载均衡(转)
0.前期准备 使用Debian环境.安装Nginx(默认安装),一个web项目,安装tomcat(默认安装)等. 1.一份Nginx.conf配置文件 # 定义Nginx运行的用户 和 用户组 如果对 ...
- 数据结构(复习)---------字符串-----KMP算法(转载)
字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...
- DispatcherServlet--Spring的前置控制器作用简介
参考网址:http://jinnianshilongnian.iteye.com/blog/1602617 Struts有一个ActionServlet,用来完成前置控制器(分发器)的功能.其实,所有 ...
- [转载]Windows 7 IIS (HTTP Error 500.21 - Internal Server Error)解决
今天在测试网站的时候,在浏览器中输入http://localhost/时,发生如下错误: HTTP Error 500.21 - Internal Server Error Handler " ...
- 【IOC--Common Service Locator】不依赖于某个具体的IoC
你在你的应用程序应用IoC容器了吗,你是否希望不依赖于某个具体的IoC,微软的模式与实践团队在Codeplex上发布的Common Service Locator.Common Service Loc ...
- object-c 入门基础篇
原地址:http://www.cnblogs.com/moonvan/archive/2011/10/13/2210498.html 一.Objective-C与C的渊源 Objective-C诞生于 ...
- 判断不同IOS设备
var iOSGen = iPhone.generation; if (Debug.isDebugBuild) { Debug.Log("iPhone.generation : " ...
- 2016,除了 DevOps,企业还应该知道 CMDB!
CMDB 是 Configuration Management Database(配置管理数据库)的简称,CMDB 存储与管理企业 IT 架构中设备的各种配置信息,它与所有服务支持和服务交付流程都紧密 ...
- [翻译] - <Entity Framework> - 直接执行数据库命令
原文:[翻译] - <Entity Framework> - 直接执行数据库命令 纯属学习上的记录, 非专业翻译, 如有错误欢迎指正! 原文地址: http://msdn.microsof ...
- jQuery插件开发(转)
jQuery插件开发 - 其实很简单 [前言]jQuery已经被广泛使用,凭借其简洁的API,对DOM强大的操控性,易扩展性越来越受到web开发人员的喜爱,我在社区也发布了很多的jQuery插件,经常 ...