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的更多相关文章

  1. Codeforces 551 D. GukiZ and Binary Operations

    \(>Codeforces \space 551 D. GukiZ and Binary Operations<\) 题目大意 :给出 \(n, \ k\) 求有多少个长度为 \(n\) ...

  2. CF 551 E GukiZ and GukiZiana

    https://codeforces.com/contest/551/problem/E 分块真强. 题意就是1.区间加,2.询问整个区间中,最远的两个x的距离. 分块,然后,每次找位子用二分找即可. ...

  3. codeforces 551 C GukiZ hates Boxes

    --睡太晚了. ..脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...

  4. 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 ...

  5. CodeForces 551E GukiZ and GukiZiana

    GukiZ and GukiZiana Time Limit: 10000ms Memory Limit: 262144KB This problem will be judged on CodeFo ...

  6. Codeforces 551E - GukiZ and GukiZiana(分块)

    Problem E. GukiZ and GukiZiana Solution: 先分成N=sqrt(n)块,然后对这N块进行排序. 利用二分查找确定最前面和最后面的位置. #include < ...

  7. 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 ...

  8. Codeforces 551E GukiZ and GukiZiana(分块思想)

    题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...

  9. CF 551E. GukiZ and GukiZiana [分块 二分]

    GukiZ and GukiZiana 题意: 区间加 给出$y$查询$a_i=a_j=y$的$j-i$最大值 一开始以为和论文CC题一样...然后发现他带修改并且是给定了值 这样就更简单了.... ...

随机推荐

  1. django模板-自定义标签、过滤器

    自定义标签或者过滤器的步骤 ①将要创建自定义标签或过滤器的app加入settings文件的installed_apps中 ②在app中创建templatetags目录,类型为包即packages ③在 ...

  2. 搭建一个简单的svn服务器(旧)

    cenos 6.5,svnserver 1.6.11 默认可能已经安装,没有的话就: yum install svn -ysvnserver --version 创建一个svn仓库: svnadmin ...

  3. RHEL6/7 x86_64下cachefilesd占用cpu达到100%

    昨天,有个测试环境cachedfilesd CPU 100%,一直在跑了挺久,经查 1. CacheFiles介绍NFS是一种经常使用到的网络共享文件系统,在分布式环境下,多台服务器的文件共享是一个问 ...

  4. 【题解】Luogu P4363 [九省联考2018]一双木棋chess

    原题传送门 这道题珂以轮廓线dp解决 经过推导,我们珂以发现下一行的棋子比上一行的棋子少(或等于),而且每一行中的棋子都是从左向右依次排列(从头开始,中间没有空隙) 所以每下完一步棋,棋盘的一部分是有 ...

  5. ACM札记

    1. 逗号表达式 在“计蒜客“的ACM教程中,看到这样一段很好的代码: int n; while (scanf("%d", &n), n) { //do something ...

  6. [c/c++] programming之路(10)、格式符后续

    一.格式符 1. f格式符 #include<stdio.h> #include<stdlib.h> void main(){ printf("%f",10 ...

  7. Matlab绘制三维曲面(以二维高斯函数为例)

    原文地址为:Matlab绘制三维曲面(以二维高斯函数为例) 寒假学习了一下Python下的NumPy和pymatlab,感觉不是很容易上手.来学校之后,决定继续看完数字图像处理一书.还是想按照上学期的 ...

  8. topcoder srm 712 div1

    problem1 link 将$a_{0},a_{1},...,a_{n-1}$看做$a_{0}x^{0}+a_{1}x^{1}+...+a_{n-1}x^{n-1}$.那么第一种操作相当于乘以$1+ ...

  9. 【做题】TCSRM592 Div1 500 LittleElephantAndPermutationDiv1——计数&dp

    题意:定义函数\(f(A,B) = \sum_{i=1}^n \max(A_i,B_i)\),其中\(A\)和\(B\)都是长度为\(n\)的排列.给出\(n\)和\(k\),问有多少对\((A,B) ...

  10. cannot open window service on computer '.' in window application

    1.配置错误,需要检查对应的windows service的exe文件所在文件夹下的log 2.在命令行通过Start-Service启动,需要有管理员权限.