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 思路: 直接分块暴力即可 对于区间加,普通标记加 ...
随机推荐
- bzoj 1069 [SCOI2007]最大土地面积(旋转卡壳)
1069: [SCOI2007]最大土地面积 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2277 Solved: 853[Submit][Stat ...
- 初学python(print使用、条件分支、循环、模块引用)
import random """ #查看源代码日后爬虫用 import urllib.request # coding=utf-8 url = "http:/ ...
- Code Forces 711C Coloring Trees
C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Idea实现WebService实例 转
作者:http://blog.csdn.net/dreamfly88/article/details/52350370 因为工作需要,数据传输部分需要使用webservice实现,经过两天的研究,实现 ...
- 使用CLRMD编写一个自己的C#调试器
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:使用CLRMD编写一个自己的C#调试器.
- redis学习大全
http://blog.csdn.net/menergy/article/details/17577985 http://blog.sina.com.cn/s/blog_64008ed70102uy ...
- 关于android各种双卡手机获取imei,imsi的处理(mtk,展讯,高通等)
目前国内对于双卡智能手机的需求还是很大的,各种复杂的业务会涉及到双卡模块:而android标准的api又不提供对双卡的支持.导致国内双卡模块标准混乱,各个厂商各玩各的.目前我知道的双卡解决方案就有:m ...
- Java JDBC批处理插入数据操作(转)
在此笔记里,我们将看到我们如何可以使用像Statement和PreparedStatement JDBC API来批量在任何数据库中插入数据.此外,我们将努力探索一些场景,如在内存不足时正常运行,以及 ...
- iOS内存泄漏自动检测工具PLeakSniffer
新款objective-C内存泄漏自动检测工具 PLeakSniffer , GitHub地址 (https://github.com/music4kid/PLeakSniffer). 背景 前些天读 ...
- HTTP基础:URL格式、 HTTP请求、响应、消息
HTTP URL 格式: http://host[:port][abs_path] 其中http表示要通过HTTP协议来定位网络资源. host表示合法的Internet主机域名或IP地址(以点分十进 ...