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 ...
随机推荐
- 服务器端spice配置详解
1. 安装必要的工具 sudo apt-get install build-essential autoconf git-core intltool 2. 安装必要的依赖包 -dev libxfixe ...
- OC面向对象封装
面向对象语言的三大特性:封装.继承.多态 封装:不暴露自己类的内部的属性,提高自己的数据的安全性:就像一个接线盒一样,内部结构看不到,只有外部的接口提供给我们使用,这样既安全又美观:在代码方面就是结构 ...
- android support Percent支持库开发
Android的布局支持百分比的设置进行开发,来学习如何去实现它,不过看起来会像网页的设置,比如宽度的设置属性是`layout_widthPercent`.在此之前,我们一般都会设置Linearlay ...
- 1561:The more, The Better - hdu
Problem DescriptionACboy很喜欢玩一种战略游戏,在一个地图上,有N座城堡,每座城堡都有一定的宝物,在每次游戏中ACboy允许攻克M个城堡并获得里面的宝物.但由于地理位置原因,有些 ...
- Codeforces Beta Round #10 D. LCIS
题目链接: http://www.codeforces.com/contest/10/problem/D D. LCIS time limit per test:1 secondmemory limi ...
- 2014 Multi-University Training Contest 2
官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uyet.html ZCC Loves Intersection ZCC Loves COT 首先考 ...
- Oracle session连接数和inactive的问题记录
Oracle session连接数和inactive的问题记录 http://timnity.javaeye.com/blog/280383 从上周起,服务器Oracle数据库出现问题,用不到半天,就 ...
- What are Scopes?
scope is an object that refers to the application model. It is an execution context for expressions. ...
- HDU 1753 大明A+B(字符串模拟,简单题)
简单题,但要考虑一些细节: 前导0不要,后导0不要,小数长度不一样时,有进位时,逆置处理输出 然后处理起来就比较麻烦了. 题目链接 我的代码纯模拟,把小数点前后分开来处理,写的很繁杂,纯当纪念——可怜 ...
- orale内置函数COALESCE和GREATEST和LEAST
1. COALESCE 返回该表达式列表的第一个非空value. 格式: COALESCE(value1, value2, value3, ...) 含义: 返回value列表第一个非空的值. val ...