HDU 5023 A Corrupt Mayor's Performance Art(线段树区间更新)
题目链接: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(线段树区间更新)的更多相关文章
- HDU 5023 A Corrupt Mayor's Performance Art 线段树区间更新+状态压缩
Link: http://acm.hdu.edu.cn/showproblem.php?pid=5023 #include <cstdio> #include <cstring&g ...
- 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 ...
- 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 ...
- HDU5023:A Corrupt Mayor's Performance Art(线段树区域更新+二进制)
http://acm.hdu.edu.cn/showproblem.php?pid=5023 Problem Description Corrupt governors always find way ...
- ACM学习历程—HDU 5023 A Corrupt Mayor's Performance Art(广州赛区网赛)(线段树)
Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sel ...
- HDU 5023 A Corrupt Mayor's Performance Art (据说是线段树)
题意:给定一个1-n的墙,然后有两种操作,一种是P l ,r, a 把l-r的墙都染成a这种颜色,另一种是 Q l, r 表示,输出 l-r 区间内的颜色. 析:应该是一个线段树+状态压缩,但是我用s ...
- 2014 网选 广州赛区 hdu 5023 A Corrupt Mayor's Performance Art
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #d ...
- hdu - 5023 - A Corrupt Mayor's Performance Art(线段树)
题目原文废话太多太多太多,我就不copyandpaste到这里啦..发个链接吧题目 题目意思就是:P l r c 将区间 [l ,r]上的颜色变成c Q l r 就是打印出区间[l,r ...
- POJ 2528 Mayor's posters(线段树/区间更新 离散化)
题目链接: 传送门 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of By ...
随机推荐
- Blast本地化
转载]Blast本地化:使用Blastall进行数据库比对 (2012-02-13 21:25:31) 用blastall进行序列比对 blastall是最常用的blast程序之一,其功能非常强大 ...
- Yocto开发笔记之《驱动调试-华为3G模块》(QQ交流群:519230208)
QQ群:519230208,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================================== 参考:ht ...
- 【转】jquery的extend和fn.extend
jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend(object); 为扩展j ...
- SQL Server 2012 安装
SQL Server 2012 安装过程很漫长, 里面很多界面不一一截取了,我分别在win7 企业版 64位 和 win10专业版SP1 64位 装了SQL Server 2012 ,都没有问题. 1 ...
- Latent Semantic Analysis (LSA) Tutorial 潜语义分析LSA介绍 一
Latent Semantic Analysis (LSA) Tutorial 译:http://www.puffinwarellc.com/index.php/news-and-articles/a ...
- PNG类库
libpng depends on Zlib http://www.libpng.org/pub/png/libpng.html LodePNG http://lodev.org/lodepng/ P ...
- UITextView限制输入文字
一.设置UITextView的delegate为控制器 二.实现代理方法 #pragma mark - UITextViewDelegate - (BOOL)textView:(UITextView ...
- python 二分法查找实例(递归、循环)
二分法,主要应用于有序序列中,原理是每次查找都将原序列折半,逐渐缩小查找范围的一种算法. 需求 要求在一个有序序列中,例如[0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30 ...
- Apache配置--用户认证(针对目录访问)-update2015-05-02
通过Apache配置可以限制用户对目录的访问,会弹出像phpadmin一样的登陆框. ========================================================= ...
- 使用docker exec 就可以进入container,例如:docker exec -it <container_id> /bin/bash
使用docker exec 就可以进入container,例如:docker exec -it <container_id> /bin/bash