Codeforces Round #307 (Div. 2)E. GukiZ and GukiZiana(分块)
2015-06-18 13:28
429 查看
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 y, GukiZiana(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:
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 test(s)
input
output
input
output
比较裸的分块题,开始我是分块然后用一个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";
}
}
}
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 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:
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 test(s)
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
比较裸的分块题,开始我是分块然后用一个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";
}
}
}
相关文章推荐
- 快速排序里的学问:从猜数字开始
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- Search Insert Position,Search for a Range,Pow(x, n),Sqrt(x)
- Find Minimum in Rotated Sorted Array II
- [LeetCode] Sqrt(x)
- [LeetCode] Pow(x, n)
- [LeetCode] Search Insert Position
- [LeetCode] Search for a Range
- [LeetCode] Search in Rotated Sorted Array
- PAT 1057 Stack (30)
- int sqrt(int x)
- Pow(x, n)
- Find Minimum in Rotated Sorted Array
- Divide Two Integers
- Leetcode:Search a 2D Matrix 菜鸟解法
- [BZOJ2038][2009国家集训队][莫队][分块]小z的袜子
- [BZOJ1257][CQOI2007]余数之和
- Codeforces538F A Heap of Heaps【分块+差分求前缀和】
- POJ2391解题报告
- poj_2002