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 ...
随机推荐
- 特征值分解,奇异值分解(SVD)
特征值分解和奇异值分解在机器学习领域都是属于满地可见的方法.两者有着很紧密的关系,我在接下来会谈到,特征值分解和奇异值分解的目的都是一样,就是提取出一个矩阵最重要的特征. 1. 特征值: 如果说一个向 ...
- windows android studio 编译Jni动态库
项目需要,折腾了半天搞定windows android studio环境编译Jni动态库,现记录下来. 准备安装环境: 1. android studio 下载地址是http://www.androi ...
- javascript_22_for_js控制div每五个换一行
2. 3. css: <style type="text/css"> div{height: 50px; width: 50px; background: #f1161 ...
- python 日期转星期
import time import datetime today = int(time.strftime('%w')) print today anyday = datetime.datetime( ...
- 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
// test20.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include< ...
- windows下几种I/O端口(了解)
如果你想在Windows平台上构建服务器应用,那么I/O模型是你必须考虑的.Windows操作系统提供了选择(Select).异步选择(WSAAsyncSelect).事件选择(WSAEventSel ...
- 使用文本文件(.txt)进行数据存取的技巧总结(相当的经典)
使用文本文件(.txt)进行数据存取的技巧总结(相当的经典) 使用文本文件(.txt)进行数据存取的技巧总结 由于本帖内容较多,部分转自他人的心得,因此,凡转贴的地方仅用“----转----”标注,原 ...
- hdu 4465 Candy 数学
思路:易知结果为 ∑(n-k)*C(n+k,k)*(p^(n+1)*q^k+q^(n+1)*p^k). 注意不能直接算,注意点技巧!!!看代码 代码如下: #include<iostream&g ...
- FileOutputStream和FileInputStream
package one.string; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFound ...
- 深入浅出Java并发包—锁机制(三)
接上文<深入浅出Java并发包—锁机制(二)> 由锁衍生的下一个对象是条件变量,这个对象的存在很大程度上是为了解决Object.wait/notify/notifyAll难以使用的问题. ...