跳到正文

题解

Waking Shadows 题解

Gold14526约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年6月17日
收录
2026年6月29日
更新
2026年11月8日
篇幅
约 5 分钟
获赞
2
题目
P16998

版本

  1. v1当前

设 SL,RS_{L,R} 表示区间 L,RL,R 操作得到的集合 SS,设 ins⁡(S,x)\operatorname{ins}(S,x) 表示往集合 SS 中加入一个最小的大于等于 xx 且未出现的数之后的集合。

显然,对于集合 A,BA,B,若 A⊆BA\sube B,那么对于任意的非负整数 xx,ins⁡(A,x)⊆ins⁡(B,x)\operatorname{ins}(A,x)\sube \operatorname{ins}(B,x)。

显然,若 mex⁡(SL,R−1)<lR\operatorname{mex}(S_{L,R-1})<l_R,那么 SL,R=ins⁡(SL,R−1,0)S_{L,R}=\operatorname{ins}(S_{L,R-1},0),否则 SL,R=ins⁡(SL,R−1,rR+1)S_{L,R}=\operatorname{ins}(S_{L,R-1},r_R+1)。

引理: ∀1≤L<R≤n,SL+1,R⊆SL,R\forall 1\le L< R\le n,S_{L+1,R}\sube S_{L,R}.

证明

不妨假设 Si+1,i=∅S_{i+1,i}=\varnothing,试图证明 ∀1≤L≤R≤n,SL+1,R⊆SL,R\forall 1\le L\le R\le n,S_{L+1,R}\sube S_{L,R}。

L=RL=R 时显然成立。

归纳证明,设对于 R′<RR'< R,所有 SL+1,R′⊆SL,R′S_{L+1,R'}\sube S_{L,R'} 成立。

由于 SL+1,R−1⊆SL,R−1S_{L+1,R-1}\sube S_{L,R-1},所以 mex⁡(SL+1,R−1)≤mex⁡(SL,R−1)\operatorname{mex}(S_{L+1,R-1})\le \operatorname{mex}(S_{L,R-1})。

若 mex⁡(SL+1,R−1)<lR\operatorname{mex}(S_{L+1,R-1})<l_R 且 mex⁡(SL,R−1)<lR\operatorname{mex}(S_{L,R-1})<l_R,那么 SL+1,R=ins⁡(SL+1,R−1,0)⊆ins⁡(SL,R−1,0)=SL,RS_{L+1,R}=\operatorname{ins}(S_{L+1,R-1},0)\sube \operatorname{ins}(S_{L,R-1},0)=S_{L,R}。

mex⁡(SL+1,R−1)≥lR\operatorname{mex}(S_{L+1,R-1})\ge l_R 且 mex⁡(SL,R−1)≥lR\operatorname{mex}(S_{L,R-1})\ge l_R 的情况同理。

若 mex⁡(SL+1,R−1)<lR\operatorname{mex}(S_{L+1,R-1})<l_R 且 mex⁡(SL,R−1)≥lR\operatorname{mex}(S_{L,R-1})\ge l_R,那么 mex⁡(SL+1,R−1)∈SL,R−1\operatorname{mex}(S_{L+1,R-1})\in S_{L,R-1},所以 SL+1,R=ins⁡(SL+1,R−1,0)=SL+1,R−1∪{mex⁡(SL+1,R−1)}⊆SL,R−1⊆SL,RS_{L+1,R}=\operatorname{ins}(S_{L+1,R-1},0)=S_{L+1,R-1}\cup\{\operatorname{mex}(S_{L+1,R-1})\}\sube S_{L,R-1}\sube S_{L,R}。

于是,考虑对 RR 从左往右进行扫描线,维护 txt_x 表示最大的 LL 使得 x∈SL,Rx\in S_{L,R}。

考虑修改在 tt 上的表现,先考虑 lil_i 都等于 nn,即所有 SL,RS_{L,R} 都等于 ins⁡(SL,R−1,0)\operatorname{ins}(S_{L,R-1},0) 的情况,也就是说,要往每个集合中加入它的 mex⁡\operatorname{mex},如果 txt_x 发生变化,那么说明 xx 作为一个 mex⁡\operatorname{mex} 出现了,根据 mex⁡\operatorname{mex} 的定义,有 ∀y<x,ty>tx\forall y<x,t_y>t_x,因为 xx 必须要作为集合中最小未出现的数,并且容易发现这样的 txt_x 一定会改变。

具体的,设 tt 的所有严格前缀 min⁡\min 为 tx1,tx2,…,txkt_{x_1},t_{x_2},\ldots,t_{x_k},其中 xx 从小到大排序,那么修改完后会变成 tx1′=R,tx2′=tx1,tx3′=tx2,…,txk′=txk−1t'_{x_1}=R,t'_{x_2}=t_{x_1},t'_{x_3}=t_{x_2},\ldots,t'_{x_k}=t_{x_{k-1}}。

如果考虑 lil_i 随便取的话,那么就是相当于取出 tt 扣掉 tli∼rit_{l_i\sim r_i} 之后的所有前缀 min⁡\min 进行 shift。

可以看作是,你当前手里有一个数 now=Rnow=R,依次遍历 i=0,1,2,…,ni=0,1,2,\ldots,n,中间跳过区间 [lR,rR][l_R,r_R],如果 ti<nowt_i<now 那么就交换 tit_i 和 nownow。

如果你做过回转寿司的话,你会发现就是这个题,做法是这样的:

对 tt 进行分块,每块开一个堆维护这一块里面的元素,nownow 经过整个块,相当于将 nownow 加入堆中,然后将堆中的最小值弹出作为新的 nownow,这样整块是 O(n2log⁡nB)O(\frac{n^2\log n}{B})。

散块重构考虑块的第一个数会跟最大的 tagtag 比较,如果最大的 tagtag 比他大,那么可以把 tagtag 弹出来放到第一个数,并把第一个数加入 tagtag 贡献到第二个数,以此类推,那我们用一个大根堆维护 tagtag,这样散块暴力是 O(nBlog⁡n)O(nB\log n)。

查询的时候只需找最小的 xx 使得 tx<Lt_x<L 即可,复杂度是 O(nqB+qBlog⁡n)O(\frac{nq}{B}+qB\log n)。

不过你发现直接这样好像复杂度好像有点爆,复杂度可以平衡到 O(nnlog⁡n+qnlog⁡n)O(n\sqrt n\log n+q\sqrt n\log n) 或者 O(nnlog⁡nlog⁡n+qnlog⁡n)O(n\sqrt{n\log n}\log n+q\sqrt{n\log n}) 之类的,获得一些部分分,但是通过不了这个题。

根据数据范围猜测 qq 后面要跟一个纯净的 n\sqrt n,所以考虑优化散块重构部分。

考虑到 tagtag 的堆顶是在不断变小的,也就是说我们可以用一个指针维护当前的最大值,每次将指针不断减小直到存在对应的数即可,只需维护一个计数器记录每个数分别出现了几次,这样散块重构是 O(B)O(B) 的,复杂度可以平衡到 O(nnlog⁡n+qn)O(n\sqrt n\log n+q\sqrt n)。

精细实现可以 O(nn+qn)O(n\sqrt n+q\sqrt n),但是不确定有没有常数比较小的写法。

原文
存档评论3 条抓取于