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. 如何更改iTunes备份地址(修改iphone ipad 备份地址) itunes文件目录修改方法 【亲测有效,附带原理说明】

    前言 C盘空间有限,但是iTunes就是那么龌龊,只能把手机备份存到C盘.那么怎么才能把备份文件存到其他分区的文件夹里面呢? 当时我想先看看度娘,看看有没有现成的! 结果 nnd!! 我看了一大堆相关 ...

  2. 最经典的SDK程序结构 HelloWin

    程序运行效果:在创建窗口的时候,播放一个声音.且在窗口的客户区中央画一句文字:Hello, Windows 98!,无论程序怎么移动.最大化,文字始终在程序的中央部位. 程序总共分为六个步骤:定义,注 ...

  3. 修改android系统开机动画

    本文转载自:http://blog.csdn.net/u012301841/article/details/51598115 修改android系统开机动画

  4. openSTack manual 整合调优

  5. sql小计合计

    转自:http://www.jb51.net/article/18860.htm 这里介绍sql server2005里面的一个使用实例: CREATE TABLE tb(province nvarc ...

  6. lodop使用

    根据相应的操作系统,安装install_lodop32.exe文件,它里面包含两个exe文件install_lodop32.exe和install_lodop64.exe,在页面的头部中引入: < ...

  7. bzoj1143(2718)[CTSC2008]祭祀river(最长反链)

    1143: [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2781  Solved: 1420[Submit][S ...

  8. 原生JS---8

    原生js学习笔记8——Ajax基础   什么是Ajax 不刷新页面的情况下从服务器获取.提交数据的一种数据交互方式. Ajax使用步骤 1.创建Ajax对象 var httpRequest = new ...

  9. 练习2 及pl/sql

    Rownum 如果不是对主键排序是不会变得 -查询没有学分的学生信息 --SELECT * FROM z_student zs WHERE zs.code NOT IN (SELECT DISTINC ...

  10. thymeleaf公共页面元素抽取

    1.抽取公共片段 使用thymeleaf的th:fragment为样抽取的公共片段命名, 如下把div标签命名为 copy,就可以获取到div整个里的内容<div th:fragment=&qu ...