GukiZ and GukiZiana

Time Limit: 10000ms
Memory Limit: 262144KB

This problem will be judged on CodeForces. Original ID: 551E
64-bit integer IO format: %I64d      Java class name: (Any)

 

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 numberyGukiZiana(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 nq (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.

 

Sample Input

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

Source

 
解题:分块搞
 
 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = ;
LL a[maxn*maxn],lazy[maxn],x;
vector<int>block[maxn];
int b_size,N,pos[maxn*maxn],n,q,cmd,L,R;
bool cmp(const int x,const int y) {
if(a[x] == a[y]) return x < y;
return a[x] < a[y];
}
void update(int L,int R,LL x) {
int k = pos[L],t = pos[R];
if(k == t) {
for(int i = L; i <= R; ++i) a[i] += x;
sort(block[k].begin(),block[k].end(),cmp);
return;
}
for(int i = k + (pos[L-] == k); i <= t - (pos[R + ] == t); ++i) lazy[i] += x;
if(pos[L-] == k) {
for(int i = L; pos[i] == k; ++i) a[i] += x;
sort(block[k].begin(),block[k].end(),cmp);
}
if(pos[R+] == t) {
for(int i = R; pos[i] == t; --i) a[i] += x;
sort(block[t].begin(),block[t].end(),cmp);
}
}
LL query(LL x) {
int L = -,R = -,i;
for(i = ; i <= N; ++i){
a[] = x - lazy[i];
vector<int>::iterator it = lower_bound(block[i].begin(),block[i].end(),,cmp);
if(it == block[i].end()) continue;
if(a[*it] + lazy[i] == x){
L = *it;
break;
}
}
if(L == -) return -;
for(int j = N; j >= i; --j){
a[n+] = x - lazy[j];
vector<int>::iterator it = lower_bound(block[j].begin(),block[j].end(),n+,cmp);
if(it == block[j].begin()) continue;
--it;
if(a[*it] + lazy[j] == x){
R = *it;
break;
}
}
return R - L;
}
int main() {
ios::sync_with_stdio(false);
cin.tie();
cin>>n>>q;
b_size = ceil(sqrt(n*1.0));
for(int i = ; i <= n; ++i) {
cin>>a[i];
pos[i] = (i - )/b_size + ;
block[pos[i]].push_back(i);
}
N = (n - )/b_size + ;
for(int i = ; i <= N; ++i) sort(block[i].begin(),block[i].end(),cmp);
while(q--) {
cin>>cmd;
if(cmd == ) {
cin>>L>>R>>x;
update(L,R,x);
} else {
cin>>x;
cout<<query(x)<<endl;
}
}
return ;
}

CodeForces 551E GukiZ and GukiZiana的更多相关文章

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

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

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

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

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

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

  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 551 E - GukiZ and GukiZiana

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

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

  7. [codeforces551E]GukiZ and GukiZiana

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

  8. (分块)GukiZ and GukiZiana CodeForces - 551E

    题意: 给你一段序列,并且有两种操作 操作①:将序列中从l-r每个元素加上x 操作②:在序列中找到ai=aj=y,j-i的最大值,如果找不到则输出-1 思路: 直接分块暴力即可 对于区间加,普通标记加 ...

  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. Navicat Lite 提示Connection to mysql server on 10065

    Navicat Lite 提示Connection to mysql server on 10065 验证过主要是防火墙问题 [root@014136251035 zhop]# vi /etc/sys ...

  2. 传智播客C/C++学员荣膺微软&amp;Cocos 2d-x黑客松最佳创新奖

     6月30日,历时32小时的微软开放技术Cocos 2d-x 编程黑客松在北京望京微软大厦成功落下帷幕,这是微软开放技术首次联合Cocos 2d-x 在中国举办黑客松. 此次活动共同拥有包含传智播 ...

  3. luogu1026 统计单词个数

    题目大意 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1< k< =40),且每份中包含 ...

  4. luogu1006 传纸条

    题目大意 小渊坐在矩阵的左上角,坐标 (1,1 ),小轩坐在矩阵的右下角,坐标 (m,n) .从小渊传到小轩的纸条只可以向下或者向右传递,从小轩传给小渊的纸条只可以向上或者向左传递. 在活动进行中,小 ...

  5. oc53--autorelease注意事项

    // // main.m // autorelease注意事项 #import <Foundation/Foundation.h> #import "Person.h" ...

  6. 在ubuntu中安装photoshop cs6

    对于很多专业的PS高手来说,真心难以找到顺手的且可以完美替代PS的软件,所以我们这里的解决办法就是用wine来安装. 虽然网上有很多的wine安装ps的方法,但是在使用过程往住会发生莫名其妙的崩溃,体 ...

  7. oracle buffer cache的基本原理

    Buffer cache 的原理 一. 1·)当一个服务器进程需要读数据到buffer cache中时,首先必须判断该数据在buffer 中是否存在,如果存在且可用,则获取该数据,根据lru算法在lr ...

  8. Django day07 (一) 模板的导入 母板的继承 静态文件配置

    一:模板的导入 -写一个模板 {% include '模板的名字' %} 二:母板的继承 -写一个母版(可以留多个盒子) {% block 名字 %} / {% endblock %} 三:静态文件配 ...

  9. BZOJ 3876 有上下界的网络流

    思路: 套用有上下界的网络流 就好了   (这算是裸题吧) 比如 有条 x->y 的边  流量上限为R 下限为L 那么du[x]-=L,du[y]+=L 流量上限变成R-L du[x]>0 ...

  10. POJ 2823 线段树 Or 单调队列

    时限12s! 所以我用了线段树的黑暗做法,其实正解是用单调队列来做的. //By SiriusRen #include <cstdio> #include <cstring> ...