题目链接: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. Yocto开发笔记之《Tip-bitbake常用命令》(QQ交流群:519230208)

    开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 =============================== ...

  2. Java——下拉列表框:JComboBox

    import java.awt.Container; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import j ...

  3. nuget包管理器控制台下的powershell脚本介绍

    http://personball.com/powershell/2016/07/15/powershell-tips 定制自己的powershell,减少重复工作 安装一系列自己的常用nuget包 ...

  4. python 占位符 %s Format

    1.百分号方式 %[(name)][flags][width].[precision]typecode (name)      可选,用于选择指定的key flags          可选,可供选择 ...

  5. 贴上自己写的代码-jq隐藏事件

  6. php中的正则函数主要有三个-正则匹配,正则替换

    php中变量的声明? 由于php声明变量的时候, 不支持使用 var关键字, 又不能直接写一个变量名字, 孤零零的放在那里, 所以, 在php中声明变量的方式, 同时也是给变量初始化的形式, 即: & ...

  7. 几个Web server的HA架构资料

    提高Web性能, 最关键还是要看瓶颈在哪里. 手段不外乎下面几个. 实现从易到难一般为: 优化Big SQL -> 引入CDN -> 引入Memcache等缓存 -> Web负载平衡 ...

  8. 2015年12月01日 GitHub入门学习(三)GitHub创建仓库

    序:创建自己的GITHub账号,并创建自己第一个仓库,尝试通过msysgit客户端,往仓库提交文件. 一.创建GitHub账户 链接地址:https://github.com/join,很简单,自己创 ...

  9. [译]Mongoose指南 - 中间件

    中间件是一些函数, 当document发生init, validate, save和remove方法的时候中间件发生. 中间件都是document级别的不是model级别的. 下面讲讲两种中间件pre ...

  10. 包介绍 - Fluent Validation (用于验证)

    Install-Package FluentValidation 如果你使用MVC5 可以使用下面的包 Install-Package FluentValidation.MVC5 例子: public ...