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题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...
随机推荐
- Golang并发编程进程通信channel了解及简单使用
概念及作用 channel是一个数据类型,用于实现同步,用于两个协程之间交换数据.goroutine奉行通过通信来共享内存,而不是共享内存来通信.引用类型channel是CSP模式的具体实现,用于多个 ...
- python之udp协议的套接字
udp是无链接的,先启动哪一端都不会报错 udp服务端 1 ss = socket() #创建一个服务器的套接字 2 ss.bind() #绑定服务器套接字 3 inf_loop: #服务器无限循环 ...
- Android之扫描文件或文件夹
我们或许经常会遇到这种情况,明明保存了图片,但是当你打开图片时,却没有找到这张图片,手机重启之后才能看到.这是因为SD卡并没有重新挂载,图库也无法把这张图片加载进去,解决这个问题非常简单,只需要我们重 ...
- MongoDB ReplicaSet 集群搭建
说明 本文创建的集群的名字为test,在同一台机器上创建了三个mongo实例,端口不同即可. 安装mongodb的教程,之前总结过,请参考:CentOS安装MongoDB笔记 创建实例 # 本机默认原 ...
- List of 3rd Party .NET UI & Reporting Components
https://www.codeproject.com/Reference/788434/List-of-rd-Party-NET-UI-Reporting-Components Introducti ...
- 题解——ATCoder AtCoder Grand Contest 017 B - Moderate Differences(数学,构造)
题面 B - Moderate Differences Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Stat ...
- 论文笔记:Tracking by Natural Language Specification
Tracking by Natural Language Specification 2018-04-27 15:16:13 Paper: http://openaccess.thecvf.com/ ...
- Graph Convolutional Networks (GCNs) 简介
Graph Convolutional Networks 2018-01-16 19:35:17 this Tutorial comes from YouTube Video:https://www ...
- ZooKeeper分布式过程协同技术详解2——了解ZooKeeper
这个服务如何实现这些协作方面的原语? ZooKeeper基础
- Hyper-v虚拟机
Hyper-V1:创建和管理虚拟机 Hyper-V2:向VM增加虚拟硬盘 Hyper-V3:虚拟机的配置 使用Hyper-V创建虚拟机 Hyper-v 安装CentOS 7 (其他虚拟机一样参考)