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. Web---图片验证码生成教程详解-从简单到复杂-从本地到前后台

    首先,我们先来看本地如何生成图片验证码的,再来写输出到网页的验证码如何实现. 先来看最简单的-实现的功能是,将一个字符串变成图片写入到文件中 实现代码: package cn.hncu.img; im ...

  2. HTML5 Canvas核心技术—图形、动画与游戏开发.pdf2

    事件处理: HTML5应用程序是以事件来驱动的,可以在canvas中增加一个事件监听器,当事件发生时,浏览器就会调用这个监听器 //方法一canvas.onmousedown=function(e){ ...

  3. Dot Net设计模式—MVC模式

    1 MVC设计模式简介 MVC结构是为那些需要为同样的数据提供多个视图的应用程序而设计的,它很好的实现了数据层与表示层的分离.MVC作为一种开发模型,通常用于分布式应用系统的设计和分析中,以及用于确定 ...

  4. 为 Joomla 而生的 Kunena 论坛安装手册

    查看并下载最新版本Kunena论坛,或直接下载Kunena1.7.2.如果你打算在Joomla网站中安装一个论坛,可以考虑一下Kunena论坛组件,一个为Joomla而生的免费的开源论坛组件. 安装: ...

  5. Jsp_demo:自定义标签

    Jsp自定义标签: 1.继承SimpleTagSupport,重写doTag(). 2.在WEB-INF/ 下配置**.tld文件 3.Jsp页面引入自定义标签:<%@ taglib uri=& ...

  6. weekend110(Hadoop)的 第二天笔记

    (2015年1月11日) 课程目录 01-NN元数据管理机制 02-NN工作机制2 03-DN工作原理 04-HDFS的JAVA客户端编写 05-filesystem设计思想总结 06-hadoop中 ...

  7. PHP检查表单提交是否来自于本站(验证HTTP_REFERER等)

    方法一: 你可以把处理提交数据的代码写到一个单独的文件里,比如form.php.      <?php      if   (defined(’INSIDE’))   {//判断是否有定义INS ...

  8. mysql常用的一些命令,用于查看数据库、表、字段编码

    1.查看数据库支持的所有字符集         show character set;或show char set; 2.查看当前状态 里面包括当然的字符集设置         status或者\s ...

  9. JAVA基础英语单词表(上)

    action                            / 'ækʃən /          动作,行为 active                           / 'ækti ...

  10. sql server和my sql 命令(语句)的差别,sql server与mysql的比較

    sql与mysql的比較 1.连接字符串 sql  :Initial Catalog(database)=x;  --数据库名称       Data Source(source)=x;        ...