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 ...
随机推荐
- POJ 2942Knights of the Round Table(二分图判定+双连通分量)
题目链接 题意:一些骑士,他们有些人之间有矛盾,现在要求选出一些骑士围成一圈,圈要满足如下条件:1.人数大于1.2.总人数为奇数.3.有仇恨的骑士不能挨着坐.问有几个骑士不能和任何人形成任何的圆圈. ...
- django rest framework
Django-Rest-Framework 教程: 4. 验证和权限 作者: Desmond Chen, 发布日期: 2014-06-01, 修改日期: 2014-06-02 到目前为止, 我们的AP ...
- js022-高级技巧
js022-高级技巧 本章内容: 使用高级函数 防篡改对象 Yielding Timers 22.1 高级函数 1.安全的类型检测 2.作用域安全的构造函数 构造函数实际上是一个使用new操作符调用的 ...
- vm centos 添加网卡 无配置文件
vm centos 添加网卡 无配置文件 解决办法 [root@test ~]# ifconfig eth0 Link encap:Ethernet HWaddr 00:0C:29:C8:41:FB ...
- centos nc命令安装
yum install nc.x86_64 nc命令的参数 参数 作用-i 设置数据报传送时间间隔-l 以服务器方式运行-k 重复接收并处理某个端口上的所有连接,必须与-l选项一起使用-n 使用ip地 ...
- CentOS7安装Nginx并部署
服务器IP是192.168.36.136 1.直接yum install nginx即可 2.主配置文件是/etc/nginx/下的nginx.conf,另外一个是/etc/nginx/conf.d/ ...
- JavaWeb学习笔记——Tomcat相关
Tomcat目录分析 1.bin 存放启动和关闭Tomcat的脚本文件 2.conf 存放Tomcat服务器的各种配置文件 3.lib 存放Tomcat服务器的支持jar包 4.logs 存放T ...
- Java学习笔记2
package welcome; public class Constants { public static void main(String[] args){ final double CM_PE ...
- yum提示字符编码错误
1.问题描述: [root@localhost data]# yum Loaded plugins: product-id, refresh-packagekit, security, subscri ...
- 快速的mysql导入导出数据(load data和outfile)
1.load data: ***实际应用:把日志生成的xls文件load到MySQL中: mysql_cmd = "iconv -c -f utf-8 -t gbk ./data/al_ve ...