HDU 4391 Paint The Wall(分块的区间维护)
题意:给出几个操作,把l-r赋值为z,询问l-r有几个z,其中z < INT_MAX
思路:因为z很大,所以很难直接用线段树去维护。这里可以使用分块来解决。我们可以让每个块用map去储存map[i]的个数,用类似线段树的lazy标记来给整个块更新,当需要对块内某些数操作时再pushdown。
注意一下不要随意开辟map的空间,在计算区间的z的个数时应采用
if(b[i].num.find(z) != b[i].num.end()) ans += b[i].num[z];
减少空间开辟。
代码:
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = + ;
const int MOD = 1e9 + ;
const int INF = 0x3f3f3f3f;
struct Block{
map<int, int> num;
int lazy, L, R;
}b[];
int belong[maxn], a[maxn], block, sz;
int n, m;
void init(){
block = sqrt(n * 1.0);
for(int i = ; i < n; i++)
belong[i] = i / block + ;
sz = belong[n - ];
for(int i = ; i <= sz; i++){
b[i].lazy = -;
b[i].L = (i - ) * block;
b[i].R = min(b[i].L + block - , n - );
b[i].num.clear();
for(int j = b[i].L; j <= b[i].R; j++){
b[i].num[a[j]]++;
}
}
}
void push_down(int x){
b[x].num.clear();
for(int i = b[x].L; i <= b[x].R; i++)
a[i] = b[x].lazy;
b[x].num[b[x].lazy] = b[x].R - b[x].L + ;
b[x].lazy = -;
}
void update(int ll, int rr, int z){
int l = belong[ll], r = belong[rr];
int L, R;
if(l == r){
if(b[l].lazy != -) push_down(l);
for(int i = ll; i <= rr; i++){
b[l].num[a[i]]--;
b[l].num[z]++;
a[i] = z;
}
}
else{
L = ll, R = b[l].R;
if(b[l].lazy != -) push_down(l);
for(int i = L; i <= R; i++){
b[l].num[a[i]]--;
b[l].num[z]++;
a[i] = z;
} L = l + , R = r - ;
for(int i = L; i <= R; i++)
b[i].lazy = z; L = b[r].L, R = rr;
if(b[r].lazy != -) push_down(r);
for(int i = L; i <= R; i++){
b[r].num[a[i]]--;
b[r].num[z]++;
a[i] = z;
}
}
}
int query(int ll, int rr, int z){
int l = belong[ll], r = belong[rr];
int L, R, ans = ; if(l == r){
if(b[l].lazy != -) push_down(l);
for(int i = ll; i <= rr; i++){
if(a[i] == z) ans++;
}
return ans;
}
else{
L = ll, R = b[l].R;
if(b[l].lazy != -) push_down(l);
for(int i = L; i <= R; i++){
if(a[i] == z) ans++;
} L = l + , R = r - ;
for(int i = L; i <= R; i++){
if(b[i].lazy != -){
if(b[i].lazy == z) ans += b[i].R - b[i].L + ;
}
else{
if(b[i].num.find(z) != b[i].num.end()) ans += b[i].num[z]; //不开辟新空间
}
} L = b[r].L, R = rr;
if(b[r].lazy != -) push_down(r);
for(int i = L; i <= R; i++){
if(a[i] == z) ans++;
}
return ans;
}
}
int main(){
while(~scanf("%d%d", &n, &m)){
for(int i = ; i < n; i++){
scanf("%d", &a[i]);
}
init();
while(m--){
int o, l, r, z;
scanf("%d%d%d%d", &o, &l, &r, &z);
if(o == ){
update(l, r, z);
}
else{
printf("%d\n", query(l, r, z));
}
}
}
return ;
}
HDU 4391 Paint The Wall(分块的区间维护)的更多相关文章
- HDU 4391 - Paint The Wall - 分块哈希入门
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=4391 题意 : 给一段区间, 有两种操作 1 : 给 x 到 y 的区间染色为 z 2 : 查询 ...
- HDU 4391 Paint The Wall(分块+延迟标记)
Paint The Wall Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4391 Paint The Wall 段树(水
意甲冠军: 特定n多头排列.m操作 以下是各点的颜色 以下m一种操纵: 1 l r col 染色 2 l r col 问间隔col色点 == 通的操作+区间内最大最小颜色数的优化,感觉非常不科学... ...
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- hdu 1543 Paint the Wall
http://acm.hdu.edu.cn/showproblem.php?pid=1543 #include <cstdio> #include <cstring> #inc ...
- HDU 6315 Naive Operations(线段树+区间维护)多校题解
题意:a数组初始全为0,b数组题目给你,有两种操作: 思路:dls的思路很妙啊,我们可以将a初始化为b,加一操作改为减一,然后我们维护一个最小值,一旦最小值为0,说明至少有一个ai > bi,那 ...
- 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543
学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...
- HDU 4012 Paint on a Wall(状压+bfs)
Paint on a Wall Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- hdu 3669 Cross the Wall(斜率优化DP)
题目连接:hdu 3669 Cross the Wall 题意: 现在有一面无限大的墙,现在有n个人,每个人都能看成一个矩形,宽是w,高是h,现在这n个人要通过这面墙,现在只能让你挖k个洞,每个洞不能 ...
随机推荐
- Lua之math
Lua之math函数: 转载请注明出处:http://www.cnblogs.com/jietian331/p/8032555.html abs 取绝对值 math.abs(-15) 15 acos ...
- Vue系列之 => 使用钩子函数的第二个参数传参
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- multiprocessing 源码解析 更新中......
一.参考链接 1.源码包下载·链接: https://pypi.org/search/?q=multiprocessing+ 2.源码包 链接:https://pan.baidu.com/s/1j ...
- 20155228 基于VirtualBox安装Ubuntu和学习linux命令的学习经历和心得
一.虚拟机VirtualBox的下载安装 基于VirtualBox虚拟机安装Ubuntu图文教程 虽然娄老师的教程对于VirtualBox的下载安装讲的很简单,可以说是一笔带过,但是我在下载安装的过程 ...
- Hive 数仓中常见的日期转换操作
(1)Hive 数仓中一些常用的dt与日期的转换操作 下面总结了自己工作中经常用到的一些日期转换,这类日期转换经常用于报表的时间粒度和统计周期的控制中 日期变换: (1)dt转日期 to_date(f ...
- Hive 修改表结构常用操作
添加列 add columns alter table table_name add columns (id int comment '主键ID' ) ; 默认在表所有字段之后,分区字段之前. 替换 ...
- Qt读取TXT文件时,GBK与UTF-8编码判断
读取txt文件时,很多时候无法获取文件的编码格式.如果直接进行使用,则有可能出现乱码.需要在使用前将其转为Unicode(Qt的默认编码格式). 虽然实际的编码格式种类非常多,但平常主要使用的有GBK ...
- Django 应用 静态文件配置
Django 应用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- 自学Java第二周的总结
在这一周里我在网上学习了java的对象和类,了解了对象与类以及简单的用法.对象是类的一个实例(对象不是找个女朋友),有状态和行为.例如,一条狗是一个对象,它的状态有:颜色.名字.品种:行为有:摇尾巴. ...
- 如何使用Linux 命令more 查看文本文件
Linux 下有很多实用工具可以让你在终端界面查看文本文件.其中一个就是 more. more 跟我之前另一篇文章里写到的工具 —— less 很相似.它们之间的主要不同点在于 more 只允许你向前 ...