题目: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(线段树 区间更新)的更多相关文章

  1. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  2. poj 2777 Count Color(线段树区区+染色问题)

    题目链接:  poj 2777 Count Color 题目大意:  给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C  a  b  c 把区间[a,b]涂为c色,P  a  b 查 ...

  3. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  4. POJ 2777 Count Color(线段树之成段更新)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...

  5. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  6. poj 2777 Count Color(线段树、状态压缩、位运算)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Des ...

  7. POJ2777 Count Color 线段树区间更新

    题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...

  8. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

  9. POJ P2777 Count Color——线段树状态压缩

    Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...

  10. POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53312   Accepted: 16050 Des ...

随机推荐

  1. sharepoint 2010

    Technical diagrams (SharePoint Server 2010) http://technet.microsoft.com/en-us/library/cc263199(offi ...

  2. C#之多态

    多态是面向对象编程中三大机制之一,其原理建立在"从父类继承而来的子类可以转换为其父类"这个规则之上,换句话说,能用父类的地方,就能用该类的子类.当从父类派生了很多子类时,由于每个子 ...

  3. 公众号开发学习Day01

    登录https://mp.weixin.qq.com/后点击注册进入注册微信公众号界面,使用邮箱进行注册 注意点1:一个邮箱只能创建一个公众号,并且公众号的三种类型(订阅号,服务号,企业号)只能选一种 ...

  4. 1452: [JSOI2009]Count - BZOJ

    Description Input Output Sample Input Sample Output 1 2HINT 一开始还想什么离线做,其实不用,空间足够,我们直接开100个二维树状数组,然后就 ...

  5. VIM配置自动提示功能

        问题描述:                  使用VIM作为Linux下的IDE,但是VIM默认情况下不支持自动代码提示功能,因此希望安装插件实现自动提示功能,目前找到的自动提示工具,非常好用 ...

  6. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  7. window.open被IE拦截的解决办法

    由于在使用window.open时,在很多情况下,弹出的窗口会被浏览器阻止,但若是使用a链接target='_blank',则不会,基于这一特点,自己封装了一个open方法: function ope ...

  8. Maven开源中国镜像

      mirrors> <mirror> <id>nexus-osc</id> <mirrorOf>central</mirrorOf> ...

  9. Grunt打包GMU组件 报错处理

    莫Q群的大神推荐移动GMU组件,GMU是基于zepto的mobile UI组件库,提供webapp.pad端简单易用的UI组件,官网:http://gmu.baidu.com/具有以下特点: 简单易用 ...

  10. hdu 4696 Answers

    思路:由于c[i]要么是1,要么是2.所以当c[i]中没有1的时候就不可能得到奇数: 再就是如果m<=0,也不可能得到. 代码如下: #include<cstdio> #includ ...