CodeForces 551E GukiZ and GukiZiana
GukiZ and GukiZiana
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 numbery, 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:
- 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.
- 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.
Sample Input
4 3
1 2 3 4
1 1 2 1
1 1 1 1
2 3
2
2 3
1 2
1 2 2 1
2 3
2 4
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的更多相关文章
- Codeforces 551E - GukiZ and GukiZiana(分块)
Problem E. GukiZ and GukiZiana Solution: 先分成N=sqrt(n)块,然后对这N块进行排序. 利用二分查找确定最前面和最后面的位置. #include < ...
- Codeforces 551E GukiZ and GukiZiana(分块思想)
题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...
- CF 551E. GukiZ and GukiZiana [分块 二分]
GukiZ and GukiZiana 题意: 区间加 给出$y$查询$a_i=a_j=y$的$j-i$最大值 一开始以为和论文CC题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...
- 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 551 E - GukiZ and GukiZiana
E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...
- 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 ...
- [codeforces551E]GukiZ and GukiZiana
[codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...
- (分块)GukiZ and GukiZiana CodeForces - 551E
题意: 给你一段序列,并且有两种操作 操作①:将序列中从l-r每个元素加上x 操作②:在序列中找到ai=aj=y,j-i的最大值,如果找不到则输出-1 思路: 直接分块暴力即可 对于区间加,普通标记加 ...
- Codeforces 307 div2 E.GukiZ and GukiZiana 分块
time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...
随机推荐
- Linux系统编程——特殊进程之僵尸进程
僵尸进程(Zombie Process) 进程已执行结束,但进程的占用的资源未被回收.这种进程称为僵尸进程. 在每一个进程退出的时候,内核释放该进程全部的资源.包含打开的文件.占用的内存等. 可是仍然 ...
- 卡尔曼滤波(Kalman Filter) 的进一步讨论
我们在上一篇文章中通过一个简单的样例算是入门卡尔曼滤波了.本文将以此为基础讨论一些技术细节. 卡尔曼滤波(Kalman Filter) http://blog.csdn.net/baimafujinj ...
- luogu2331 [SCOI2005]最大子矩阵
题目大意 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠.1≤n≤100,1≤m≤2,1≤k≤10. 思路 #include < ...
- 【IDEA】(4)---很好用的DEBUG功能
IDEA-DEBUG功能 一.常用快捷键 快捷键并不是完全一样的,我这边是MAC安装的IDEA, 这边最主要还是知道DEBUG时常用的功能. 1.快捷键 F7 #进入下一步,如果当前行是一个方法,则进 ...
- [BZOJ2321,LuoguP1861]星(之)器
丧心病狂的神仙题 丧心病狂的神仙题 丧心病狂的神仙题 显然,不管你怎么移动,答案都是一定的 然后我们很快能联系到物理里面的能量守恒,于是自然地我们要给每个点搞一个势能出来 然后把势能的表达式写出来就可 ...
- MAC软连接
在mac上不设置环境变量有的时候也可以直接就访问到了某些文件.这个是为什么呢?答案是用了软连接. 1 查看加载文件 可以使用cat命令查看paths文件 cat etc/paths /usr/loca ...
- 禁止tomcat扫描jar包的tld文件
禁止tomcat扫描jar包的tld文件tomcat/conf/logging.properties 取消注释org.apache.jasper.compiler.TldLocationsCache. ...
- Phoenix与Squirrel 是什么?
不多说,直接上干货! 前言 Phoenix是HBase的开源SQL引擎. squirrel是windows上Phoneix可视化工具. Phoenix的官网 http://phoenix.apach ...
- struts2结果处理、获取参数(二)
结果处理 1.转发 type可以不写,默认就是转发 <package name="hello" namespace="/hello" extends=&q ...
- Java中Ajaxa中文乱码问题
客户端: url='test/queryList?itemName='+itemName; //如果只转一次url=encodeURI(toUrl); 到后台时:乱码 servlet里dec ...