题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023

解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色,有两种操作:

P a b c  把区间a到b涂成c颜色

Q a b 查询区间a到b的颜色

线段树区间更新,每个节点保存的信息有,存储颜色的c,30种颜色可以压缩到一个int型里面存储,然后还有一个tot,表示这个区间一共有多少种颜色。

对于P操作,依次往下寻找,找要更新的区间,找到要更新的区间之前,如果当前所在的区间的总共的颜色只有一种,那么就要把这个区间的信息压到这个节点的两个子节点中,

然后再选择要更新的那个区间所在的那个区间继续往下更新。

对于Q操作,这个可以随便了,反正区间的信息都存在了每个节点的C 里面,只要按规则取出来就行了。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = ;
struct node
{
int l,r,c,tot;
void Node(int l1,int r1,int c1,int tot1)
{
l = l1,r = r1,c = c1,tot = tot1;
}
}tree[*maxn];
int ans; void Init(int p)
{
if(tree[p].l == tree[p].r) return ;
int mid = (tree[p].l + tree[p].r) / ;
tree[*p].Node(tree[p].l,mid,,);
tree[*p+].Node(mid+,tree[p].r,,);
Init(*p);
Init(*p+);
}
void paint(int p,int l,int r,int c)
{
if(tree[p].l == l && tree[p].r == r)
{
tree[p].c = << (c-);
tree[p].tot = ;
return ;
}
int mid = (tree[p].l + tree[p].r) / ;
int T;
if(tree[p].tot == ) //如果这个节点只有一种颜色,需要先往下推
{
T = ;
for(int i = ;i < ;++i)
if(tree[p].c & ( << i))
{
T = i+;
break;
}
paint(*p,tree[p].l,mid,T);
paint(*p+,mid+,tree[p].r,T);
}
if(r <= mid)
{
paint(*p,l,r,c);
}
if(l <= mid && r > mid)
{
paint(*p,l,mid,c);
paint(*p+,mid+,r,c);
}
else if(l > mid)
{
paint(*p+,l,r,c);
}
tree[p].c = tree[*p].c | tree[*p+].c; //回溯
int tt = ;
for(int i = ;i < ;++i)
if(tree[p].c & ( << i))
tt++;
tree[p].tot = tt;
}
void query(int p,int l,int r)
{
if((tree[p].l == l && tree[p].r == r) || tree[p].tot == )
{
ans |= tree[p].c;
return ;
}
int mid = (tree[p].l + tree[p].r) / ;
if(r <= mid) query(*p,l,r);
else if(l <= mid && r > mid)
{
query(*p,l,mid);
query(*p+,mid+,r);
}
else if(l > mid) query(*p+,l,r);
}
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int n,m;
while(scanf("%d%d",&n,&m),n+m)
{
tree[].Node(,n,,);
Init();
int a,b,c;
char oper[];
while(m--)
{
scanf("%s%d%d",oper,&a,&b);
if(oper[] == 'P')
{
scanf("%d",&c);
paint(,a,b,c);
}
else
{
ans = ;
query(,a,b);
int flag = ;
for(int i = ;i < ;++i)
if(ans & ( << i))
{
printf(flag? "%d":" %d",i+);
flag = ;
}
puts("");
}
// for(int i = 1;i <= 9;++i)
// printf(i == 9? "%d\n":"%d ",tree[i].c);
// for(int i = 1;i <= 9;++i)
// printf(i == 9? "%d\n":"%d ",tree[i].tot);
}
}
}

HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)的更多相关文章

  1. HDU 5023 A Corrupt Mayor's Performance Art 线段树区间更新+状态压缩

    Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5023 #include <cstdio> #include <cstring&g ...

  2. hdu 5023 A Corrupt Mayor's Performance Art 线段树

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

  3. hdu----(5023)A Corrupt Mayor's Performance Art(线段树区间更新以及区间查询)

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

  4. HDU5023:A Corrupt Mayor's Performance Art(线段树区域更新+二进制)

    http://acm.hdu.edu.cn/showproblem.php?pid=5023 Problem Description Corrupt governors always find way ...

  5. ACM学习历程—HDU 5023 A Corrupt Mayor's Performance Art(广州赛区网赛)(线段树)

    Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sel ...

  6. HDU 5023 A Corrupt Mayor's Performance Art (据说是线段树)

    题意:给定一个1-n的墙,然后有两种操作,一种是P l ,r, a 把l-r的墙都染成a这种颜色,另一种是 Q l, r 表示,输出 l-r 区间内的颜色. 析:应该是一个线段树+状态压缩,但是我用s ...

  7. 2014 网选 广州赛区 hdu 5023 A Corrupt Mayor's Performance Art

    #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #d ...

  8. hdu - 5023 - A Corrupt Mayor's Performance Art(线段树)

    题目原文废话太多太多太多,我就不copyandpaste到这里啦..发个链接吧题目 题目意思就是:P  l  r  c  将区间 [l ,r]上的颜色变成c    Q  l r 就是打印出区间[l,r ...

  9. POJ 2528 Mayor's posters(线段树/区间更新 离散化)

    题目链接: 传送门 Mayor's posters Time Limit: 1000MS     Memory Limit: 65536K Description The citizens of By ...

随机推荐

  1. POJ3281 Dining(拆点构图 + 最大流)

    题目链接 题意:有F种食物,D种饮料N头奶牛,只能吃某种食物和饮料(而且只能吃特定的一份) 一种食物被一头牛吃了之后,其余牛就不能吃了第一行有N,F,D三个整数接着2-N+1行代表第i头牛,前面两个整 ...

  2. MySQL下全文索引

    一种特殊的索引,它会把某个数据表的某个数据列出现过的所有单词生成一份清单. alter table tablename add fulltext(column1,column2) 只能在MyISAM数 ...

  3. BufferedReader类

    BufferedReader类用于从缓冲区中读取内容,多有的输入字节数据都将放在缓冲区中. BufferedReader中定义的构造方法只能接收字符输入流的实例,所以必须使用字符输入流和字节输入流的转 ...

  4. Java关键字——instanceof

    Java中可以使用instanceof关键字判断一个对象到底是哪一个类的实例 格式:对象 instance 类 返回 boolean类型 通过子类实例化的对象同时是子类和父类的实例,无论是直接声明子类 ...

  5. 《深入理解bootstrap》读书笔记:第4章 CSS组件(下)

    十. 标签(.label类,label-xxx) 高亮一些标题部分. 1 2 3 4 5 6 <h1>HELLO<span class="label label-defau ...

  6. js内存泄漏

    IE和webkit浏览器都是采用计数来处理垃圾,也就是说每个对象被引用一次,该对象的计数器成员+1,如果计数器为0,那么这个对象被销毁 例如: function A() { var obj = {}; ...

  7. Nvidia Nsight + .NET

    https://devtalk.nvidia.com/default/topic/804306/nsight-4-5-can-t-debug-net-applications/ http://comm ...

  8. ecshop 的transport.js 与jqueyr冲突

    1111 {insert_scripts files='common.js,global.js,transport.js'} <script type="text/javascript ...

  9. Multiple sequence alignment Benchmark Data set

    Multiple sequence alignment Benchmark Data set 1. 汇总: 序列比对标准数据集: http://www.drive5.com/bench/ This i ...

  10. jQuery 鼠标拖拽移动窗口

    拖拽移动需要注意的是:拖拽移动的窗口是如何定位的,如果"left"属性为"%" ,以"margin-left"来计算定位,如下实例,如果&q ...