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题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...
随机推荐
- 比beanutil更加灵活的dto转换工具dozer
准确的说,是因为pojo无法一招走天下或者说内外部隔离的原因,所以有些时候,不得不在两个bean之间做copy或者转换映射.对于直接性的属性拷贝beanutil以及能够满足大部分要求,但是如果遇到字段 ...
- bzoj 3875 骑士游戏 - spfa - 动态规划
Description [故事背景] 长期的宅男生活中,JYY又挖掘出了一款RPG游戏.在这个游戏中JYY会 扮演一个英勇的骑士,用他手中的长剑去杀死入侵村庄的怪兽. [问题描述] 在这个游戏中,J ...
- python --- 09 初始函数 参数
函数 1.函数: 对代码块和功能的封装和定义 2.格式及语法 def 函数名() # 定义 函数体 函数名() # 调用 3. return ret ...
- Oracle SQL——inner jion;left join;right join的区别和使用场景
背景 在一次面试的时候,面试官让我说一下这三者的使用场景和区别,当时瞬间懵逼,哈哈.回来赶快看一看,记下来. 详解 inner join 等值查询:返回两张表中,联结字段值相等的组合记录 举例:所有学 ...
- was清除项目缓存数据
步骤 彻底清理删除后的项目缓存 删除${WAS_HOME}\AppServer\profiles\AppSrv01\config\cells\xxxxxNode01Cell/applications/ ...
- SpringBoot 解决HttpServletRequest只能读取一次
业务逻辑,通过filter读取请求的request,获取token,并将token传递后面流程使用 BodyReaderHttpServletRequestWrapper: public class ...
- ZOJ 3829 Known Notation(贪心)题解
题意:给一串字符,问你最少几步能变成后缀表达式.后缀表达式定义为,1 * 1 = 1 1 *,题目所给出的字串不带空格.你可以进行两种操作:加数字,交换任意两个字符. 思路:(不)显然,最终结果数字比 ...
- How to Install Apache Tomcat 8.5 on CentOS 7.3
How to Install Apache Tomcat 8.5 on CentOS 7.3 From: https://www.howtoforge.com/tutorial/how-to-inst ...
- asp.net mvc 加三层架构 完美搭配
http://www.hysql.org/aspnet/20180630/5712.html 先来一张项目的层级结构图: Model:模型层,主要是各种类型.枚举以及ORM框架,框架完成数据库和实体类 ...
- P4720 【模板】扩展卢卡斯
思路 扩展Lucas和Lucas定理其实没什么关系 我们要求的是这样的一个问题 \[ \left(\begin{matrix}n\\m\end{matrix}\right) mod\ P \] p不一 ...