题意:

给你一段序列,并且有两种操作

操作①:将序列中从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的更多相关文章

  1. Codeforces 551E GukiZ and GukiZiana(分块思想)

    题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...

  2. Codeforces 551E - GukiZ and GukiZiana(分块)

    Problem E. GukiZ and GukiZiana Solution: 先分成N=sqrt(n)块,然后对这N块进行排序. 利用二分查找确定最前面和最后面的位置. #include < ...

  3. CodeForces 551E GukiZ and GukiZiana

    GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...

  4. 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 ...

  5. 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 ...

  6. CF 551E. GukiZ and GukiZiana [分块 二分]

    GukiZ and GukiZiana 题意: 区间加 给出$y$查询$a_i=a_j=y$的$j-i$最大值 一开始以为和论文CC题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...

  7. Codeforces 551 E - GukiZ and GukiZiana

    E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...

  8. [codeforces551E]GukiZ and GukiZiana

    [codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...

  9. Codeforces 307 div2 E.GukiZ and GukiZiana 分块

    time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...

随机推荐

  1. Spring Boot 各Starter介绍

    原文链接:https://blog.csdn.net/u014430366/article/details/53648139 Spring-Boot-Starters 最通俗的理解- jar 包,引用 ...

  2. Linux 内核PCI去除一个驱动

    去除一个驱动是一个非常容易的动作. 对于一个 PCI 驱动, 驱动调用 pci_unregister_driver 函数. 这个函数只调用驱动核心函数 driver_unregister, 使用 一个 ...

  3. vue-learning:8-template-v-on-and-modifier

    绑定元素事件的指令 v-on 及事件和修饰符 目录 对比原生事件绑定.jQuery事件绑定 Vue事件绑定 Vue绑定事件中获取事件对象event 事件修饰符 事件行为修饰符: stop / prev ...

  4. Makefile记录

    需要把sum.c编译汇编成可执行程序zzj zzj:sum.o gcc -o zzj sum.osum.o:sum.c gcc -c -o sum.o sum.cclean: rm -rf *.o z ...

  5. Servlet 会话

    在网络的七层模型中,会话层位于传输层之上,它定义如何开始.控制和结束一个会话.七层模式目前仅仅处于理论阶段,但是Web中借鉴了其中的一些思路.在Web中浏览器第一次发送请求到服务器开始直到一方断开为止 ...

  6. 16.MindManager整理交互思路

    点住主题同时按ins键可以插入一个支节点 右键主题选择下方的排列图表 可以选择排列方式 按住主题同时ctr+v就会粘帖成一个子主题 文本也可以复制黏贴 主题内容可以直接选择拖动更改结构 选择主题框上的 ...

  7. 0014 标签显示模式:display(重点)

    目标: 理解 标签的三种显示模式 三种显示模式的特点以及区别 理解三种显示模式的相互转化 应用 实现三种显示模式的相互转化 2.1 什么是标签显示模式 什么是标签的显示模式? 标签以什么方式进行显示, ...

  8. Spring Boot 单元测试示例

    Spring 框架提供了一个专门的测试模块(spring-test),用于应用程序的单元测试. 在 Spring Boot 中,你可以通过spring-boot-starter-test启动器快速开启 ...

  9. zabbix脚本监控mysql

    Zabbix监控mysql 1.1 客户端配置 1.1.1 安装客户端包 yum -y install unixODBC rpm -ivh zabbix-agent--.el6.x86_64.rpm ...

  10. 【题解】Vijos1404 遭遇战(最短路图论套路)

    [题解]Vijos1404 遭遇战(最短路图论套路) 感觉一定要有建模的思想,不管什么东西要抽象建模之后再用算法解决... 求最短代价就可能可以用最短路,这句话不是强行解释,而是因为图论建模过后,可能 ...