LibreOj 6279数列分块入门 3 练习了一下set
题目链接:https://loj.ac/problem/6279
推荐博客:https://blog.csdn.net/qq_36038511/article/details/79725027
这题区间查询某个数字x的前驱(区间里比x小的最大的数),我用的是二分,自己手写二分的时候一直用的是没有排序的数组,好无语,后面又用set做了一遍,熟系一下set。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<set>
#include<cstdio>
#include<string>
#include<deque>
using namespace std;
typedef long long LL;
#define eps 1e-8
#define INF 0x3f3f3f3f
#define maxn 50005*2
/*struct point{
int u,w;
};
bool operator <(const point &s1,const point &s2)
{
if(s1.w!=s2.w)
return s1.w>s2.w;
else
return s1.u>s2.u;
}*/
int n,m,k,t,block;
int a[maxn],tag[maxn],lump[maxn];
vector<int>ve[];
void update(int x)
{
ve[x].clear();//把第x块原来的值清除
for(int i=(x-)*block+;i<=min(x*block,n);i++)//这里要加min,因为第x块可能是最后一块并且是不完整的
ve[x].push_back(a[i]);//把增加了的值重新压入
sort(ve[x].begin(),ve[x].end());//排序
}
void add(int l,int r,int c)
{
for(int i=l;i<=min(lump[l]*block,r);i++)//暴力更新左边不完整的块
a[i]+=c;
update(lump[l]);//更新值并且重新排序
if(lump[l]!=lump[r])
{
for(int i=(lump[r]-)*block+;i<=r;i++)//暴力更新右边不完整的块
a[i]+=c;
update(lump[r]);
}
for(int i=lump[l]+;i<=lump[r]-;i++)
tag[i]+=c;
}
int binary_search(int x,int w)
{
int l=,r=ve[x].size()-;
int pos=-INF;
while(l<=r)
{
int mid=(l+r)/;
if(ve[x][mid]>=w)
{
r=mid-;
pos=r;
}
else
{
l=mid+;
pos=mid;
}
}
if(pos>=&&pos<ve[x].size()&&ve[x][pos]<w)
return ve[x][pos];
else
return INF;
}
int find(int l,int r,int c)
{
int ans=-;
for(int i=l;i<=min(lump[l]*block,r);i++)//在左边不完整的块里查找
{
if(a[i]+tag[lump[l]]<c)
ans=max(ans,a[i]+tag[lump[l]]);
}
if(lump[l]!=lump[r])
{
for(int i=(lump[r]-)*block+;i<=r;i++)//在右边的不完整的块里查找
{
if(a[i]+tag[lump[r]]<c)
ans=max(ans,a[i]+tag[lump[r]]);
}
}
for(int i=lump[l]+;i<=lump[r]-;i++)//在中间的完整的并且有序的块里二分查找
{
int t=c-tag[i];
/*int s=lower_bound(ve[i].begin(),ve[i].end(),t)-ve[i].begin();
if(s!=0&&tag[i]+ve[i][s-1]<c)
ans=max(ve[i][s-1]+tag[i],ans);*/
int s=binary_search(i,t);
if(s+tag[i]<c)
ans=max(ans,s+tag[i]);
}
return ans;
}
int main()
{
scanf("%d",&n); fill(tag,tag+maxn-,);
for(int i=;i<=n;i++)
ve[i].clear();
block=sqrt(n); for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
lump[i]=(i-)/block+;
ve[lump[i]].push_back(a[i]);
}
for(int i=;i<=lump[n];i++)//把每一块的值进行排序
sort(ve[i].begin(),ve[i].end()); for(int j=;j<=n;j++)
{
int op,l,r,c;
scanf("%d%d%d%d",&op,&l,&r,&c);
if(op==)
add(l,r,c);
else
{
int ans=find(l,r,c);
printf("%d\n",ans);
}
}
return ;
}
用set的代码:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<set>
#include<cstdio>
#include<string>
#include<deque>
using namespace std;
typedef long long LL;
#define eps 1e-8
#define INF 0x3f3f3f3f
#define maxn 50005*2
/*struct point{
int u,w;
};
bool operator <(const point &s1,const point &s2)
{
if(s1.w!=s2.w)
return s1.w>s2.w;
else
return s1.u>s2.u;
}*/
int n,m,k,t,block;
int a[maxn],tag[maxn],lump[maxn];
set<int>se[];
void add(int l,int r,int c)
{
for(int i=l;i<=min(lump[l]*block,r);i++)//暴力更新左边不完整的块
{
se[lump[l]].erase(a[i]);
a[i]+=c;
se[lump[l]].insert(a[i]);
}
if(lump[l]!=lump[r])
{
for(int i=(lump[r]-)*block+;i<=r;i++)//暴力更新右边不完整的块
{
se[lump[r]].erase(a[i]);
a[i]+=c;
se[lump[r]].insert(a[i]);
}
}
for(int i=lump[l]+;i<=lump[r]-;i++)
tag[i]+=c;
}
int find(int l,int r,int c)
{
int ans=-;
for(int i=l;i<=min(lump[l]*block,r);i++)//在左边不完整的块里查找
{
if(a[i]+tag[lump[l]]<c)
ans=max(ans,a[i]+tag[lump[l]]);
}
if(lump[l]!=lump[r])
{
for(int i=(lump[r]-)*block+;i<=r;i++)//在右边的不完整的块里查找
{
if(a[i]+tag[lump[r]]<c)
ans=max(ans,a[i]+tag[lump[r]]);
}
}
for(int i=lump[l]+;i<=lump[r]-;i++)//在中间的完整的并且有序的块里二分查找
{
int t=c-tag[i];
set<int>::iterator it=se[i].lower_bound(t);
if(it==se[i].begin())
continue;
it--;
ans=max(ans,tag[i]+(*it));
}
return ans;
}
int main()
{
scanf("%d",&n); fill(tag,tag+maxn-,);
for(int i=;i<=;i++)
se[i].clear();
block=sqrt(n); for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
lump[i]=(i-)/block+;
se[lump[i]].insert(a[i]);
}
for(int j=;j<=n;j++)
{
int op,l,r,c;
scanf("%d%d%d%d",&op,&l,&r,&c);
if(op==)
add(l,r,c);
else
{
int ans=find(l,r,c);
printf("%d\n",ans);
}
}
return ;
}
LibreOj 6279数列分块入门 3 练习了一下set的更多相关文章
- LibreOJ 6279 数列分块入门 3(分块+排序)
题解:自然是先分一波块,把同一个块中的所有数字压到一个vector中,将每一个vector进行排序.然后对于每一次区间加,不完整的块加好后暴力重构,完整的块直接修改标记.查询时不完整的块暴力找最接近x ...
- LOJ #6279. 数列分块入门 3-分块(区间加法、查询区间内小于某个值x的前驱(比其小的最大元素))
#6279. 数列分块入门 3 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: hzwer 提交提交记录统计测试数据讨论 3 题目描述 给 ...
- loj 6278 6279 数列分块入门 2 3
参考:「分块」数列分块入门1 – 9 by hzwer 2 Description 给出一个长为\(n\)的数列,以及\(n\)个操作,操作涉及区间加法,询问区间内小于某个值\(x\)的元素个数. 思 ...
- LibreOJ 6277. 数列分块入门 1 题解
题目链接:https://loj.ac/problem/6277 题目描述 给出一个长为 \(n\) 的数列,以及 \(n\) 个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字 \( ...
- LibreOJ 6277 数列分块入门 1(分块)
题解:感谢hzwer学长和loj让本蒟蒻能够找到如此合适的入门题做. 这是一道非常标准的分块模板题,本来用打标记的线段树不知道要写多少行,但是分块只有这么几行,极其高妙. 代码如下: #include ...
- LibreOJ 6278. 数列分块入门 2 题解
题目链接:https://loj.ac/problem/6278 题目描述 给出一个长为 \(n\) 的数列,以及 \(n\) 个操作,操作涉及区间加法,询问区间内小于某个值 \(x\) 的元素个数. ...
- LOJ 6279 数列分块入门3
嗯... 题目链接:https://loj.ac/problem/6279 这道题在分块的基础上用vc数组记录,然后最后分三块,两边暴力枚举找前驱,中间lower_bound找前驱. AC代码: #i ...
- #6279. 数列分块入门 3(询问区间内小于某个值 xx 的前驱(比其小的最大元素))
题目链接:https://loj.ac/problem/6279 题目大意:中文题目 具体思路:按照上一个题的模板改就行了,但是注意在整块查找的时候的下标问题. AC代码: #include<b ...
- LibreOJ 6285. 数列分块入门 9
题目链接:https://loj.ac/problem/6285 其实一看到是离线,我就想用莫队算法来做,对所有询问进行分块,但是左右边界移动的时候,不会同时更新数字最多的数,只是后面线性的扫了一遍, ...
随机推荐
- Android Netty Server
项目源码在github上,请看这里-->Android Netty Server Android netty server Start a netty server on android Dow ...
- idea 添加 VUE 的语法支持和开发
<一>VUE的开发分两种,一种是直接在HTML文件中使用,一种是VUE文件的形式开发 1,首先我们先让 HTML 文件支持 VUE 的语法指令提示 2,File -> Setting ...
- java环境变量配置方法
原创文章,转载请注明出处. 这是本人2011-9-4记录的,现在把它放在博客上. windows xp下配置JDK环境变量: 1.安装JDK,安装过程中可以自定义安装目录等信息,例如我们选择安装目录为 ...
- Crash 文件调试
Xcode目录下执行 find . -name symbolicatecrash 找到symbolicatecrash位置,将其拷贝到debug用的文件夹下 执行命令 export DEVELOPER ...
- Delphi 修改本地日期和时间
procedure TForm1.ModifySysdate(D: Double); var systemtime:Tsystemtime; DateTime:TDateTime; begin Set ...
- vue:在路由跳转中使用拦截器
1:首先在路由对象中的某一个具体的路由对象加这样一个属性 meta: { requireAuth:true } 2:然后在main.js中添加这段代码 router.beforeEach((to, ...
- Review CSS Selectors
Throughout this lesson, you learned how to select HTML elements with CSS and apply styles to them. L ...
- 尚硅谷redis学习5-初识redis.conf
redis.conf是redis的配置文件,在解压后的redis安装文件夹下 单位 1 配置大小单位,开头定义了一些基本的度量单位,只支持bytes,不支持bit 2 对大小写不敏感 包含 ...
- Haskell语言学习笔记(86)字符串格式化与插值
String 的格式化 Text.Printf 这个模块用来处理字符串格式化. printf :: PrintfType r => String -> r printf 用于格式化字符串, ...
- js字符串和控制语句
1.js的字符串 * 字符串* 字符串是js数据类型中的一种*字符串拼接:+,加号有两层含义* 1.数学中的加法运算;* 2.字符串连接,当加号的任意一边是一个字符串,那就是字符串连接的意思; < ...