Problem E. GukiZ and GukiZiana

Solution:

  先分成N=sqrt(n)块,然后对这N块进行排序。

利用二分查找确定最前面和最后面的位置。

  

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
vector<int> s[];
ll add[], a[], pos[];
ll n, q, bk, N; 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 - ] == k ); i <= t - ( pos[r + ] == t ) ; ++i )
add[i] += x; if( pos[l - ] == k ) {
for( ll i = l; pos[i] == k; ++i ) {
a[i] += x;
}
sort( s[k].begin(), s[k].end(), cmp );
} if( pos[r + ] == 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 = -, r = -, i;
for( i = ; i <= N; ++i ) {
a[] = x - add[i];
auto it = lower_bound( s[i].begin(), s[i].end(), , cmp );
if( it == s[i].end() ) continue;
if( a[*it] + add[i] == x ) {
l = *it;
break;
}
} if( l == - ) return l;
for( int j = N; j >= i; --j ) {
a[n + ] = x - add[j];
auto it = lower_bound( s[j].begin(), s[j].end(), n + , 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( );
cin >> n >> q;
bk = ceil( sqrt( .*n ) + 0.005 );
for( int i = ; i <= n; ++i ) {
cin >> a[i];
pos[i] = ( i - ) / bk + ;
s[pos[i]].push_back( i );
}
N = ( n - ) / bk + ;
for( int i = ; i <= N; ++i ) {
sort( s[i].begin(), s[i].end(), cmp );
}
ll cmd, l, r, x;
for( int i = ; i <= q; ++i ) {
cin >> cmd;
if( cmd == ) {
cin >> l >> r >> x;
modify( l, r, x );
} else {
cin >> x;
cout << query( x ) << "\n";
}
}
}

Codeforces 551E - GukiZ and GukiZiana(分块)的更多相关文章

  1. Codeforces 551E GukiZ and GukiZiana(分块思想)

    题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...

  2. CodeForces 551E GukiZ and GukiZiana

    GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...

  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 307 div2 E.GukiZ and GukiZiana 分块

    time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...

  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. Codeforces 551 E - GukiZ and GukiZiana

    E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...

  8. [codeforces551E]GukiZ and GukiZiana

    [codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...

  9. (分块)GukiZ and GukiZiana CodeForces - 551E

    题意: 给你一段序列,并且有两种操作 操作①:将序列中从l-r每个元素加上x 操作②:在序列中找到ai=aj=y,j-i的最大值,如果找不到则输出-1 思路: 直接分块暴力即可 对于区间加,普通标记加 ...

随机推荐

  1. .net常見面試題(一)

    2 .列举ASP.NET 页面之间传递值的几种方式. 答. 1).使用QueryString, 如....?id=1; response. Redirect()....                 ...

  2. LCD framebuffer驱动设计文档

    内容提要:1. android display相关的名词2. 调试LCD驱动需要注意的步骤3. 关于帧缓冲区及I/O内存---------------------------------------- ...

  3. Shell编程之函数调用

    Shell中的函数调用的使用方法见我下面的代码示例: #!/bin/bash # value init ExP="adb shell /data/local/tmp/vpxdec --yv1 ...

  4. Oracle数据库sys和system用户的默认密码及如何修改密码

    新安装好的Oracle数据库sys用户默认密码为change_on_install,system用户默认密码为manager. 以管理员身份登录的话,在命令行中输入 sqlplus sys/chang ...

  5. Java 8 简明教程

    欢迎阅读我编写的 Java 8 介绍.本教程将带领你一步步认识这门语言的所有新特性.通过简单明了的代码示例,你将会学习到如何使用默认接口方法,Lambda表达式,方法引用和可重复注解.在这篇教程的最后 ...

  6. 几款开源的图形化Redis客户端管理软件推荐

    Redis是一个超精简的基于内存的键值对数据库(key-value),一般对并发有一定要求的应用都用其储存session,乃至整个数据库.不过它公自带一个最小化的命令行式的数据库管理工具,有时侯使用起 ...

  7. sql语句书写升降序

    1, desc:降序,明天今天昨天,zyx...cba,.....321 sql语句:select * from SC_BackDiggingInfo where WriteDate<='201 ...

  8. JavaScript- 省市联动代码

    以下是JS省市联动菜单代码: 代码一: <html> <head> <title></title> <script language=" ...

  9. jdk的动态代理

    至于jdk的动态代理怎么用此处并不多说,现在是更深一步的理解,jdk的Proxy类到底做了什么. Proxy.newProxyInstance可以生成代理类,此方法有三个参数(ClassLoader ...

  10. 基于前端javascript的搜索功能

    发表于 2013/11/07 当在数据量不是很大,而且没有后端对应的功能接口的时候,一些简单的搜索功能基本上是前端去实现的,正好最近用到,写了一个,贴出来和大家分享: 功能描述: 按下键盘后及时搜索条 ...