CF 551 E GukiZ and GukiZiana
https://codeforces.com/contest/551/problem/E
分块真强。
题意就是1、区间加,2、询问整个区间中,最远的两个x的距离。
分块,然后,每次找位子用二分找即可。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;
typedef pair<ll,int>pli;
//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'
//#define R register
#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c);
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/ const int maxn = 5e5+;
ll a[maxn];
int be[maxn];
int blo;
ll add[maxn];
vector<pli>v[maxn];
int n,m;
void rebuild(int id){
v[id].clear(); for(int i=(id-)*blo + ; i<= min(n,id*blo); i++){
a[i] += add[id];
v[id].pb(pli(a[i],i));
}
sort(v[id].begin(),v[id].end());
add[id] = ;
}
int main(){
scanf("%d%d", &n, &m);
for(int i=; i<=n; i++) scanf("%lld", &a[i]);
// blo = (int)sqrt(n);
blo = ;
for(int i=; i<=n; i++){
be[i] = (i-)/blo + ;
v[be[i]].pb(pli(a[i],i));
}
for(int i=; i<=be[n]; i++){
sort(v[i].begin(),v[i].end());
} while(m--){
int op; scanf("%d", &op);
if(op == ){
int l,r,x;
scanf("%d%d%d", &l, &r, &x);
for(int i=l; i<= min(r, be[l]*blo); i++){
a[i] += x;
}
rebuild(be[l]); if(be[l] < be[r]){
for(int i=be[l]+; i<=be[r]-; i++){
add[i] += x;
} for(int i=(be[r]-)*blo+; i<= r; i++){
a[i] += x;
}
rebuild(be[r]);
}
}
else {
int x; scanf("%d", &x);
int le=n+,ri=-;
for(int i=; i<=be[n]; i++){
int tmp = lower_bound(v[i].begin(),v[i].end(),pli(1ll*(x-add[i]),)) - v[i].begin(); if(v[i][tmp].fi == x-add[i]){
le = min(le, v[i][tmp].se);
}
tmp = upper_bound(v[i].begin(), v[i].end(), pli(1ll*(x-add[i]),n+)) - v[i].begin() - ;
if(tmp>= && v[i][tmp].fi == x-add[i]){
ri = max(ri, v[i][tmp].se);
}
}
if(le <= ri){
printf("%d\n", ri - le);
}
else puts("-1"); }
}
return ;
}
自己一开始把 块的id 和 i 搞混了...
CF 551 E GukiZ and GukiZiana的更多相关文章
- Codeforces 551 E - GukiZ and GukiZiana
E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...
- 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 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 551 D. GukiZ and Binary Operations
\(>Codeforces \space 551 D. GukiZ and Binary Operations<\) 题目大意 :给出 \(n, \ k\) 求有多少个长度为 \(n\) ...
- Codeforces 551E GukiZ and GukiZiana(分块思想)
题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...
- [codeforces551E]GukiZ and GukiZiana
[codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...
- CodeForces 551E GukiZ and GukiZiana
GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...
随机推荐
- linuk相关命令
1,Linux的每个文件一般都有三个权限 r--读,w--写,x--执行,其分别对应的数值为4,2,1. 输入ll可以查看到文件的权限. 2,给目录或文件授权 chmod 777 目录名 chmod ...
- EM算法和高斯混合模型GMM介绍
EM算法 EM算法主要用于求概率密度函数参数的最大似然估计,将问题$\arg \max _{\theta_{1}} \sum_{i=1}^{n} \ln p\left(x_{i} | \theta_{ ...
- .Net集合详解
前言 前面几篇文章讲了泛型.讲了数组,都有提到集合,这一节重点对集合进行详细解说.本文主要使用各种集合类型.以至于评估其性能,针对不同的场景选择不同的集合使用. 集合分类详解 一.列表 列表的创建 v ...
- Superset 官方入门教程中文翻译
本文翻译自 Superset 的官方文档:Toturial - Creating your first dashboard 最新版本的 Superset 界面与功能上与文档中提到的会有些许出入,以实际 ...
- Linux基础管道管理
一.I/O重定向 标准输入,标准输出,标准错误 file descriptors (FD, 文件描述符或Process I/O channels); 进程使用文件描述符来管理打开的文件 [root@l ...
- JVM系列(1)- JVM常见参数及堆内存分配
常见参数配置 基于JDK1.6 -XX:+PrintGC 每次触发GC的时候打印相关日志 -XX:+UseSerialGC 串行回收模式 -XX:+PrintGCDetails 打印更详细的GC日志 ...
- java并发编程(二十六)----ThreadLocal的使用
其实ThreadLocal很多接触过多线程的同学都可能会很陌生,他不像current包里面那些耳熟能详的api一样在我们面前经常出现,更多的他作为一个本地类出现在系统设计里面.我们可以说一下Sprin ...
- 2019牛客多校训练第三场H.Magic Line(思维)
题目传送门 大致题意: 输入测试用例个数T,输入点的个数n(n为偶数),再分别输入n个不同的点的坐标,要求输出四个整数x1,y1,x2,y2,表示有一条经过点(x1,y1),(x2,y2)的直线将该二 ...
- 洛谷 P3413 SAC#1 - 萌数
题意简述 求l~r之间存在长度至少为2的回文子串的正整数的个数 题解思路 数位DP 注意到有偶数长度的回文串必有长度为2的回文串,有奇数长度的回文串必有长度为3的回文串 所以只需判断与前一位,前两位是 ...
- 洛谷 P1120 小木棍
题意简述 给出n个数,求最小的l,使n个数可分成若干组,每组和都为l. 题解思路 暴力搜索+剪枝 代码 #include <cstdio> #include <cstdlib> ...