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

来源:互联网 发布:淘宝宝贝排名技巧 编辑:程序博客网 时间:2024/04/28 01:23

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 yGukiZiana(a, y) represents maximum value of j - i, such that aj = ai = y. If there is noy 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 ≤ n0 ≤ 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 test(s)
input
4 31 2 3 41 1 2 11 1 1 12 3
output
2
input
2 31 21 2 2 12 32 4
output
0-1


比较裸的分块题,开始我是分块然后用一个map套set搞的,结果超时了,这种静态的就应该用数组加二分嘛!,复杂度是q*b*log(b),b = sqrt(n),自己写的很挫,借用cf上别人的代码一下,感觉那个query写的很机智呀。

#include <bits/stdc++.h>using namespace std;typedef long long ll;vector<int> s[1000];ll add[1000], a[512346], pos[512346];ll n, q, bk, N , ti;bool cmp( int x, int y ){    if( a[x] == a[y]  ) return x < y;    return a[x] < a[y];}inline void modify( ll l, ll r, ll x ){    int k = pos[l], t = pos[r];    if( k == t ) {        for( ll i = l; i <= r; ++i )            a[i] += x;        sort( s[k].begin(), s[k].end(), cmp );        return ;    }    for( ll i = k + ( pos[l - 1] == k ); i <= t - ( pos[r + 1] == t ) ; ++i )        add[i] += x;    if( pos[l - 1] == k ) {        for( ll i = l; pos[i] == k; ++i ) {            a[i] += x;        }        sort( s[k].begin(), s[k].end(), cmp );    }    if( pos[r + 1] == t ) {        for( ll i = r; pos[i] == t; --i ) {            a[i] += x;        }        sort( s[t].begin(), s[t].end(), cmp );    }}inline ll query( ll x ){    int l = -1, r = -1, i;    for( i = 1; i <= N; ++i ) {        a[0] = x - add[i];        auto it = lower_bound( s[i].begin(), s[i].end(), 0, cmp );        if( it == s[i].end() ) continue;        if( a[*it] + add[i] == x ) {            l = *it;            break;        }    }    if( l == -1 ) return l;    for( int  j = N; j >= i; --j ) {        a[n + 1] = x - add[j];        auto it = lower_bound( s[j].begin(), s[j].end(), n + 1, cmp );        if( it == s[j].begin() ) continue;        it--;        if( a[*it] + add[j] == x ) {            r = *it;            break;        }    }    return r - l;}int main(){    ios::sync_with_stdio( 0 );    cin >> n >> q;    bk = ceil( sqrt( 1.*n ) + 0.005 );    for( int i = 1; i <= n; ++i ) {        cin >> a[i];        pos[i] = ( i - 1 ) / bk + 1;        s[pos[i]].push_back( i );    }    N = ( n - 1 ) / bk + 1;    for( int i = 1; i <= N; ++i ) {        sort( s[i].begin(), s[i].end(), cmp );    }    ll cmd, l, r, x;    for( int i = 1; i <= q; ++i ) {        cin >> cmd;        if( cmd == 1 ) {            cin >> l >> r >> x;            modify( l, r, x );        } else {            ++ti;            cin >> x;            cout << query( x ) << "\n";        }    }}


0 0
原创粉丝点击