(分块)GukiZ and GukiZiana CodeForces - 551E
题意:
给你一段序列,并且有两种操作
操作①:将序列中从l-r每个元素加上x
操作②:在序列中找到ai=aj=y,j-i的最大值,如果找不到则输出-1
思路:
直接分块暴力即可
对于区间加,普通标记加暴力即可
对于找最大值,直接在每个块中二分找y,找不到即为-1
#include<iostream>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
const int maxn=5e5+10;
ll n,blo,tot;
ll a[maxn];//bl数组记录属于哪个块
int bel[maxn],m;
ll tag[maxn];//维护区间加标记
vector <ll> vc[maxn];
void build()
{
blo=sqrt(n);
tot=n/blo;
if(n%blo) tot++;
for(int i=1;i<=n;i++){
bel[i]=(i-1)/blo+1;
vc[bel[i]].push_back(a[i]);
}
for(int i=1;i<=tot;i++)
sort(vc[i].begin(),vc[i].end());
}
void reset(int x)
{
vc[x].clear();
for(int i=(x-1)*blo+1;i<=x*blo;i++)
vc[x].push_back(a[i]);
sort(vc[x].begin(),vc[x].end());
}
void add(int l,int r,int x)
{
int b1=bel[l],b2=bel[r];
if(b1==b2){
for(int i=l;i<=r;i++)
a[i]+=x;
reset(b1);
}
else{
for(int i=l;i<=b1*blo;i++)
a[i]+=x;
reset(b1);
for(int i=b1+1;i<b2;i++)
tag[i]+=x;
for(int i=(b2-1)*blo+1;i<=r;i++) a[i]+=x;
reset(b2);
}
}
int query(int y)
{
int l=0,r=0;
for(int i=1;i<=tot;i++){
int pos=lower_bound(vc[i].begin(),vc[i].end(),y-tag[i])-vc[i].begin();
if(y-tag[i] == vc[i][pos]){
for(int j=(i-1)*blo+1;j<=i*blo;j++)
if(a[j]+tag[i]==y){
l=j;
break;
}
break;
}
}
if(l==0) return -1;
for(int i=tot;i>=1;i--){
int pos=lower_bound(vc[i].begin(),vc[i].end(),y-tag[i])-vc[i].begin();
if(y-tag[i] == vc[i][pos]){
for(int j=i*blo;j>(i-1)*blo;j--)
if(a[j]+tag[i]==y){
r=j;
break;
}
break;
}
}
return r-l;
} int main()
{
int q,l,r,y,op;
scanf("%lld%d",&n,&q);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build();
for(int i=1;i<=q;i++){
scanf("%d",&op);
if(op==1){
scanf("%d%d%d",&l,&r,&y);
add(l,r,y);
}
else{
scanf("%d",&y);
printf("%d\n",query(y));
}
}
return 0;
}
(分块)GukiZ and GukiZiana CodeForces - 551E的更多相关文章
- Codeforces 551E GukiZ and GukiZiana(分块思想)
题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...
- Codeforces 551E - GukiZ and GukiZiana(分块)
Problem E. GukiZ and GukiZiana Solution: 先分成N=sqrt(n)块,然后对这N块进行排序. 利用二分查找确定最前面和最后面的位置. #include < ...
- CodeForces 551E GukiZ and GukiZiana
GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...
- Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana 分块
E. GukiZ and GukiZiana Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55 ...
- Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana(分块)
E. GukiZ and GukiZiana time limit per test 10 seconds memory limit per test 256 megabytes input stan ...
- CF 551E. GukiZ and GukiZiana [分块 二分]
GukiZ and GukiZiana 题意: 区间加 给出$y$查询$a_i=a_j=y$的$j-i$最大值 一开始以为和论文CC题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...
- Codeforces 551 E - GukiZ and GukiZiana
E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...
- [codeforces551E]GukiZ and GukiZiana
[codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...
- Codeforces 307 div2 E.GukiZ and GukiZiana 分块
time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...
随机推荐
- Git的使用--如何将本地项目上传到Github(两种简单、方便的方法..)
https://blog.csdn.net/u014135752/article/details/79951802 总结:其实只需要进行下面几步就能把本地项目上传到Github 1.在本地创建一个版本 ...
- linux 存取 I/O 内存
在一些平台上, 你可能逃过作为一个指针使用 ioremap 的返回值的惩罚. 这样的使用不 是可移植的, 并且, 更加地, 内核开发者已经努力来消除任何这样的使用. 使用 I/O 内 存的正确方式是通 ...
- 土旦:移动端 Vue+Vant 的Uploader 实现 :上传、压缩、旋转图片
面向百度开发 html <van-uploader :after-read="onRead" accept="image/*"> <img s ...
- C#面试题整理2(不带答案)
一.C# 理论 1.1.简述 private. protected. public. internal.protected internal 访问修饰符和访问权限 1.2.简述abstract.sea ...
- k8s的网络方案对比
如下图,三台虚拟机k8s-master.k8s-node-1.k8s-node-2组成k8s集群,网络拓扑和节点IP分配如下图: 一.flannel组网方案 https://github.com/co ...
- Struts2 控件标签
Struts 2 的标签有一组标签,更容易控制流程页面执行.以下是重要的Struts2控制标签列表: if /else 标签: 这些标签执行可在每一种语言找到的一种基本条件流程. 'If'标签可用于本 ...
- Mybatis 多对多(易百教程)
mybatis3.0 添加了association和collection标签专门用于对多个相关实体类数据进行级联查询,但仍不支持多个相关实体类数据的级联保存和级联删除操作.因此在进行实体类多对多映射表 ...
- javaweb-选课系统
选课系统中用到了4个表,分别是classs.yonghu.teacher.student.在用户中存放管理员的信息name和password以及id,在另三个表中存放对应的数据如图: calss: t ...
- vue学习笔记(四)事件处理器
前言 在上一章vue学习笔记(三)class和style绑定的内容中,我们学习了如何在vue中绑定class和style,介绍了常用的绑定方法,class的数组绑定和对象绑定以及style的数组绑定和 ...
- 洛谷$P$2286 宠物收养场 $[HNOI2004]$ $splay$
正解:$splay$ 解题报告: 传送门! $splay$板子,,,? 先考虑这题要实现些什么东西嘛$QwQ$ 其实只要实现一个东西?就查询数列中与给定数字相差最小的数,显然用$splay$查询前驱后 ...