Codeforces 551 E - GukiZ and GukiZiana
思路:分块, 块内二分
代码:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 5e5 + , M = ;
const int INF = 0x3f3f3f3f;
int bl[N], blo, n;
LL tmp[M], a[N];
vector<pii> block[M];
void reset(int x) {
block[x].clear();
for (int i = (x-)*blo + ; i <= min(x*blo, n); i++) block[x].pb({a[i], i});
sort(block[x].begin(), block[x].end());
}
void update(int l, int r, int x) {
if(bl[l] == bl[r]) {
for (int i = l; i <= r; i++) a[i] += x;
reset(bl[l]);
return ;
}
for (int i = l; i <= bl[l]*blo; i++) a[i] += x;
reset(bl[l]);
for (int i = bl[l]+; i <= bl[r]-; i++) tmp[i] += x;
for (int i = (bl[r]-)*blo+; i <= r; i++) a[i] += x;
reset(bl[r]);
} int query(int x) {
int l = -, r = -;
for (int i = ; i <= bl[n]; i++) {
LL t = x - tmp[i];
auto it = lower_bound(block[i].begin(), block[i].end(), pii{t, });
if(it != block[i].end() && (*it).fi == t) {
if(l == -) l = r = (*it).se;
else r = (*it).se;
}
it = upper_bound(block[i].begin(), block[i].end(), pii{t, INF});
it--;
if((*it).fi == t) {
if(l == -) l = r = (*it).se;
else r = (*it).se;
}
}
if(l == -) return -;
else return r - l;
}
int main() {
int q, ty, l, r, x;
scanf("%d %d", &n, &q);
blo = sqrt(n);
for (int i = ; i <= n; i++) scanf("%lld", &a[i]);
for (int i = ; i <= n; i++) bl[i] = (i-)/blo + ;
for (int i = ; i <= n; i++) block[bl[i]].pb({a[i], i});
for (int i = ; i <= bl[n]; i++) sort(block[i].begin(), block[i].end());
while(q--) {
scanf("%d", &ty);
if(ty == ) {
scanf("%d %d %d", &l, &r, &x);
update(l, r, x);
}
else {
scanf("%d", &x);
printf("%d\n", query(x));
}
}
return ;
}
Codeforces 551 E - GukiZ and GukiZiana的更多相关文章
- Codeforces 551 D. GukiZ and Binary Operations
		
\(>Codeforces \space 551 D. GukiZ and Binary Operations<\) 题目大意 :给出 \(n, \ k\) 求有多少个长度为 \(n\) ...
 - CF 551 E GukiZ and GukiZiana
		
https://codeforces.com/contest/551/problem/E 分块真强. 题意就是1.区间加,2.询问整个区间中,最远的两个x的距离. 分块,然后,每次找位子用二分找即可. ...
 - codeforces 551 C GukiZ hates Boxes
		
--睡太晚了. ..脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...
 - 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 551E GukiZ and GukiZiana
		
GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...
 - Codeforces 551E - GukiZ and GukiZiana(分块)
		
Problem E. GukiZ and GukiZiana Solution: 先分成N=sqrt(n)块,然后对这N块进行排序. 利用二分查找确定最前面和最后面的位置. #include < ...
 - 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 551E GukiZ and GukiZiana(分块思想)
		
题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...
 - CF 551E. GukiZ and GukiZiana [分块 二分]
		
GukiZ and GukiZiana 题意: 区间加 给出$y$查询$a_i=a_j=y$的$j-i$最大值 一开始以为和论文CC题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...
 
随机推荐
- python的zipfile、tarfile模块
			
zipfile.tarfile的用法 先占个位置,后续再实际操作和补充 参考 https://www.cnblogs.com/MnCu8261/p/5494807.html
 - 《2015 html5 iweb 峰会观会后感》
			
今天是2015 html5 iweb 峰会,大会在早上8:30,就开始了,没想到被坑了,看错一个字,把"国际会议中心",看成了"国家会议中心".大约过了10多 ...
 - 【题解】 P2234 [HNOI2002]营业额统计
			
平衡树板题 原题传送门 这道题要用Splay,我博客里有对Splay的详细介绍 这道题目还算比较模板的 每输入一个数,先不要插入 要求一下前驱和后继与x差的最小值并加到答案中 再把x插入平衡树 然后你 ...
 - shell脚本一键安装redis集群[最终版]
			
直接上shell了. #!/bin/bash #---------------------------------------------------------------------------- ...
 - php mysqli 的使用方法
			
原文链接:https://blog.csdn.net/solly793755670/article/details/52217456 Mysqli是php5之后才有的功能 需要修改php.ini的配置 ...
 - CS224n学习笔记(一)
			
How do we have usable meaning in a computer? Represents the words as discrete symbols, (离散型变量) Use t ...
 - Ubuntu + CUDA9 + CUDNN7 + OpenCV3.4 + contrib +CAFFE-master
			
安装ubuntu时赞美Rufus(建议ubuntu16.04.04),过程参考 https://www.cnblogs.com/willnote/p/6725594.html 安 装 前 一 定 要 ...
 - 旅行商问题【山财新生赛E】
			
链接:https://ac.nowcoder.com/acm/contest/547/E 来源:牛客网 题目描述 旅行商来到了一个新的国家,这个国家有N个城市,他们直接由N-1条道路相连接,每条道路的 ...
 - MongoDB集群配置笔记二(实战)
			
单台mongodb配置文件: dbpath=/opt/mongodb/data logpath=/opt/mongodb/logs/mongodb.log logappend=true fork=tr ...
 - EPPlus实战篇——Excel写入
			
.net core 项目 可以向excel写入任何类型(T)的数据,只要T中的field的[Display(Name = "1233", Description = "# ...