Codeforces 551E - GukiZ and GukiZiana(分块)
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(分块)的更多相关文章
- Codeforces 551E GukiZ and GukiZiana(分块思想)
题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...
- CodeForces 551E GukiZ and GukiZiana
GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...
- CF 551E. GukiZ and GukiZiana [分块 二分]
GukiZ and GukiZiana 题意: 区间加 给出$y$查询$a_i=a_j=y$的$j-i$最大值 一开始以为和论文CC题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...
- 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 ...
- Codeforces 307 div2 E.GukiZ and GukiZiana 分块
time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...
- 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 ...
- Codeforces 551 E - GukiZ and GukiZiana
E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...
- [codeforces551E]GukiZ and GukiZiana
[codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...
- (分块)GukiZ and GukiZiana CodeForces - 551E
题意: 给你一段序列,并且有两种操作 操作①:将序列中从l-r每个元素加上x 操作②:在序列中找到ai=aj=y,j-i的最大值,如果找不到则输出-1 思路: 直接分块暴力即可 对于区间加,普通标记加 ...
随机推荐
- DevExpress的JavaScript脚本智能提示
http://www.cnblogs.com/zhaozhan/archive/2011/06/08/2075767.html ASPxScriptIntelliSense.js在安装目录下的Comp ...
- 根据地址查询经纬度.html
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title>根据地址查询经纬度</t ...
- Opencl API解释(一)
首先注明:我用的AMD的opencl,它有很多sample代码,结合代码来解释这些API Opencl 常用的API 汇总总结: 信息查询函数 1. cl_int clGetDeviceInfo(cl ...
- linux 多线程基础4
六.线程的作用域 函数pthread_attr_setscope和pthread_attr_getscope分别用来设置和得到线程的作用域,这两个函数的定义如下: 7. 名称:: pthread_at ...
- window7电脑设置好了,却无法远程?
设置远程连接: 步骤:右键[我的电脑]-->[属性] 点击[远程设置],然后设置如下: 在 cmd 中 通过 [ipconfig]命令查看IP: 以上设置好了,发现仍无法远程?解决办法如下: 电 ...
- kvm 对虚拟机里面插u盘
KVM虚拟机上关于宿主机的USB设备使用问题探究 KVM usb passthrough就是将宿主机的usb接口直接给虚拟机使用,usb接口上的设备也就直接可以在虚拟机上使用: 测试通过usb pas ...
- 35个jquery技巧[转]
人人都会的35个Jquery小技巧 2015-10-28 WEB开发者 收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发. 1. 禁止右键点击 $(document).ready(fun ...
- gwt中java与js的相互调用
1. java通过jsni调用内部js Button button = new Button("java调用内部jsni的js方法"); button.addClickHandle ...
- 【转】Cannot change version of project facet Dynamic Web Module to 3.1 (Eclipse Maven唯一解决方案)
If you want to use version 3.1 you need to use the following schema: http://xmlns.jcp.org/xml/ns/jav ...
- eclipse通过classpath variable引用类库
众所周知.eclipse的project bulid path中能够引用第三方类库(如图1). 图1 可是这样的方式有个缺点:对类库的引用是通过绝对路径.假设有两台电脑(办公室1台.家1台),非常可能 ...