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 ...
随机推荐
- angular评论星级指令
地址: https://github.com/happen-zh/myStar 支持最大数,是否必填,回调,是否只读
- 《Java疯狂讲义》(第3版)学习笔记 2 - Java语言的运行机制
内容 1.高级语言的运行机制 2.Java 语言的运行机制 1.高级语言的运行机制 高级语言主要分为编译型语言和解释型语言两类. 编译型语言是指使用专门的编译器.针对特定平台(操作系统)将高级语言源代 ...
- IBatis一对多嵌套查询
1)类 public class AppData { // public int ModuleId { get; set; } public int DataId { get; set; } publ ...
- 初识Hadoop二,文件操作
1.使用hadoop命令查看hdfs下文件 [root@localhost hadoop-2.7.3]# hadoop fs -ls hdfs://192.168.36.134:9000/ 开始在se ...
- easyUI layout 中使用tabs+iframe解决请求两次方法
demo中的事例在加载tab页面时是 function createFrame(url) { var s = '<iframe name="iframepanel" scro ...
- JavaWeb学习笔记——JDOM
JavaDOC的网址:http://www.jdom.org/docs/apidocs/index.html import java.io.FileOutputStream; import org.j ...
- JSON-LD
RDF RDF用于信息需要被应用程序处理而不是仅仅显示给人观看的场合.RDF提供了一种用于表达这一信息.并使其能在应用程序间交换而不丧失语义的通用框架.既然是通用框架,应用程序设计者可以利用现成的通用 ...
- (转)Rest介绍
参考文献:Rest简介 REST是一种组织Web服务的架构,其只在架构方面提出了一系列约束. 关于Restful的无状态 所以在stackoverflow中,我们常常会看到有人问:我现在使用了这样一种 ...
- Struct Member Default Value
在C#中struct是值类型的数据类型,其默认值相当于调用了无参数默认构造函数之后的值.这也许就是struct不允许重载无参数默认构造函数的原因. https://msdn.microsoft.com ...
- ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
http://www.cnblogs.com/John-Connor/archive/2012/05/03/2478821.html 引言-- 在初级篇中,我们介绍了如何利用基于ASP.NET MVC ...