【LOJ#6279】数列分块3
题目大意:维护 N 个数组成的序列,支持两种操作:区间加、区间查询某个值的前驱(小于该值的最大值,若无前驱,输出-1)。
题解1:可以像分块2一样,维护每个块内元素的一个有序序列,每次查询时二分查找即可。
代码如下
#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int maxn=1e5+10;
const int maxb=1010;
const int inf=0x3f3f3f3f;
inline int read(){
int x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
}
int n,m,a[maxn];
struct node{
int l,r,add;
}b[maxb];
int tot,bl[maxn];vector<int> v[maxb];
void make_block(){
tot=sqrt(n);
for(int i=1;i<=tot;i++)b[i].l=b[i-1].r+1,b[i].r=i*tot;
if(b[tot].r<n)++tot,b[tot].l=b[tot-1].r+1,b[tot].r=n;
for(int i=1;i<=tot;i++){
for(int j=b[i].l;j<=b[i].r;j++)bl[j]=i,v[i].pb(a[j]);
sort(all(v[i]));
}
}
inline void rebuild(int p){
v[p].clear();
for(int i=b[p].l;i<=b[p].r;i++)v[p].pb(a[i]);
sort(all(v[p]));
}
void modify(int l,int r,int c){
int x=bl[l],y=bl[r];
if(x==y){
for(int i=l;i<=r;i++)a[i]+=c;
rebuild(x);
}else{
for(int i=x+1;i<=y-1;i++)b[i].add+=c;
for(int i=l;i<=b[x].r;i++)a[i]+=c;
for(int i=b[y].l;i<=r;i++)a[i]+=c;
rebuild(x),rebuild(y);
}
}
int query(int l,int r,int c){
int x=bl[l],y=bl[r],res=-inf;
if(x==y){
for(int i=l;i<=r;i++){
int u=a[i]+b[x].add;
if(u<c)res=max(res,u);
}
}else{
for(int i=x+1;i<=y-1;i++){
auto u=lower_bound(all(v[i]),c-b[i].add);
if(u!=v[i].begin())res=max(res,*(--u)+b[i].add);
}
for(int i=l;i<=b[x].r;i++){
int u=a[i]+b[x].add;
if(u<c)res=max(res,u);
}
for(int i=b[y].l;i<=r;i++){
int u=a[i]+b[y].add;
if(u<c)res=max(res,u);
}
}
return res==-inf?-1:res;
}
int main(){
n=m=read();
for(int i=1;i<=n;i++)a[i]=read();
make_block();
while(m--){
int opt=read(),l=read(),r=read(),c=read();
if(opt==0)modify(l,r,c);
else if(opt==1)printf("%d\n",query(l,r,c));
}
return 0;
}
题解2:可以在每个块内维护一个平衡树,支持插入删除操作,且平衡树具有自动排序功能,每次插入时将原来的值删除,并插入修改后的数值。不过常数较大,比第一种方法慢了一倍。QAQ
代码如下
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
inline int read(){
int x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
}
int n,q,a[maxn],pos[maxn],tot;
struct node{
int l,r,add;
set<int> st;
}b[1000];
void make_block(){
tot=(int)sqrt(n);
for(int i=1;i<=tot;i++)b[i].l=(i-1)*tot+1,b[i].r=i*tot;
if(b[tot].r<n)++tot,b[tot].l=b[tot-1].r+1,b[tot].r=n;
for(int i=1;i<=tot;i++)
for(int j=b[i].l;j<=b[i].r;j++)
pos[j]=i,b[i].st.insert(a[j]);
}
void read_and_parse(){
n=read(),q=n;
for(int i=1;i<=n;i++)a[i]=read();
make_block();
}
void modify(int l,int r,int val){
int x=pos[l],y=pos[r];
if(x==y){
for(int i=l;i<=r;i++)b[x].st.erase(a[i]),a[i]+=val,b[x].st.insert(a[i]);
}else{
for(int i=x+1;i<=y-1;i++)b[i].add+=val;
for(int i=l;i<=b[x].r;i++)b[x].st.erase(a[i]),a[i]+=val,b[x].st.insert(a[i]);
for(int i=b[y].l;i<=r;i++)b[y].st.erase(a[i]),a[i]+=val,b[y].st.insert(a[i]);
}
}
int query(int l,int r,int val){
int x=pos[l],y=pos[r],ans=-inf;
if(x==y){
for(int i=l;i<=r;i++){
int v=a[i]+b[x].add;
if(v<val&&v>ans)ans=v;
}
}else{
for(int i=x+1;i<=y-1;i++){
set<int>::iterator it=b[i].st.lower_bound(val-b[i].add);
if(it==b[i].st.begin())continue;
ans=max(ans,*--it+b[i].add);
}
for(int i=l;i<=b[x].r;i++){
int v=a[i]+b[x].add;
if(v<val&&v>ans)ans=v;
}
for(int i=b[y].l;i<=r;i++){
int v=a[i]+b[y].add;
if(v<val&&v>ans)ans=v;
}
}
return ans==-inf?-1:ans;
}
void solve(){
int opt,l,r,val;
while(q--){
opt=read(),l=read(),r=read(),val=read();
if(opt==0)modify(l,r,val);
else if(opt==1)printf("%d\n",query(l,r,val));
}
}
int main(){
read_and_parse();
solve();
return 0;
}
【LOJ#6279】数列分块3的更多相关文章
- LOJ #6279. 数列分块入门 3-分块(区间加法、查询区间内小于某个值x的前驱(比其小的最大元素))
#6279. 数列分块入门 3 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 3 题目描述 给 ...
- LOJ 6279 数列分块入门3
嗯... 题目链接:https://loj.ac/problem/6279 这道题在分块的基础上用vc数组记录,然后最后分三块,两边暴力枚举找前驱,中间lower_bound找前驱. AC代码: #i ...
- LOJ#6279. 数列分块入门 3
区间加值还是正常的操作,查找前驱的时候用lower_bound查找,然后范围所在位置的值 #include<map> #include<set> #include<cti ...
- LOJ——#6277. 数列分块入门 1
~~推荐播客~~ 「分块」数列分块入门1 – 9 by hzwer 浅谈基础根号算法——分块 博主蒟蒻,有缘人可直接观摩以上大佬的博客... #6277. 数列分块入门 1 题目大意: 给出一个长为 ...
- loj 6278 6279 数列分块入门 2 3
参考:「分块」数列分块入门1 – 9 by hzwer 2 Description 给出一个长为\(n\)的数列,以及\(n\)个操作,操作涉及区间加法,询问区间内小于某个值\(x\)的元素个数. 思 ...
- LOJ 6277-6280 数列分块入门 1-4
数列分块是莫队分块的前置技能,练习一下 1.loj6277 给出一个长为n的数列,以及n个操作,操作涉及区间加法,单点查值. 直接分块+tag即可 #include <bits/stdc++.h ...
- LOJ #6285. 数列分块入门 9-分块(查询区间的最小众数)
#6285. 数列分块入门 9 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 2 题目描述 给 ...
- LOJ #6284. 数列分块入门 8-分块(区间查询等于一个数c的元素,并将这个区间的所有元素改为c)
#6284. 数列分块入门 8 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 2 题目描述 给出 ...
- LOJ #6283. 数列分块入门 7-分块(区间乘法、区间加法、单点查询)
#6283. 数列分块入门 7 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 2 题目描述 给出 ...
- LOJ #6282. 数列分块入门 6-分块(单点插入、单点查询、数据随机生成)
#6282. 数列分块入门 6 内存限制:256 MiB时间限制:500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 1 题目描述 给出 ...
随机推荐
- TRIO-basic指令--MOVEMODIFY
Syntax: MOVEMODIFY(position) Parameters: position: Absolute position for the current move to complet ...
- Mysql主从同步(1) - 概念和原理介绍 以及 主从/主主模式 部署记录
Mysql复制概念Mysql内建的复制功能是构建大型高性能应用程序的基础, 将Mysql数据分布到多个系统上,这种分布机制是通过将Mysql某一台主机数据复制到其它主机(slaves)上,并重新执行一 ...
- Python常见字符编码间的转换
主要内容: 1.Unicode 和 UTF-8的爱恨纠葛 2.字符在硬盘上的存储 3.编码的转换 4.验证编码是否转换正确 5.Python bytes类型 前 ...
- Week2 关于代码规范的一些认识
代码规范 我觉得代码规范是有必要的,而对于以下的四个观点我要提出自己的反驳: 这些规范都是官僚制度下产生的浪费大家的编程时间.影响人们开发效率, 浪费时间的东西 首先应该明白,什么是“编码规范”?它不 ...
- Linux内核及分析 第七周 可执行程序的装载
实验步骤 1. 更新menu,用test.c覆盖test_exec.c 2. 把init 和 hello 放到了rootfs.img目录下,执行exec命令的时候自动加载了hello程序 3. 执行e ...
- linux内核分析第四次实验
实验步骤: 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用.本次实验中我使用第20号系统调用getpid()函数,用于取得进程识别码. C代码(getpid.c): #include ...
- David Silver强化学习Lecture2:马尔可夫决策过程
课件:Lecture 2: Markov Decision Processes 视频:David Silver深度强化学习第2课 - 简介 (中文字幕) 马尔可夫过程 马尔可夫决策过程简介 马尔可夫决 ...
- Java WebMail
http://www.open-open.com/06.htm http://www.oracle.com/technetwork/java/javamail/third-party-136965.h ...
- [转帖]DevOps/TestOps概念
发现收藏不好用..还是转吧.. https://www.cnblogs.com/fnng/p/8232410.html DevOps/TestOps概念 2018-01-07 22:02 by 虫师, ...
- 给kali linux2.0装一个中文输入法
没有中文输入法好痛苦啊.. 毕竟做了无限网卡,虚拟机和主机可以完完全全当两台设备使用了,所以kali还是需要一个中文输入法才方便. 由于使用的是比较新的kali版本和源,现在安装fcitx已经可以直接 ...