跳到正文

个人记录

[OJ46698] 栈的维护

Para约 1 分钟
原文
文章信息与版本

文章信息

发布
2022年1月3日
收录
2025年12月12日
更新
2026年9月10日
篇幅
约 1 分钟

版本

  1. v1当前

比较简单的一道数据结构。

由于要修改操作,很容易想到用线段树来维护。

在每一个节点处维护它会向后面的节点贡献的个数 raddradd,它左边会删除的个数 lcutlcut,与当前位置上的答案 ansans,会发现 ansans 并不容易求解。

考虑如何求 ansans,可以得到有如下递推式 ansx=ansrson+f(lson,lcutrson)ans_x = ans_{rson} + f(lson, lcut_{rson})

f(x,y)f(x,y) 表示在 xx 处,最后还要弹出 yy 个元素所得到的答案。

如何快速求解 f(x,y)f (x, y)。

  • 当 y>=raddrsony >= radd_{rson}, f(x,y)=f(lson,y−raddrson+lcutrson)f(x,y) = f(lson, y - radd_{rson} + lcut_{rson})
  • 当 y<raddrsony < radd_{rson},f(x,y)=f(lson,lcutrson)+f(rson,y)f(x,y) = f(lson, lcut_{rson}) + f(rson,y),有之前 ansans 的递推式可得 f(lson,lcutrson)=ansx−ansrsonf(lson, lcut_{rson}) = ans_x - ans_{rson},整合之后为 f(x,y)=ansx−ansrson+f(rson,y)f(x,y) = ans_x - ans_{rson} + f(rson, y)

时间复杂度 O(nlog2n)O(nlog^2n)

原文