99 · K087 FHQ-Treap 与分裂合并
105 分钟。先验证分裂的两半,再验证合并的前提。随机优先级只决定形状,不能替代 BST 的键序,也不能许诺单次最坏 O(log n)。
1. 先修自查
同一棵 Treap 同时维护两条约束:键按 BST 排,优先级按堆排。它的中序按键而非按优先级。回第 98 节区分 cnt(本键份数)与 size(整子树元素数)。
2. 切分后再拼回
split(u,x,false) 得到所有键 <x、所有键 ≥x;true 则切成 ≤x、>x。若根属于左半,只需继续把右子树切开,把其左半接回根右侧;反之切左子树。merge(A,B) 的前提是 A 的全部键都小于 B 的全部键,选优先级较大的根,再递归拼邻近的子树,最后 pull 重算大小。
用两次分裂隔离键 x,得到 <x、=x、>x。相同值压在一个节点 cnt 里。插入使 cnt 加 1 或建新节点;删除 cnt 减 1,为 0 才丢节点。查询可以只读,不一定要拆树。用 </≤ 两种判断避免 x−1 在最小整数处溢出。
3. 跟做:只删重复值的一份
当前多重集 [1,3,3,7],按 x=3 分三段:[1]、[3,3]、[7]。删一次 3,让中段 cnt 从 2 变 1,再合并为 [1,3,7]。网页演示同一组段;纸上把两侧暂时圈开,再问「中间段变空时合并哪两段」。
4. 改一条规则:查询不该悄悄改变集合
闭区间 [l,r] 的元素数是 count(≤r)−count(<l),读两条路径即可。若选择用 split 查,也必须 merge 回原有所有区段并更新根;否则连续两次同样的查询就可能得出不同结果。merge 两棵键值交错的树没有定义,不能拿它当任意集合并集。
5. 独立过关:可重集合的区间频次
首行 q,1≤q≤100000。1 x 插入一份;2 x 删一份(没有则忽略);3 l r 统计闭区间内份数;4 k 查询第 k 小(不存在输出 NONE)。x、l、r 为有符号 64 位整数,l≤r。样例 8 / 1 1 / 1 3 / 1 3 / 1 7 / 3 2 3 / 2 3 / 3 2 3 / 4 2,输出 2、1、3。
6. 提示、程序与错因
展开:先保存分裂返回的两个新根
随机优先级独立于键顺序时,期望高度 O(log(q+1)),单次期望 O(log(q+1)),最坏仍可线性。样例用固定种子便于复现,不声称能抵抗特意针对优先级安排的输入。空间 O(q),删除节点不复用槽。连续查询、清空中段、在最小 64 位键上切割都应通过。
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include <random>
using namespace std;
class FHQ {
struct Node {long long key;unsigned long long pri;int l,r,cnt,sz;};
vector<Node> t{{0,0,0,0,0,0}};
mt19937_64 gen{20261204ULL};
int rt=0;
void pull(int u) {t[u].sz=t[t[u].l].sz+t[t[u].r].sz+t[u].cnt;}
pair<int,int> split(int u,long long x,bool eq) {
if (!u) return {0,0};
if (t[u].key<x || (eq && t[u].key==x)) {
auto p=split(t[u].r,x,eq);
t[u].r=p.first;pull(u);return {u,p.second};
}
auto p=split(t[u].l,x,eq);
t[u].l=p.second;pull(u);return {p.first,u}; // 只更改跨过分界的一侧
}
int merge(int u,int v) {
if (!u || !v) return u?u:v;
if (t[u].pri>=t[v].pri) {
t[u].r=merge(t[u].r,v);pull(u);return u;
}
t[v].l=merge(u,t[v].l);pull(v);return v;
}
public:
void alter(long long x,int add) {
auto left=split(rt,x,false);
auto mid=split(left.second,x,true); // <x、=x、>x 三段
if (mid.first) {
t[mid.first].cnt+=add;pull(mid.first);
if (!t[mid.first].cnt) mid.first=0; // 每个不同键只建一个节点
} else if (add>0) {
t.push_back({x,gen(),0,0,1,1});mid.first=static_cast<int>(t.size())-1;
}
rt=merge(merge(left.first,mid.first),mid.second); // 恢复所有未修改的块
}
int size() const {return t[rt].sz;}
int count(long long x,bool eq) const {
int u=rt,ans=0;
while (u) {
if (t[u].key<x || (eq && t[u].key==x)) {
ans+=t[t[u].l].sz+t[u].cnt;u=t[u].r;
} else u=t[u].l;
}
return ans;
}
long long kth(int k) const {
int u=rt;
while (u) {
int s=t[t[u].l].sz;
if (k<=s) u=t[u].l;
else if (k<=s+t[u].cnt) return t[u].key;
else {k-=s+t[u].cnt;u=t[u].r;}
}
return 0; // 调用前校验名次
}
};
int main() {
ios::sync_with_stdio(false);cin.tie(nullptr);
int q;cin>>q;FHQ t;
while (q--) {
int op;long long x;cin>>op>>x;
if (op==1) t.alter(x,1);
else if (op==2) t.alter(x,-1);
else if (op==3) {
long long r;cin>>r;cout<<t.count(r,true)-t.count(x,false)<<'\n';
} else if (x<1 || x>t.size()) cout<<"NONE\n";
else cout<<t.kth(static_cast<int>(x))<<'\n';
}
}7. 真题解析:HNOI 2002 营业额统计 · 洛谷 P2234
n≤32767,数值可负。先在此前数值中找与 x 差最小的候选,再插入当天值;否则每次都会找到自己,结果大多误为 0。最近值只可能是严格前驱或第一个 ≥x 的数;有相等就贡献 0。第 1 天贡献是原值本身,不取绝对值。例 [5,1,2,5,4,6] 得 5+4+1+0+1+1=12。此题也可用标准库 set;这里用于实战练 split/merge 的维护。期望 O(n log(n+1)) 时间、O(n) 空间。
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#include <random>
using namespace std;
class FHQ {
struct Node {long long key;unsigned long long pri;int l,r,cnt,sz;};
vector<Node> t{{0,0,0,0,0,0}};
mt19937_64 gen{20261204ULL};
int rt=0;
void pull(int u) {t[u].sz=t[t[u].l].sz+t[t[u].r].sz+t[u].cnt;}
pair<int,int> split(int u,long long x,bool eq) {
if (!u) return {0,0};
if (t[u].key<x || (eq && t[u].key==x)) {
auto p=split(t[u].r,x,eq);
t[u].r=p.first;pull(u);return {u,p.second};
}
auto p=split(t[u].l,x,eq);
t[u].l=p.second;pull(u);return {p.first,u}; // 只更改跨过分界的一侧
}
int merge(int u,int v) {
if (!u || !v) return u?u:v;
if (t[u].pri>=t[v].pri) {
t[u].r=merge(t[u].r,v);pull(u);return u;
}
t[v].l=merge(u,t[v].l);pull(v);return v;
}
public:
void alter(long long x,int add) {
auto left=split(rt,x,false);
auto mid=split(left.second,x,true); // <x、=x、>x 三段
if (mid.first) {
t[mid.first].cnt+=add;pull(mid.first);
if (!t[mid.first].cnt) mid.first=0; // 每个不同键只建一个节点
} else if (add>0) {
t.push_back({x,gen(),0,0,1,1});mid.first=static_cast<int>(t.size())-1;
}
rt=merge(merge(left.first,mid.first),mid.second); // 恢复所有未修改的块
}
int size() const {return t[rt].sz;}
int count(long long x,bool eq) const {
int u=rt,ans=0;
while (u) {
if (t[u].key<x || (eq && t[u].key==x)) {
ans+=t[t[u].l].sz+t[u].cnt;u=t[u].r;
} else u=t[u].l;
}
return ans;
}
long long kth(int k) const {
int u=rt;
while (u) {
int s=t[t[u].l].sz;
if (k<=s) u=t[u].l;
else if (k<=s+t[u].cnt) return t[u].key;
else {k-=s+t[u].cnt;u=t[u].r;}
}
return 0; // 调用前校验名次
}
};
int main() {
ios::sync_with_stdio(false);cin.tie(nullptr);
int n;cin>>n;FHQ t;
long long x;cin>>x;long long total=x;t.alter(x,1);
for (int i=1;i<n;++i) {
cin>>x;
int k=t.count(x,false);
long long best=1LL<<60;
if (k>0) best=min(best,x-t.kth(k));
if (k<t.size()) best=min(best,t.kth(k+1)-x);
total+=best;t.alter(x,1); // 先比较以前的日期,再加入今天
}
cout<<total<<'\n';
}