题目链接:CF 或者 洛谷

想了很久,然后想起做过的一道题:秃子酋长,一开始以为差不多,结果写着写着就发现不对劲了。最后写出了个神仙回滚莫队解法,感觉很妙,记录下。

进入神仙分析时刻

首先,我们来考虑一个事实,加上一个数以后,如果能找到它的前后驱,那么可以立马更新最优解,这个也即是瓶颈点。因为最优的绝对值最小值,一定是排序以后相邻位置的所有数对中选取才是最优的。而回滚莫队提供了一个很好的东西:如果增加和删除是互逆的过程,那么我就可以回滚?so?我们直接只加不减回滚莫队就完了吗?

当然没那么简单,考虑一个事实,你在链表或者一个序列上加上一个数,如何能 \(O(1)\) 找到,显然这玩意没法做到。但我们发现!删除一个原有的数在链表中,可以轻松做到 \(O(1)\)。so?只删不加回滚莫队?问题来了,问的答案是 \(min(abs(a_i-a_j))\) 这玩意是删除一个数对以后没办法维护啊,只有加才可以维护。又陷入死胡同了,此时我想了很多办法甚至想要放弃回滚莫队了。但是想到了一个题:CF763E Timofey and our friends animals。其实到现在为止,我还是没有太多的思路,但这题也给了我后续一些贡献计算的启发。都属于盲人摸象。忽然想了下,只加不减是肯定得要的,因为答案只能这样构造,关键是这个链表,如果我一开始建好,然后从 \(n\) 一路删到当前查询块的右端点,然后重新当做加入,左端点当做删除一段,然后回滚,类似又加又删?我是这么感觉的。这样一来链表的形态解决了!!!

比较激动的开始码,问题来了,删除左端点后贡献该咋算,然后我想了下 \(L\) 到 \(blockR\) 的数量不超过 \(\sqrt{n}\),那我直接枚举左半边的每个链表点的前后缀作为贡献的一部分,另一部分贡献为右边的点加入时候计算。嗯,很快写好了,写了个对拍,wa 了。

检查,发现右端点可能和要删除的左端点有贡献被计算了,嗯,加个判断,写个变量 \(split\) 表示查询块的右端点,\(r\) 节点的前后缀在 \(split\) 右边的才计算答案。至此过了几个对拍以后又 wa 了。噢,看了半小时,发现还得特判当前加入的点是不是右半边的点,因为我回滚的时候也用的是一个函数,右半边的点如果和左半边相连,这也是不行的。

好了,写了半天,又 wa 了,如果同一个块,写一发暴力:\(O(\sqrt{n}\log{\sqrt{n}}+\sqrt{n})\),完全够用。感觉这个 \(\log{\sqrt{n}}\) 当 \(O(1)\) 都行,小的可怜。好了,拍过了一堆,拍一个 \(1e4\) 的试试,wa 了一两个,进入崩溃阶段。拍了十几组小数据,终于找到了一个小 wa 数据可以调的了,debug 了半小时,找到了问题,因为在左端点删除的时候,可能会伴随着原有的两个右端点的数变成相邻的了,这个时候其实也该更新右半部分的答案了。至此,解决了。

在途中,有过那么一瞬间冲动,在最后找到问题时,想要再无脑搞个链表单独维护右半边,想了大半天发现根本做不到一个较优复杂度,没办法无脑。一点也不好 “回滚”。当然肯定有用树类 ds 的更优解,但莫队的魅力就是这样的,看似暴力,其实处处充满着思考,令人喜爱。无论是常用的值域分块平衡修改与查询的复杂度,亦或者二次莫队离线的巧妙计算,都是充满着各种各样的探索与思考。最后拍了几波大数据和暴力对拍,过的时候还是很激动的。至此记录下思考心得。

参照代码
  1. #include <bits/stdc++.h>
  2. // #pragma GCC optimize(2)
  3. // #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
  4. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
  5. // #define isPbdsFile
  6. #ifdef isPbdsFile
  7. #include <bits/extc++.h>
  8. #else
  9. #include <ext/pb_ds/priority_queue.hpp>
  10. #include <ext/pb_ds/hash_policy.hpp>
  11. #include <ext/pb_ds/tree_policy.hpp>
  12. #include <ext/pb_ds/trie_policy.hpp>
  13. #include <ext/pb_ds/tag_and_trait.hpp>
  14. #include <ext/pb_ds/hash_policy.hpp>
  15. #include <ext/pb_ds/list_update_policy.hpp>
  16. #include <ext/pb_ds/assoc_container.hpp>
  17. #include <ext/pb_ds/exception.hpp>
  18. #include <ext/rope>
  19. #endif
  20. using namespace std;
  21. using namespace __gnu_cxx;
  22. using namespace __gnu_pbds;
  23. typedef long long ll;
  24. typedef long double ld;
  25. typedef pair<int, int> pii;
  26. typedef pair<ll, ll> pll;
  27. typedef tuple<int, int, int> tii;
  28. typedef tuple<ll, ll, ll> tll;
  29. typedef unsigned int ui;
  30. typedef unsigned long long ull;
  31. typedef __int128 i128;
  32. #define hash1 unordered_map
  33. #define hash2 gp_hash_table
  34. #define hash3 cc_hash_table
  35. #define stdHeap std::priority_queue
  36. #define pbdsHeap __gnu_pbds::priority_queue
  37. #define sortArr(a, n) sort(a+1,a+n+1)
  38. #define all(v) v.begin(),v.end()
  39. #define yes cout<<"YES"
  40. #define no cout<<"NO"
  41. #define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  42. #define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
  43. #define forn(i, a, b) for(int i = a; i <= b; i++)
  44. #define forv(i, a, b) for(int i=a;i>=b;i--)
  45. #define ls(x) (x<<1)
  46. #define rs(x) (x<<1|1)
  47. #define endl '\n'
  48. //用于Miller-Rabin
  49. [[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37};
  50. template <typename T>
  51. int disc(T* a, int n)
  52. {
  53. return unique(a + 1, a + n + 1) - (a + 1);
  54. }
  55. template <typename T>
  56. T lowBit(T x)
  57. {
  58. return x & -x;
  59. }
  60. template <typename T>
  61. T Rand(T l, T r)
  62. {
  63. static mt19937 Rand(time(nullptr));
  64. uniform_int_distribution<T> dis(l, r);
  65. return dis(Rand);
  66. }
  67. template <typename T1, typename T2>
  68. T1 modt(T1 a, T2 b)
  69. {
  70. return (a % b + b) % b;
  71. }
  72. template <typename T1, typename T2, typename T3>
  73. T1 qPow(T1 a, T2 b, T3 c)
  74. {
  75. a %= c;
  76. T1 ans = 1;
  77. for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
  78. return modt(ans, c);
  79. }
  80. template <typename T>
  81. void read(T& x)
  82. {
  83. x = 0;
  84. T sign = 1;
  85. char ch = getchar();
  86. while (!isdigit(ch))
  87. {
  88. if (ch == '-')sign = -1;
  89. ch = getchar();
  90. }
  91. while (isdigit(ch))
  92. {
  93. x = (x << 3) + (x << 1) + (ch ^ 48);
  94. ch = getchar();
  95. }
  96. x *= sign;
  97. }
  98. template <typename T, typename... U>
  99. void read(T& x, U&... y)
  100. {
  101. read(x);
  102. read(y...);
  103. }
  104. template <typename T>
  105. void write(T x)
  106. {
  107. if (typeid(x) == typeid(char))return;
  108. if (x < 0)x = -x, putchar('-');
  109. if (x > 9)write(x / 10);
  110. putchar(x % 10 ^ 48);
  111. }
  112. template <typename C, typename T, typename... U>
  113. void write(C c, T x, U... y)
  114. {
  115. write(x), putchar(c);
  116. write(c, y...);
  117. }
  118. template <typename T11, typename T22, typename T33>
  119. struct T3
  120. {
  121. T11 one;
  122. T22 tow;
  123. T33 three;
  124. bool operator<(const T3 other) const
  125. {
  126. if (one == other.one)
  127. {
  128. if (tow == other.tow)return three < other.three;
  129. return tow < other.tow;
  130. }
  131. return one < other.one;
  132. }
  133. T3() { one = tow = three = 0; }
  134. T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
  135. {
  136. }
  137. };
  138. template <typename T1, typename T2>
  139. void uMax(T1& x, T2 y)
  140. {
  141. if (x < y)x = y;
  142. }
  143. template <typename T1, typename T2>
  144. void uMin(T1& x, T2 y)
  145. {
  146. if (x > y)x = y;
  147. }
  148. constexpr int N = 3e5 + 10;
  149. int pre[N], nxt[N]; //链表前后
  150. int pos[N]; //序列分块
  151. struct Mo
  152. {
  153. int l, r, id;
  154. bool operator<(const Mo& other) const
  155. {
  156. return pos[l] ^ pos[other.l] ? l < other.l : r < other.r;
  157. }
  158. } node[N];
  159. constexpr int INF = 1e9 + 7;
  160. int curr = INF;
  161. pii a[N];
  162. int val[N], idx[N], mp[N]; //排序以后的值、每个点对应排序后的编号、每个排序后的编号对应的原下标
  163. int split; //分割点
  164. //删除
  165. inline void del(const int id)
  166. {
  167. const int prePos = pre[id];
  168. const int nxtPos = nxt[id];
  169. nxt[prePos] = nxtPos;
  170. pre[nxtPos] = prePos;
  171. if (mp[nxtPos] > split and mp[prePos] > split) uMin(curr, val[nxtPos] - val[prePos]);
  172. }
  173. //增加,只考虑分割点右侧不需要回滚的贡献
  174. inline void add(const int id)
  175. {
  176. const int prePos = pre[id];
  177. const int nxtPos = nxt[id];
  178. pre[nxtPos] = id;
  179. nxt[prePos] = id;
  180. if (mp[id] <= split)return;
  181. if (mp[prePos] > split)uMin(curr, val[id] - val[prePos]);
  182. if (mp[nxtPos] > split)uMin(curr, val[nxtPos] - val[id]);
  183. }
  184. int n, q;
  185. int ans[N];
  186. int siz;
  187. int tmpVal[N], tmpCnt;
  188. int old[N];
  189. //计数
  190. inline int cale(const int id)
  191. {
  192. const int prePos = pre[id];
  193. const int nxtPos = nxt[id];
  194. int res = INF;
  195. if (nxtPos)uMin(res, val[nxtPos] - val[id]);
  196. if (prePos)uMin(res, val[id] - val[prePos]);
  197. return res;
  198. }
  199. inline void solve()
  200. {
  201. cin >> n;
  202. siz = sqrt(n);
  203. forn(i, 1, n)cin >> a[i].first, old[i] = a[i].first, a[i].second = i, pos[i] = (i - 1) / siz + 1;
  204. sortArr(a, n);
  205. forn(i, 1, n)idx[a[i].second] = i, mp[i] = a[i].second, val[i] = a[i].first;
  206. cin >> q;
  207. forn(i, 1, q)
  208. {
  209. auto& [l,r,id] = node[i];
  210. cin >> l >> r, id = i;
  211. }
  212. sortArr(node, q);
  213. int l = 1, r = n, last = 0;
  214. forn(i, 1, n)pre[i] = i - 1, nxt[i] = i + 1;
  215. nxt[n] = 0;
  216. forn(i, 1, q)
  217. {
  218. auto [L,R,id] = node[i];
  219. if (pos[L] == pos[R])
  220. {
  221. tmpCnt = 0;
  222. forn(i, L, R)tmpVal[++tmpCnt] = old[i];
  223. sortArr(tmpVal, tmpCnt);
  224. int res = INF;
  225. forn(i, 1, tmpCnt-1)uMin(res, tmpVal[i + 1] - tmpVal[i]);
  226. ans[id] = res;
  227. continue;
  228. }
  229. if (pos[L] != last)
  230. {
  231. const int start = (pos[L] - 1) * siz + 1;
  232. const int end = min(pos[L] * siz, n);
  233. //回滚莫维护链表形状的精髓之章,建议反复体会回滚莫的互逆核心
  234. while (r < n)add(idx[++r]);
  235. while (l < start)del(idx[l++]);
  236. while (r > end)del(idx[r--]);
  237. curr = INF;
  238. last = pos[L];
  239. split = end;
  240. }
  241. while (r < R)add(idx[++r]);
  242. //需要回滚
  243. int tmpL = l, preCurr = curr;
  244. while (tmpL < L)del(idx[tmpL++]);
  245. int res = curr;
  246. forn(i, L, split)uMin(res, cale(idx[i])); //暴力再统计左半部分对全局的情况
  247. ans[id] = res;
  248. while (tmpL > l)add(idx[--tmpL]); //回滚
  249. curr = preCurr;
  250. }
  251. forn(i, 1, q)cout << ans[i] << endl;
  252. }
  253. signed int main()
  254. {
  255. // MyFile
  256. Spider
  257. //------------------------------------------------------
  258. // clock_t start = clock();
  259. int test = 1;
  260. // read(test);
  261. // cin >> test;
  262. forn(i, 1, test)solve();
  263. // while (cin >> n, n)solve();
  264. // while (cin >> test)solve();
  265. // clock_t end = clock();
  266. // cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
  267. }
\[
时间复杂度最坏为:\ O(q\sqrt{n}\log{\sqrt{n})}
\]

CF765F Souvenirs 题解的更多相关文章

  1. CF765F Souvenirs

    CF765F Souvenirs [CF765F]Souvenirs 主席树 - CQzhangyu - 博客园 其实不用主席树 感觉像是离线问题 但是不能支持差分.分治又处理不了 考虑按照右端点排序 ...

  2. CF765F Souvenirs 解题报告

    CF765F Souvenirs 题意翻译 给出\(n(2 \le n \le 10^5 )\) ,一个长为\(n\)的序列\(a(0 \le a_i \le 10^9 )\). 给出\(m(1\le ...

  3. CF765F Souvenirs 离线+线段树+主席树

    $ \color{#0066ff}{ 题目描述 }$ A县旁,连绵着一条长度为 n 的山脉,这条山脉由 n 座山峰组成,第 i 座山 峰的高度为 ai.作为著名的旅游县城,每天来到山脉游玩的旅客络绎不 ...

  4. 多校联训 DS 专题

    CF1039D You Are Given a Tree 容易发现,当 \(k\) 不断增大时,答案不断减小,且 \(k\) 的答案不超过 \(\lfloor\frac {n}{k}\rfloor\) ...

  5. 【CF765F】Souvenirs 主席树

    [CF765F]Souvenirs 题意:给你一个长度为n的序列{ai},有m个询问,每次询问给出l,r,问在所有$l\le x < y\le r$中,$|a_x-a_y|$的最小值是多少. $ ...

  6. 【CF765F】Souvenirs

    [CF765F]Souvenirs 题面 洛谷 题解 我们可以发现,对于某个右端点\(i\),左端点\(j\)在由\(i\rightarrow 1\)的过程中,每一段的答案是单调不增的,由这个性质,我 ...

  7. Codeforces 765F Souvenirs 线段树 + 主席树 (看题解)

    Souvenirs 我们将询问离线, 我们从左往右加元素, 如果当前的位置为 i ,用一棵线段树保存区间[x, i]的答案, 每次更新完, 遍历R位于 i 的询问更新答案. 我们先考虑最暴力的做法, ...

  8. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) F. Souvenirs 线段树套set

    F. Souvenirs 题目连接: http://codeforces.com/contest/765/problem/F Description Artsem is on vacation and ...

  9. Codeforces Round #397 题解

    Problem A. Neverending competitions 题目大意 一个团队有多个比赛,每次去比赛都会先订机票去比赛地点,然后再订机票返回.给出\(n\)个含有起止地点的购票记录(不按时 ...

  10. 【HDU 2126】Buy the souvenirs(01背包)

    When the winter holiday comes, a lot of people will have a trip. Generally, there are a lot of souve ...

随机推荐

  1. 【每日一题】20.K-th Number (二分 + 尺取)

    关于此题,我们分析一下: 一个区间第k大的数不小于x的条件是什么? 答案就是一个区间内不小于x的数的个数不小于k 那么,我们就会发现,我们其实并不需要知道每个数的值,实际上对我们有用的只有每个数与x的 ...

  2. 师爷,翻译翻译什么叫AOP

    张麻子:汤师爷,翻译翻译,什么叫AOP? 汤师爷:这还用翻译. 张麻子:我让你翻译给我听,什么叫AOP? 汤师爷:不用翻译,切面编程就是AOP啊. 黄四郎:难道你听不懂什么叫AOP? 张麻子:我就想让 ...

  3. vue学习笔记 二、环境搭建+项目创建

    系列导航 vue学习笔记 一.环境搭建 vue学习笔记 二.环境搭建+项目创建 vue学习笔记 三.文件和目录结构 vue学习笔记 四.定义组件(组件基本结构) vue学习笔记 五.创建子组件实例 v ...

  4. uni-app学习笔记

    uniapp封装请求方法(含请求期间的Loading样式) https://blog.csdn.net/weixin_43242112/article/details/108019404?utm_me ...

  5. SpringBoot AOP 记录操作日志、异常日志

    使用SpringBoot AOP 记录操作日志.异常日志 我们在做项目时经常需要对一些重要功能操作记录日志,方便以后跟踪是谁在操作此功能.在操作某些功能时也有可能会发生异常,但是每次发生异常要定位原因 ...

  6. POJ

    //poj 2080//题目大意:给定天数,从2000年1月1日经过这些天后的 年 月 日 及 星期几//代码参照大牛的写的,本人还是处于菜鸟阶段,思路很好#include<stdio.h> ...

  7. 每天学五分钟 Liunx 0000 | 存储篇:GlusterFS

    GlusterFS GlusterFS,是一个分布式文件系统,它通过 TCP/IP 或 IB(InfiniBand RDMA)网络将多个存储资源整合在一起,提供存储服务,具有很强的可扩展能力.   G ...

  8. 在线photoshop网页版工具开发

    基于javascript开发的在线ps工具,打包方式webpack 在线预览 在线ps网页版 源码地址 https://github.com/geeeeeeeek 功能介绍 在线图像编辑器允许您使用H ...

  9. 百度网盘(百度云)SVIP超级会员共享账号每日更新(2023.12.11)

    一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...

  10. JVM的GC学习

    JVM的GC学习 2023-12-28T17:20:25.182+0800: 7.363: [Full GC (Metadata GC Threshold) [PSYoungGen: 29067K-& ...