题目链接 GukiZ and GukiZiana

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

$n <= 500000, q <= 50000$

我一开始的反应是线段树,然后发现自己完全想错了……

这道题时限$10$秒,但也很容易超时。我后来是用分块过的。

把序列分成$\sqrt{n}$个块,每个块的大小为$\sqrt{n}$(最后一个块可能因为不能整除的关系可能会小一些)

每个块维护一个值$delta[i]$,表示这块的每一个数值都要加上这个值。

第1种操作的时候,找到$l$和$r$所在的块。

这两个块之间(不包含$l$所在的块和$r$所在的块,如果没有就不修改)的所有块的$delta$都加上$x$

这样就降低了修改的时间复杂度

$l$所在的块中的元素依次遍历,若下标满足$l <= i <= r$,则值加$x$

$r$所在的块中的元素依次遍历,若下标满足$l <= i <= r$,则值加$x$

每个块内按照值升序排序(第二关键字为下标)

当一个块的整体大小顺序可能发生改变时,就对这个块内部$sort$一遍,当然没必要$sort$的时候不要$sort$

不然可能$TLE$

查询的时候对每个块二分查找,找到值为$x$的元素的下标,并实时更新答案。

时间复杂度$O(q\sqrt{n}log(\sqrt{n}))$

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i)
#define fi first
#define se second typedef long long LL; const int N = 500010;
const int Q = 810; int block_size, block_num, n, q, c[N], cnt, et, op, l, r;
LL a[N], delta[N], x;
vector <pair<LL, int> > block[Q]; void update(int l, int r, LL x){ rep(i, c[l] + 1, c[r] - 1)
delta[i] += x; for (auto &node : block[c[l]])
if (node.se >= l && node.se <= r)
node.fi += x; sort(block[c[l]].begin(), block[c[l]].end()); if (c[r] > c[l]){
for (auto &node : block[c[r]])
if (node.se >= l && node.se <= r)
node.fi += x; sort(block[c[r]].begin(), block[c[r]].end()); } } void query(LL x){
int L = 1 << 30, R = -1;
rep(i, 1, block_num){
auto it = lower_bound(block[i].begin(), block[i].end(), make_pair(x - delta[i], 0));
if (it != block[i].end() && it -> first == x - delta[i])
L = min(L, it -> se); it = lower_bound(block[i].begin(), block[i].end(), make_pair(x - delta[i] + 1, 0));
if (it != block[i].begin()){
--it;
if (it -> fi == x - delta[i])
R = max(R, it -> se);
}
} if (~R) printf("%d\n", R - L);
else puts("-1");
} int main(){ scanf("%d%d", &n, &q);
rep(i, 1, n) scanf("%lld", a + i);
block_size = sqrt(n + 0.5); block_num = n / block_size;
if (n % block_size) ++block_num; cnt = 1;
rep(i, 1, n){
++et;
c[i] = cnt;
block[cnt].push_back({a[i], i});
if (et == block_size){
et = 0;
++cnt;
} } rep(i, 1, block_num) sort(block[i].begin(), block[i].end()); for (; q--; ){
scanf("%d", &op);
if (op == 1){
scanf("%d%d%lld", &l, &r, &x);
update(l, r, x);
} else{
scanf("%lld", &x);
query(x);
}
} return 0;
}

Codeforces 551E GukiZ and GukiZiana(分块思想)的更多相关文章

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

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

  2. CodeForces 551E GukiZ and GukiZiana

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

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

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

  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 307 div2 E.GukiZ and GukiZiana 分块

    time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...

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

  7. Codeforces 551 E - GukiZ and GukiZiana

    E - GukiZ and GukiZiana 思路:分块, 块内二分 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC ...

  8. Codeforces Round #319 (Div. 1)C. Points on Plane 分块思想

                                                                              C. Points on Plane On a pl ...

  9. [codeforces551E]GukiZ and GukiZiana

    [codeforces551E]GukiZ and GukiZiana 试题描述 Professor GukiZ was playing with arrays again and accidenta ...

随机推荐

  1. spartan6不能直接把时钟连到IO上

    1.问题的提出:spartan6中不允许时钟信号直接连到IO口上面? 2.解决办法: ODDR2的使用 ODDR2Primitive: Double Data Rate Output D Flip-F ...

  2. 【结构型模式】《大话设计模式》——读后感 (12)在NBA我需要翻译?——适配器模式

    适配器模式:将一个类的接口转换成客户希望的另外一个接口,Adapter模式使得原本由于接口不兼容而不能在一起工作的 那些类可以在一起工作了[DP] UML类图: 简单模拟一下代码: //已存在的.具有 ...

  3. HUD:4405-Aeroplane chess(期望飞行棋)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...

  4. Leetcode 559. N叉树的最大深度

    题目链接 https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/description/ 题目描述 给定一个N叉树,找到其最大深度. ...

  5. 对java多线程的一些浅浅的理解

    作为一名JAVA初学者,前几天刚刚接触多线程这个东西,有了些微微的理解想写下来(不对的地方请多多包涵并指教哈). 多线程怎么写代码就不说了,一搜一大堆.说说多线程我认为最难搞的地方,就是来回释放锁以及 ...

  6. 搭建基于金山快盘的Git服务器

    最近迷上了Git,这货堪称神器,用了它就再也不想用其他VCS了,就像上了高速就不想再走国道一样. 一般人使用Git+Github来搭建进行本地远程交互,不过Github弄个私人仓库是要刀乐思的,如果你 ...

  7. 取出列表中第N大的数

    array=list(range(10)) random.shuffle(array) print(array) def func1(array,n): d,k={},n while k>0: ...

  8. Facebook App 的头文件会有更多的收获

    最近在看一些 App 架构相关的文章,也看了 Facebook 分享的两个不同时期的架构(2013 和 2014),于是就想一窥 Facebook App 的头文件,看看会不会有更多的收获,确实有,还 ...

  9. rsa Round #71 (Div. 2 only)

    Replace A Time limit: 1000 msMemory limit: 256 MB   You are given a string SS containing only letter ...

  10. 关于iOS 7的几个开源项目

    MBSwitch   MBSwitch是一个体现了iOS 7扁平化设计风格的UISwitch,支持iOS 7以下系统.允许使用者进行颜色的深度自定义,你可以定义边框的颜色,开/关的颜色以及按钮的颜色. ...