您的位置:首页 > 其它

codeforces contest 551 problem E(分块)

2017-10-08 22:52 399 查看
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:

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.

Examples

Input
4 3
1 2 3 4
1 1 2 1
1 1 1 1
2 3


Output
2


Input
2 3
1 21 2 2 1
2 3
2 4


Output
0
-1


裸题

#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int N = 1e6+10;
LL a
, maxt
, kx;
int lx
, rx
, id
;
struct node
{
LL x;
int px;
bool operator <(const node &A)const
{
if(x!=A.x) return x<A.x;
return px<A.px;
}
};
vector<node>p
;
void solve(int l,int r,LL h)
{
int l1=id[l], r1=id[r];
if(l1==r1)
{
for(int i=l; i<=r; i++) a[i]+=h;
for(int i=0; i<p[l1].size(); i++)
if(p[l1][i].px>=l&&p[l1][i].px<=r) p[l1][i].x+=h;
sort(p[l1].begin(),p[l1].end());
return ;
}
for(int i=l; i<=rx[l1]; i++) a[i]+=h;
for(int i=0; i<p[l1].size(); i++)
if(p[l1][i].px>=l&&p[l1][i].px<=r) p[l1][i].x+=h;
sort(p[l1].begin(),p[l1].end());

for(int i=lx[r1]; i<=r; i++) a[i]+=h;
for(int i=0; i<p[r1].size(); i++)
if(p[r1][i].px>=l&&p[r1][i].px<=r) p[r1][i].x+=h;
sort(p[r1].begin(),p[r1].end());

for(int i=l1+1; i<r1; i++) maxt[i]+=h;
return ;
}
LL get(LL v)
{
LL l=-1,r=-1;
for(LL i=1; i<=kx; i++)
{
int pos=lower_bound(p[i].begin(),p[i].end(),node {v-maxt[i],-1})-p[i].begin();
if(p[i][pos].x!=v-maxt[i]) continue;
for(int j=pos; j<p[i].size(); j++)
{
if(p[i][j].x!=v-maxt[i]) break;
if(l==-1||l>p[i][j].px) l=p[i][j].px;
if(r==-1||r<p[i][j].px) r=p[i][j].px;
}
}
if(l==-1) return -1;
return r-l;
}

int main()
{
int n, q;
scanf("%d %d", &n, &q);
for(int i=1; i<=n; i++) scanf("%I64d", &a[i]);
LL k=(LL)sqrt(1.0*n),i;
for(i=1; i*k<n; i++)
{
lx[i]=(i-1)*k+1,rx[i]=i*k,p[i].clear();
maxt[i]=0;
for(int j=lx[i]; j<=rx[i]; j++) p[i].push_back(node {a[j],j}),id[j]=i;
sort(p[i].begin(),p[i].end());
}
lx[i]=(i-1)*k+1,rx[i]=n,p[i].clear();
maxt[i]=0,kx=i;
for(int j=lx[i]; j<=rx[i]; j++) p[i].push_back(node {a[j],j}),id[j]=i;
sort(p[i].begin(),p[i].end());
while(q--)
{
int x;
LL h;
scanf("%d", &x);
if(x==1)
{
int l, r;
scanf("%d %d %I64d", &l, &r, &h);
solve(l,r,h);
}
else
{
scanf("%I64d", &h);
printf("%I64d\n",get(h));
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: