题目链接: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 的更优解,但莫队的魅力就是这样的,看似暴力,其实处处充满着思考,令人喜爱。无论是常用的值域分块平衡修改与查询的复杂度,亦或者二次莫队离线的巧妙计算,都是充满着各种各样的探索与思考。最后拍了几波大数据和暴力对拍,过的时候还是很激动的。至此记录下思考心得。

参照代码
#include <bits/stdc++.h>

// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") // #define isPbdsFile #ifdef isPbdsFile #include <bits/extc++.h> #else #include <ext/pb_ds/priority_queue.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/trie_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#include <ext/pb_ds/list_update_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/exception.hpp>
#include <ext/rope> #endif using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tii;
typedef tuple<ll, ll, ll> tll;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef __int128 i128;
#define hash1 unordered_map
#define hash2 gp_hash_table
#define hash3 cc_hash_table
#define stdHeap std::priority_queue
#define pbdsHeap __gnu_pbds::priority_queue
#define sortArr(a, n) sort(a+1,a+n+1)
#define all(v) v.begin(),v.end()
#define yes cout<<"YES"
#define no cout<<"NO"
#define Spider ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
#define MyFile freopen("..\\input.txt", "r", stdin),freopen("..\\output.txt", "w", stdout);
#define forn(i, a, b) for(int i = a; i <= b; i++)
#define forv(i, a, b) for(int i=a;i>=b;i--)
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define endl '\n'
//用于Miller-Rabin
[[maybe_unused]] static int Prime_Number[13] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}; template <typename T>
int disc(T* a, int n)
{
return unique(a + 1, a + n + 1) - (a + 1);
} template <typename T>
T lowBit(T x)
{
return x & -x;
} template <typename T>
T Rand(T l, T r)
{
static mt19937 Rand(time(nullptr));
uniform_int_distribution<T> dis(l, r);
return dis(Rand);
} template <typename T1, typename T2>
T1 modt(T1 a, T2 b)
{
return (a % b + b) % b;
} template <typename T1, typename T2, typename T3>
T1 qPow(T1 a, T2 b, T3 c)
{
a %= c;
T1 ans = 1;
for (; b; b >>= 1, (a *= a) %= c)if (b & 1)(ans *= a) %= c;
return modt(ans, c);
} template <typename T>
void read(T& x)
{
x = 0;
T sign = 1;
char ch = getchar();
while (!isdigit(ch))
{
if (ch == '-')sign = -1;
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 3) + (x << 1) + (ch ^ 48);
ch = getchar();
}
x *= sign;
} template <typename T, typename... U>
void read(T& x, U&... y)
{
read(x);
read(y...);
} template <typename T>
void write(T x)
{
if (typeid(x) == typeid(char))return;
if (x < 0)x = -x, putchar('-');
if (x > 9)write(x / 10);
putchar(x % 10 ^ 48);
} template <typename C, typename T, typename... U>
void write(C c, T x, U... y)
{
write(x), putchar(c);
write(c, y...);
} template <typename T11, typename T22, typename T33>
struct T3
{
T11 one;
T22 tow;
T33 three; bool operator<(const T3 other) const
{
if (one == other.one)
{
if (tow == other.tow)return three < other.three;
return tow < other.tow;
}
return one < other.one;
} T3() { one = tow = three = 0; } T3(T11 one, T22 tow, T33 three) : one(one), tow(tow), three(three)
{
}
}; template <typename T1, typename T2>
void uMax(T1& x, T2 y)
{
if (x < y)x = y;
} template <typename T1, typename T2>
void uMin(T1& x, T2 y)
{
if (x > y)x = y;
} constexpr int N = 3e5 + 10;
int pre[N], nxt[N]; //链表前后
int pos[N]; //序列分块 struct Mo
{
int l, r, id; bool operator<(const Mo& other) const
{
return pos[l] ^ pos[other.l] ? l < other.l : r < other.r;
}
} node[N]; constexpr int INF = 1e9 + 7;
int curr = INF;
pii a[N];
int val[N], idx[N], mp[N]; //排序以后的值、每个点对应排序后的编号、每个排序后的编号对应的原下标
int split; //分割点
//删除
inline void del(const int id)
{
const int prePos = pre[id];
const int nxtPos = nxt[id];
nxt[prePos] = nxtPos;
pre[nxtPos] = prePos;
if (mp[nxtPos] > split and mp[prePos] > split) uMin(curr, val[nxtPos] - val[prePos]);
} //增加,只考虑分割点右侧不需要回滚的贡献
inline void add(const int id)
{
const int prePos = pre[id];
const int nxtPos = nxt[id];
pre[nxtPos] = id;
nxt[prePos] = id;
if (mp[id] <= split)return;
if (mp[prePos] > split)uMin(curr, val[id] - val[prePos]);
if (mp[nxtPos] > split)uMin(curr, val[nxtPos] - val[id]);
} int n, q;
int ans[N];
int siz;
int tmpVal[N], tmpCnt;
int old[N];
//计数
inline int cale(const int id)
{
const int prePos = pre[id];
const int nxtPos = nxt[id];
int res = INF;
if (nxtPos)uMin(res, val[nxtPos] - val[id]);
if (prePos)uMin(res, val[id] - val[prePos]);
return res;
} inline void solve()
{
cin >> n;
siz = sqrt(n);
forn(i, 1, n)cin >> a[i].first, old[i] = a[i].first, a[i].second = i, pos[i] = (i - 1) / siz + 1;
sortArr(a, n);
forn(i, 1, n)idx[a[i].second] = i, mp[i] = a[i].second, val[i] = a[i].first;
cin >> q;
forn(i, 1, q)
{
auto& [l,r,id] = node[i];
cin >> l >> r, id = i;
}
sortArr(node, q);
int l = 1, r = n, last = 0;
forn(i, 1, n)pre[i] = i - 1, nxt[i] = i + 1;
nxt[n] = 0;
forn(i, 1, q)
{
auto [L,R,id] = node[i];
if (pos[L] == pos[R])
{
tmpCnt = 0;
forn(i, L, R)tmpVal[++tmpCnt] = old[i];
sortArr(tmpVal, tmpCnt);
int res = INF;
forn(i, 1, tmpCnt-1)uMin(res, tmpVal[i + 1] - tmpVal[i]);
ans[id] = res;
continue;
}
if (pos[L] != last)
{
const int start = (pos[L] - 1) * siz + 1;
const int end = min(pos[L] * siz, n);
//回滚莫维护链表形状的精髓之章,建议反复体会回滚莫的互逆核心
while (r < n)add(idx[++r]);
while (l < start)del(idx[l++]);
while (r > end)del(idx[r--]);
curr = INF;
last = pos[L];
split = end;
}
while (r < R)add(idx[++r]);
//需要回滚
int tmpL = l, preCurr = curr;
while (tmpL < L)del(idx[tmpL++]);
int res = curr;
forn(i, L, split)uMin(res, cale(idx[i])); //暴力再统计左半部分对全局的情况
ans[id] = res;
while (tmpL > l)add(idx[--tmpL]); //回滚
curr = preCurr;
}
forn(i, 1, q)cout << ans[i] << endl;
} signed int main()
{
// MyFile
Spider
//------------------------------------------------------
// clock_t start = clock();
int test = 1;
// read(test);
// cin >> test;
forn(i, 1, test)solve();
// while (cin >> n, n)solve();
// while (cin >> test)solve();
// clock_t end = clock();
// cerr << "time = " << double(end - start) / CLOCKS_PER_SEC << "s" << endl;
}
\[
时间复杂度最坏为:\ 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. Codeforces 1312B Bogosort (逆序证明)

    Example input 3 1 7 4 1 1 3 5 6 3 2 1 5 6 4 output 7 1 5 1 3 2 4 6 1 3 5 看题的时候发现和sort有关,但一定要逆序排序 证明: ...

  2. 四、swift大对象--静态态大对象

    系列导航 一.swift对象存储环境搭建 二.swift添加存储策略 三.swift大对象--动态大对象 四.swift大对象--静态态大对象 五.java操作swift对象存储(官网样例) 六.ja ...

  3. C#设计模式16——中介者模式的写法

    是什么: 中介者模式是一种行为型设计模式,它定义了一个中介者对象来封装一系列对象之间的交互.中介者模式可以使得对象间的交互更加松耦合,避免了对象之间的直接依赖,从而使系统更加灵活.易于扩展和维护. 为 ...

  4. 斐波拉契序列的 Go 实现

    本篇文章主要介绍斐波拉契序列的 Go 语言实现. 斐波拉契序列: 前面相邻两项之后构成后一项. 1. 循环迭代 package main import "fmt" const ma ...

  5. cs 保研经验贴 | 数学试题 · 自动化所特供版

    据(2022 年我所看的)往年经验,自动化所比较重视数学. 感觉,按照自动化所的数学题库复习,就足以应付大多数夏令营的笔试面试了. 目录 高等数学 线性代数 概率论 机器学习 复变函数 其他 同站相关 ...

  6. Could not get a resource from the pool 异常定位和解决

    最近在服务中经常看到以下错误,进行下定位和问题解决分析: 2023-12-08 00:10:58.248 WARN [terra-sr-server,a9006fd27ccb81d0,a9006fd2 ...

  7. 【TouchGFX】实测工程所在路径深度不可超过10级(10级有效)

    实测环境 win10 64bit touchgfx designer 4.13.0

  8. [转帖]三篇文章了解 TiDB 技术内幕 - 说存储

    https://cn.pingcap.com/blog/tidb-internal-1 引言 数据库.操作系统和编译器并称为三大系统,可以说是整个计算机软件的基石.其中数据库更靠近应用层,是很多业务的 ...

  9. iptables 命令学习

    iptables 命令学习 摘要 Linux 早起版本使用netfilter进行数据包过滤. 最新的版本开始改用 ebpf的方式进行内核编程式的包过滤. netfilter 可以理解为内核态的一个处理 ...

  10. [转帖]总结:Tomcat的IO模型

    一.介绍 对于 linux 操作系统,IO 多路复用使用的是 epoll 方式,对于 windows 操作系统中 IO 多路复用使用的是 iocp 方式,对于 mac 操作系统 IO 多路复用使用的是 ...