hdu - 5023 - A Corrupt Mayor's Performance Art(线段树)
题目原文废话太多太多太多,我就不copyandpaste到这里啦。。发个链接吧
题目
题目意思就是:P l r c 将区间 [l ,r]上的颜色变成c Q l r 就是打印出区间[l,r]中所有的颜色,并且要升序排列出来,注意最坑的就是各个区间的颜色初始化为2,这个条件竟然夹杂在那又长又臭的题目的某个角落里面!!
比赛的时候思路是有的,并且也能想到用set来撸,哎,对set的用法太挫逼了,写线段树又写得太挫逼了,后来补回这道题的时候,才发现其实是一道非常常规的线段树,所以最近给自己留了20道线段树慢慢刷,主要是能够更加熟练地写出线段树的模板,因为我发觉之前只是看得懂别人写的线段树的代码,却很少完全靠自己去敲出来,今天在补这道题的时候依然wa了很多次,最终才发现在query的那里忘记PushDown了 QAQ,废话少说 直接贴代码:
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <cstring>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn = ;
set<int> ans;
int SIZE;
int sum[maxn<<];
void PushDown(int rt){
if(sum[rt]){
sum[rt<<] = sum[rt];
sum[rt<<|] = sum[rt];
sum[rt] = ;
}
}
void build(int l,int r,int rt){
if(l == r){
sum[rt] = ;return;
}
int m = (l + r) >>;
build(lson);
build(rson);
return ;
}
void update(int L,int R,int c,int l,int r,int rt){
if(L <= l&&r <= R){
sum[rt] = c;
return ;
}
int m = (l + r)>>;
PushDown(rt);
if(L <= m) update(L,R,c,lson); if(m < R) update(L,R,c,rson);
return ;
}
void query(int L,int R,int l,int r,int rt){
//if(rt > SIZE) return;
if(L <= l&&r <= R&&sum[rt]){
ans.insert(sum[rt]);
return ;
}
PushDown(rt);
int m = (l + r)>>;
if(L <= m) query( L, R,lson);
if(m < R) query( L, R,rson);
return;
}
void print(){
set<int>::iterator it;
it = ans.begin();
cout<<*it;
ans.erase(it);
for(it = ans.begin();it != ans.end();++it)
printf(" %d",*it);
puts("");
}
void print_debug(){
cout<<"sum: "<<endl;
for(int i = ;i <= ;i++)
cout<<sum[i]<<" ";
puts("");
}
int main(){
int N,Q,a,b,c;
char ope[];
while(~scanf("%d%d",&N,&Q)&&(N+Q)){
SIZE = (N+)*N/;
memset(sum,,sizeof(sum));
build(,N,);
while(Q--){
scanf("%s",ope);
if(ope[] == 'Q'){
scanf("%d%d",&a,&b);
ans.clear();
query(a,b,,N,);
print();
}else{
scanf("%d%d%d",&a,&b,&c);
update(a,b,c,,N,);
//print_debug();
}
}
}
return ;
}
然后我又看到了另一份比较好玩的代码,是通过巧妙的位移运算来表示的,恩 感觉挺好的 链接请点击~
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define LL int const int maxn = ;
LL add[maxn<<];
LL sum[maxn<<];
void PushUp(int rt)
{
//把当前结点的信息更新到父结点
sum[rt] = sum[rt<<] | sum[rt<<|];//总共的颜色
}
void PushDown(int rt,int m)
{
if(add[rt])
{
add[rt<<] = add[rt];
add[rt<<|] = add[rt];
sum[rt<<] = add[rt] ;
sum[rt<<|] = add[rt] ;
add[rt] = ;//将标记向儿子节点移动后,父节点的延迟标记去掉
//传递后,当前节点标记域清空
}
}
void build(int l,int r,int rt)
{
add[rt] = ;//初始化为所有结点未被标记
if (l == r)
{
sum[rt]=;//初始颜色为2
return ;
}
int mid = (l + r) >> ;
build(lson);
build(rson);
PushUp(rt);
}
void update(int L,int R,int c,int l,int r,int rt)
{
if (L <= l && r <= R)
{
add[rt] =<<(c-);//位运算左移表示有某种颜色
sum[rt] =<<(c-);
return ;
}
PushDown(rt , r - l + );//----延迟标志域向下传递
int mid = (l + r) >> ;
if (L <= mid)
update(L , R , c , lson);//更新左儿子
if (mid < R)
update(L , R , c , rson);//更新右儿子
PushUp(rt);
}
LL query(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
{
return sum[rt];
}
//要取rt子节点的值时,也要先把rt的延迟标记向下移动
PushDown(rt , r - l + );
int mid = (l + r) >> ;
LL ret = ;
if (L <= mid)
ret |= query(L , R , lson);
if (mid < R)
ret |= query(L , R , rson);
return ret;
}
int main()
{
int N , Q;
int a , b , c;
while(scanf("%d%d",&N,&Q))
{
if(N== && Q==)
break;
build( , N , );//建树
while(Q--)//Q为询问次数
{
char op[];
scanf("%s",op);
if(op[] == 'Q')//Q为询问次数
{
scanf("%d%d",&a,&b);
LL tt=query(a , b , , N , );
LL flag = ;
for(int i=; i<=; i++)
{
if(tt>>(i-)& && flag==)
{
printf("%d",i);
flag = ;
}
else if(tt>>(i-)&)
printf(" %d",i);
}
printf("\n");
}
else
{
scanf("%d%d%d",&a,&b,&c);
update(a , b , c , , N , );
}
}
}
return ;
}
hdu - 5023 - A Corrupt Mayor's Performance Art(线段树)的更多相关文章
- 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 线段树区间更新+状态压缩
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 ...
- 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(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- HDU5023:A Corrupt Mayor's Performance Art(线段树区域更新+二进制)
http://acm.hdu.edu.cn/showproblem.php?pid=5023 Problem Description Corrupt governors always find way ...
- 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 ...
- A Corrupt Mayor's Performance Art(线段树区间更新+位运算,颜色段种类)
A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100 ...
随机推荐
- Python学习-列表的其它主要操作
列表的其它主要操作 还记得之前使用del语句去清除一个列表中的所有内容,那么会因为把列表全部清空,所以输出会报错.可以使用clear() 完成 clear(self):可以将一个列表变成空列表 lis ...
- Ubuntu环境修改IP地址方法
ubuntu环境修改IP地址方法和CentOS系统修改方法不太一样.ubuntu系统修改IP地址方法如下: 编辑/etc/network/interfaces,增加以下内容: auto eth0 if ...
- xshell连接不了虚拟机处理方法(错误提示:Connection closing...Socket close.Connection closed by foreign host.Disconnected from remote host(localhost) at 08:47:23.)
一.问题描述:xshell连接不了虚拟机,出现错误提示:Connection closing...Socket close.Connection closed by foreign host.Disc ...
- Python基础—线程、进程和协程
今天已是学习Python的第十一天,来干一碗鸡汤继续今天的内容,今天的鸡汤是:超越别人对你的期望.本篇博客主要介绍以下几点内容: 线程的基本使用: 线程的锁机制: 生产者消费之模型(队列): 如何自定 ...
- 7-9 旅游规划(25 分)(Dijkstra最短路径算法)
有了一张自驾旅游路线图,你会知道城市间的高速公路长度.以及该公路要收取的过路费.现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径.如果有若干条路径都是最短的,那么需要输出最便 ...
- 【BZOJ4583】购物(组合计数)
题意:商店出售3种颜色的球,分别为红.绿.蓝. 城市里有n个商店,第i个商店在第First_i天开始营业,连续营业Red_i+Green_i+Blue_i天,每个商店每天只能出售一种颜色的球. 每天最 ...
- 主席树初探--BZOJ1901: Zju2112 Dynamic Rankings
n<=10000的序列做m<=10000个操作:单点修改,查区间第k小. 所谓的主席树也就是一个值域线段树嘛..不过在这里还是%%fotile 需要做一个区间查询,由于查第k小,需要一些能 ...
- SOJ 4454 (矩阵快速幂)
先引入数的快速幂 例如计算2的5次方,常规算法2*2*2*2*2,利用快速幂的思想,求出5的二进制表达式101,权值为1和4的位上数字为1,即2^5=2^1*2^4.代码如下,时间复杂度为O(logn ...
- Mycat环境搭建教程收集(待实践)
先收集,后续再实践. http://blog.csdn.net/dreamcode/article/details/44307377 http://blog.csdn.net/lanonola/art ...
- linux network name space
linux network namespace概念类似于网络中的 VRF (virtual routing and forwarding).但是,你不知道VRF的概念也没关系,下面我们通过一个简单的介 ...