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个洞,每个洞不能 ...
随机推荐
- JSP—连接池
1:为什么要使用连接池? 解决频繁连接释放造成的资源浪费 2:配置好的数据库连接池也是以数据源DateSource的形式存, 连接池的实现类负责建立与数据库的连接. 3:使用连接池关闭资源的区别? 使 ...
- SQLGetStmtAttr
SQLGetStmtAttr 函数定义: SQLRETURN SQLGetStmtAttr( SQLHSTMT StatementHandle, SQLINTEGER Attribut ...
- ASP.NET界面重定向传值
这么说吧,当程序启动时,ASP.NET会自动创建一些经常使用的类的实例,而这些实例就是ASP.NET的内置对象,常用的实例对象有:Response(来自HttpResponse类).Request(来 ...
- hashcat 中文文档
hashcat 描述 hashcat是世界上最快,最先进的密码恢复工具. 此版本结合了以前基于CPU的hashcat(现在称为hashcat-legacy)和基于GPU的oclHashcat. H ...
- pyspider 示例
数据存放目录: C:\Users\Administrator\data 升级版(可加载文章内所有多层嵌套的图片标签) #!/usr/bin/env python # -*- encoding: utf ...
- 主流的Nosql数据库的对比
主流的Nosql数据库的对比 MongoDB,Cassandra,CouchDB,Hypertable, Redis,Riak,Neo4j,Hadoop HBase, Couchbase,Mem ...
- git克隆远程项目并创建本地对应分支
http://jingyan.baidu.com/article/19192ad83ea879e53e5707ce.html
- Linux服务器---配置nfs
配置nfs NFS服务的主要配置文件为/etc/exports./etc/exports文件内容格式: <输出目录> 客户端(选项:访问权限,用户映射,其他) 1.输出目录 输出目录是指N ...
- golang学习笔记13 Golang 类型转换整理 go语言string、int、int64、float64、complex 互相转换
golang学习笔记13 Golang 类型转换整理 go语言string.int.int64.float64.complex 互相转换 #string到intint,err:=strconv.Ato ...
- CATALINA_OPTS和 JAVA_OPTS区别
在Tomcat的catalina.sh文件中的启停server脚本中都应用到了两个变量: CATALINA_OPTS和JAVA_OPTS.用于保存Tomcat运行所需的各种参数. 他们在文件中的注释如 ...