HDU5023:A Corrupt Mayor's Performance Art(线段树区域更新+二进制)
http://acm.hdu.edu.cn/showproblem.php?pid=5023
Because a lot of people praised mayor X's painting(of course, X was a mayor), mayor X believed more and more that he was a very talented painter. Soon mayor X was not satisfied with only making money. He wanted to be a famous painter. So he joined the local painting associates. Other painters had to elect him as the chairman of the associates. Then his painting sold at better price.
The local middle school from which mayor X graduated, wanted to beat mayor X's horse fart(In Chinese English, beating one's horse fart means flattering one hard). They built a wall, and invited mayor X to paint on it. Mayor X was very happy. But he really had no idea about what to paint because he could only paint very abstract paintings which nobody really understand. Mayor X's secretary suggested that he could make this thing not only a painting, but also a performance art work.
This was the secretary's idea:
The wall was divided into N segments and the width of each segment was one cun(cun is a Chinese length unit). All segments were numbered from 1 to N, from left to right. There were 30 kinds of colors mayor X could use to paint the wall. They named those colors as color 1, color 2 .... color 30. The wall's original color was color 2. Every time mayor X would paint some consecutive segments with a certain kind of color, and he did this for many times. Trying to make his performance art fancy, mayor X declared that at any moment, if someone asked how many kind of colors were there on any consecutive segments, he could give the number immediately without counting.
But mayor X didn't know how to give the right answer. Your friend, Mr. W was an secret officer of anti-corruption bureau, he helped mayor X on this problem and gained his trust. Do you know how Mr. Q did this?
For each test case:
The first line contains two integers, N and M ,meaning that the wall is divided into N segments and there are M operations(0 < N <= 1,000,000; 0<M<=100,000)
Then M lines follow, each representing an operation. There are two kinds of operations, as described below:
1) P a b c
a, b and c are integers. This operation means that mayor X painted all segments from segment a to segment b with color c ( 0 < a<=b <= N, 0 < c <= 30).
2) Q a b
a and b are integers. This is a query operation. It means that someone asked that how many kinds of colors were there from segment a to segment b ( 0 < a<=b <= N).
Please note that the operations are given in time sequence.
The input ends with M = 0 and N = 0.
题意:一长由N小段组成的长条,每小段的初始颜色为2。现执行M个操作,每个操作是以下两种中的一种(0 < N <= 1,000,000; 0<M<=100,000) :
P a b c ——> 将段a到段b涂成颜色c,c是1, 2, ... 30中的一种(0 < a<=b <= N, 0 < c <= 30)。
Q a b ——> 问段a到段b之间有哪几种颜色,按颜色大小从小到大输出(0 < a<=b <= N, 0 < c <= 30)。
——>>很明显此题可以用线段树实现Mlog(N)的解法。。
题目解析:这题收获很大,对二进制有了一点理解,一个int型数据占4个字节,总共32位,例如0的二进制为 00000000 00000000 00000000 00000000 ,而这题只有30种颜色,
所以完全可以用二进制来保存。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#define N 1000010
using namespace std;
struct node
{
int l,r,lz,w;
} q[*N];
int n,m,res,tt;
void pushup(int rt)
{
q[rt].w=q[rt<<].w|q[rt<<|].w;
}
void build(int l,int r,int rt)
{
q[rt].l=l;
q[rt].r=r;
q[rt].lz=;
q[rt].w=;
if(l==r) return ;
int mid=(l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
pushup(rt);
return ;
}
void pushdown(int rt)
{
if(q[rt].lz)
{
q[rt<<].lz=q[rt].lz;
q[rt<<|].lz=q[rt].lz;
q[rt<<].w=q[rt].lz;
q[rt<<|].w=q[rt].lz;
q[rt].lz=;
}
}
void update(int lf,int rf,int l,int r,int rt,int key)
{
if(lf<=l&&rf>=r)
{
q[rt].w=key;
q[rt].lz=key;
return ;
}
pushdown(rt);
int mid=(l+r)>>;
if(lf<=mid) update(lf,rf,l,mid,rt<<,key);
if(rf>mid) update(lf,rf,mid+,r,rt<<|,key);
pushup(rt);
return ;
}
void query(int lf,int rf,int l,int r,int rt)
{
if(lf<=l&&rf>=r)
{
res=res|q[rt].w;
return ;
}
pushdown(rt);
int mid=(l+r)>>;
if(lf<=mid) query(lf,rf,l,mid,rt<<);
if(rf>mid) query(lf,rf,mid+,r,rt<<|);
return ;
}
int main()
{
char ch[];
int sum1,sum2,key;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
build(,n,);
for(int i=; i<m; i++)
{
scanf("%s",ch);
if(ch[]=='P')
{
scanf("%d%d%d",&sum1,&sum2,&key);
update(sum1,sum2,,n,,<<(key-));
}
else if(ch[]=='Q')
{
scanf("%d%d",&sum1,&sum2);
res=;
query(sum1,sum2,,n,);
int flag=;
for(int i=; i<; i++)
{
if(res&(<<i)&&flag==)
{
printf(" %d",i+);
}
else if(res&(<<i)&&flag==)
{
printf("%d",i+);
flag=;
}
}
printf("\n");
}
}
}
return ;
}
HDU5023: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 ...
- 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(线段树区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5023 解题报告:一面墙长度为n,有N个单元,每个单元编号从1到n,墙的初始的颜色是2,一共有30种颜色 ...
- hdu5023--A Corrupt Mayor's Performance Art
来源:2014 ACM/ICPC Asia Regional Guangzhou Online 题意:长度为n的一个线段,1-30为颜色代号.初始状态每个单位长度颜色都为2,然后有q次操作,P操作把区 ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- POJ-2528 Mayor's posters(线段树区间更新+离散化)
http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...
- ACM学习历程—HDU 5023 A Corrupt Mayor's Performance Art(广州赛区网赛)(线段树)
Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sel ...
随机推荐
- 如何使用matlab中的胞元数组
胞元数组(cell Arry)的基本组分是胞元(cell),每个胞元本身在数组中是平等的,只能以下标区分.胞元可以存放任何类型.任何大小的数组,如任意维数值数组.字符串数组.符号对象等,而且同一个胞元 ...
- sqlite数据库下载安装和初步操作和所遇到的问题near "sqlite3":syntax error
1.下载sqlite数据库:http://www.sqlite.org/download.html 假设是在window上安装须要在 Windows 区下载预编译的二进制文件.如图下载下载 sqlit ...
- swift--设置app图标和启动页面
1,如下图:
- hadoop程序MapReduce之average
需求:求多门课程的平均值. 样板:math.txt zhangsan 90 lisi 88 wanghua 80 china.txt zhangsan 80lisi 90wanghua 88 输出:z ...
- docker pull 详解
docker pull 用于从镜像仓库中拉取或更新指定镜像,用法如:docker pull centos ,默认是从 Docker Hub 中拉取镜像 在拉取镜像前,我们可以先配置 docker 加速 ...
- 什么叫做hack
由于不同的浏览器,比如Internet Explorer 6,Internet Explorer 7,Mozilla Firefox等,对CSS的解析认识不一样,因此会导致生成的页面效果不一样,得不到 ...
- 剑指offer练习
1.题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. public c ...
- ExtJS6的中sencha cmd中自动创建案例项目代码分析
在之前的博文中,我们按照sencha cmd的指点,在自己win7虚拟机上创建了一个案例项目,相当于创建了一个固定格式的文档目录结构,然后里面自动创建了一系列js代码.这是使用sencha cmd自动 ...
- Windows Phone 几种页面间传递数据的方式
首先,我们要引用:using Microsoft.Phone.Shell; 第一种: // 导航到新页面 NavigationService.Navigate(new Uri("/Detai ...
- MySQL 5.6 my.cnf 参数说明(转)
# 以下选项会被MySQL客户端应用读取. # 注意只有MySQL附带的客户端应用程序保证可以读取这段内容. # 如果你想你自己的MySQL应用程序获取这些值. # 需要在MySQL客户端库初始化的时 ...