E. GukiZ and GukiZiana
time limit per test

10 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Professor GukiZ was playing with arrays again and accidentally discovered new function, which he called GukiZiana. For given array a, indexed with integers from 1 to n, and number y, GukiZiana(a, y) represents maximum value of j - i, such that aj = ai = y. If there is no y as an element in a, then GukiZiana(a, y) is equal to  - 1. GukiZ also prepared a problem for you. This time, you have two types of queries:

  1. First type has form 1 l r x and asks you to increase values of all ai such that l ≤ i ≤ r by the non-negative integer x.
  2. Second type has form 2 y and asks you to find value of GukiZiana(a, y).

For each query of type 2, print the answer and make GukiZ happy!

Input

The first line contains two integers n, q (1 ≤ n ≤ 5 * 105, 1 ≤ q ≤ 5 * 104), size of array a, and the number of queries.

The second line contains n integers a1, a2, ... an (1 ≤ ai ≤ 109), forming an array a.

Each of next q lines contain either four or two numbers, as described in statement:

If line starts with 1, then the query looks like 1 l r x (1 ≤ l ≤ r ≤ n, 0 ≤ x ≤ 109), first type query.

If line starts with 2, then th query looks like 2 y (1 ≤ y ≤ 109), second type query.

Output

For each query of type 2, print the value of GukiZiana(a, y), for y value for that query.

Examples
Input
4 3
1 2 3 4
1 1 2 1
1 1 1 1
2 3
Output
2
Input
2 3
1 2
1 2 2 1
2 3
2 4
Output
0
-1
【分析】给出一个数组a,然后q次操作,1 l r x表示将区间[l,r]所有数+1 ; 2 y表示询问这个数组中最左边的y和最右边的y的下标差为多少。
可分块做。将所有块里面的数排序,(便于查找要查询的y)。对于更新操作,单独处理左端点和右端点的块,改变a[i];然后对于中间跨过的块,用lazy数组标记add值。
对于查询操作,依次遍历每个块,二分查找y的位置即可。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 1000000000
#define met(a,b) memset(a,b,sizeof a)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;
using namespace std;
const int N = 5e5+;
const int M = 7e2+;
int n,m;
int l[N],r[N],belong[N];
int cnt,num,x,v,ans;
int a[N],tot[M],lazy[M];
pair<int,int>p[M][M];
void init(){
num=sqrt(n);
cnt=n/num;
if(n%num)cnt++;
for(int i=;i<=n;i++){
belong[i]=(i-)/num+;
}
for(int i=;i<=cnt;i++){
l[i]=(i-)*num+;
r[i]=min(n,i*num);
for(int j=l[i];j<=r[i];j++){
p[i][++tot[i]]=make_pair(a[j],j);
}
sort(p[i]+,p[i]++tot[i]);
}
}
void update1(int b,int x){
if(lazy[b]+x>inf)lazy[b]=inf+;
else lazy[b]+=x;
}
void update2(int b,int ll,int rr,int x){
if(ll==l[b]&&rr==r[b]){
update1(b,x);
return;
}
for(int i=;i<=tot[b];i++){
int po=p[b][i].second;
if(po>=ll&&po<=rr){
p[b][i].first+=x;
if(p[b][i].first>inf)p[b][i].first=inf+;
}
}
sort(p[b]+,p[b]+tot[b]+);
}
int main() {
int op,ll,rr,x,y;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
init();
while(m--){
scanf("%d",&op);
if(op==){
scanf("%d%d%d",&ll,&rr,&x);
if(belong[ll]==belong[rr])update2(belong[ll],ll,rr,x);
else {
for(int i=belong[ll]+;i<=belong[rr]-;i++)update1(i,x);
update2(belong[ll],ll,r[belong[ll]],x);
update2(belong[rr],l[belong[rr]],rr,x);
}
}
else {
scanf("%d",&y);
int last=,fir=;
for(int i=cnt;i>=;i--){
if(!last){
int po=upper_bound(p[i]+,p[i]+tot[i]+,make_pair(y-lazy[i],))-p[i]-;
if(p[i][po].first+lazy[i]==y){
fir=last=p[i][po].second;
}
}
int po=lower_bound(p[i]+,p[i]+tot[i]+,make_pair(y-lazy[i],))-p[i];
if(p[i][po].first+lazy[i]==y){
fir=p[i][po].second;
}
}
if(!last)puts("-1");
else printf("%d\n",last-fir);
}
}
return ;
}

Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana(分块)的更多相关文章

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

  2. Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana (分块)

    题目地址:http://codeforces.com/contest/551/problem/E 将n平均分成sqrt(n)块,对每一块从小到大排序,并设置一个总体偏移量. 改动操作:l~r区间内,对 ...

  3. 水题 Codeforces Round #307 (Div. 2) A. GukiZ and Contest

    题目传送门 /* 水题:开个结构体,rk记录排名,相同的值有相同的排名 */ #include <cstdio> #include <cstring> #include < ...

  4. Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分

    C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...

  5. Codeforces Round #307 (Div. 2) A. GukiZ and Contest 水题

    A. GukiZ and Contest Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...

  6. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations 矩阵快速幂优化dp

    D. GukiZ and Binary Operations time limit per test 1 second memory limit per test 256 megabytes inpu ...

  7. Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 二分

    C. GukiZ hates Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations (矩阵高速幂)

    题目地址:http://codeforces.com/contest/551/problem/D 分析下公式能够知道,相当于每一位上放0或者1使得最后成为0或者1.假设最后是0的话,那么全部相邻位一定 ...

  9. Codeforces Round #307 (Div. 2) D. GukiZ and Binary Operations

    得到k二进制后,对每一位可取得的方法进行相乘即可,k的二进制形式每一位又分为2种0,1,0时,a数组必定要为一长为n的01串,且串中不出现连续的11,1时与前述情况是相反的. 且0时其方法总数为f(n ...

随机推荐

  1. 2015/9/19 Python基础(15):变量作用域及生成器

    变量作用域标识符的作用域是定义为其声明的可应用范围,或者即是我们所说的变量可见性.也就是,我们可以在程序的那个部分去访问一个制定的标识符.全局变量与局部变量定义在函数内的变量有局部作用域,在一个模块中 ...

  2. 【C++对象模型】第一章 关于对象

    1.C/C++区别 C++较之C的最大区别,无疑在于面向对象,C程序中程序性地使用全局数据.而C++采用ADT(abstract data tpye)或class hierarchy的数据封装.类相较 ...

  3. struts2常用标签之数据标签

    数据标签1  property标签  property标签的主要属性:  value:用来获取值的OGNL表达式,如果value属性值没有指定,那么将会被设定为top,也就是返回位于值栈最顶端的对象. ...

  4. Jmeter-分布式

    转载自: http://www.51testing.com/html/28/116228-247521.html 由于Jmeter本身的瓶颈,当需要模拟数以千计的并发用户时,使用单台机器模拟所有的并发 ...

  5. Jmeter-8-FTP测试

    1. 此处要深刻理解FTP的用法. 2. Get的时候填写的Remote File 路径/, 此处是相对路径. 实际为/home/user/ 3. Local file 此处要写到具体的文件. 4. ...

  6. intellj idea点击导航栏打开的一个类,怎么才能定位到类的目录

  7. python学习笔记(三)之变量和字符串

    在其他语言中,变量就是有名字的存储区,可以将值存储在变量中,也即内存中.在Python中略有不同,python并不是将值存储在变量中,更像是把名字贴在值上边.所以,有些python程序员会说pytho ...

  8. 6、MySQL索引种类

    1.普通索引 这是最基本的索引,它没有任何限制,比如上文中为title字段创建的索引就是一个普通索引,MyIASM中默认的BTREE类型的索引,也是我们大多数情况下用到的索引. –直接创建索引 CRE ...

  9. Python 源码学习之内存管理 -- (转)

    Python 的内存管理架构(Objects/obmalloc.c): _____ ______ ______ ________ [ int ] [ dict ] [ list ] ... [ str ...

  10. Java中通过方法创建一个http连接并请求(服务器间进行通信)

    服务器间进行通信只能通过流(Stream)的方式进行,不能用方法的返回值. 1.Java代码创建一个连接并请求该连接返回的数据 doGet()方法,execute()方法中调用 package dem ...