题解:P17880 玉兰 / yulan
震惊!怎么今年你谷的 NOIP 难度好像比今年 SCP 难度小得多。
况且怎么不像往年放黑了。
题目自己看,考虑如何进行量化。
首先我们需要在线做法,其次对于
当我们只看前
且因为
由于序列由
- 若
p_k < b_i ,则显然这一段p_k 永远不变。 - 若
p_k \ge b_i ,显然这一段全部都需要加一,则p_k \xleftarrow{+} a_i 比较显然。
具体来说,先对
观察到因为
考虑合并的问题,因为两个函数都是递增的阶梯函数,于是可以直接双指针合并。
于是现在我们可以直接用线段树维护区间的分段函数,预处理时间和空间都是
在查询的时候,直接提出来
不是怎么做完了,感觉好简单啊。
继续考虑优化,如何少一个 log?
在线查询之所以是
对于分治中心
那么在任意
可是这一看上去就不行啊,猫树要求存每一个前缀和后缀的分段函数,单层空间
而我们考虑,从前缀
首先考虑
(mid,r - 1] \to (mid,r] 。设
F_{r - 1}(x) 为r - 1 的前缀分段函数,现在在后面接上一个f_r :F_r(x) = F_{r - 1}(x) + a_r[F_{r - 1}(x) \ge b_r] 因为
F_{r - 1}(x) 本身就关于x 严格递增,因此对于所有满足F_{r - 1}(x) \ge b_r 的x ,必定是一段连续的后缀[val_r,+\infty) (val_r 为满足F_{r - 1}(x) \ge b_r 的最小x )。于是我们可以观察到
F_r 仅在F_{r - 1} 中把包含val_r 的那一段劈开(增加一个断点val_r ),并且在后面打上+a_r 的加法标记。
对于
[l + 1,mid] \to [l,mid] 同理。设
G(x) 为[l + 1,mid] 的分段函数,在前面复合上f_l(x) 得到G(x + a_l[x \ge b_l]) 。注意到当
x \ge b_l 时,传给G 的自变量直接从b_l - 1 跳到了b_l + a_l 。这意味着原函数G 中定义域在[b_l,b_l + a_l) 这一段的分段点直接被跳过了(也就是被吞掉了),而原本定义域在[b_l + a_l,+\infty) 的部分,只需要把横坐标左移a_l ,函数增量再加上a_l 即可。
于是这两边都可以直接上可持久化 FHQ Treap:
- 右半边二分求出来
val_r ,把对应区间拆开后对后缀打+a_r 标记。 - 左半边直接按阈值把
[b_l,b_l + a_l) 这一段 Split 出来丢掉,在b_l 处补一个断点,再对右边打上横坐标左移a_l 、纵坐标加a_l 的标记,最后 Merge 回去就行了。
这样查询被我们弄到了
我咋这么菜,怎么把
好的接下来开始口胡。
猫树之所以有
首先进行底层分块,取块长
于是所有小块预处理的时间和空间为
并且我们考虑减少猫树层数,每隔
在每一大层中,设当前节点长度为
- 在每个儿子块内部,我们依然用上面的可持久化 FHQ Treap 维护块内的前缀和后缀,单层所有节点的这部分代价是
O(n \log n) 。 - 至于跨过多个完整儿子块的部分,我们直接把这
K 个完整儿子块看作K 个大元素,在它们之间建一棵高度为\log K = \log \log n 的微型二叉猫树,并且直接用平铺数组双指针归并来维护这棵微型猫树的前后缀! 因为双指针归并是线性的,在微型猫树的第d 层,平铺数组复制的总长度为O(\frac{K}{2^d} S) ,对\log \log n 层求和是个几何级数\sum O(\frac{K}{2^d} S) = O(K \cdot S) = O(S \log n) 。于是单层所有节点的微型猫树总代价也是O(n \log n) 了。
查询
大猫树共有
这样看起来好一点。
:::success[一个面目全非的
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdint>
using namespace std;
namespace oi {
constexpr std::size_t IOBUFFER = 1 << 18;
constexpr char endl = '\n';
class fastio {
private:
std::uint8_t __input_buffer[IOBUFFER + 64];
std::uint8_t *__input_ptr = __input_buffer;
std::uint8_t *__input_end = __input_buffer;
std::uint8_t __output_buffer[IOBUFFER];
std::uint8_t *__output_ptr = __output_buffer;
std::uint8_t *__output_end = __output_buffer + IOBUFFER;
std::FILE *__input = nullptr;
std::FILE *__output = nullptr;
[[gnu::always_inline]] inline std::size_t __refill() {
std::size_t rem = this->__input_end - this->__input_ptr;
if (rem)
std::memmove(this->__input_buffer, this->__input_ptr, rem);
std::size_t n = std::fread(this->__input_buffer + rem, sizeof(std::uint8_t), IOBUFFER, this->__input);
this->__input_ptr = this->__input_buffer;
this->__input_end = this->__input_buffer + rem + n;
*this->__input_end = '!';
return n;
}
[[gnu::always_inline]] inline std::int16_t get_char() {
if (this->__input_ptr == this->__input_end) [[unlikely]] {
if (!this->__refill()) [[unlikely]]
return -1;
}
return *this->__input_ptr++;
}
[[gnu::always_inline]] inline void put_char(std::uint8_t v) {
if (this->__output_ptr == this->__output_end) [[unlikely]]
this->flush();
*this->__output_ptr++ = v;
}
[[gnu::always_inline]] inline void __skip_empty() {
while (*this->__input_ptr <= ' ')
++this->__input_ptr;
if (this->__input_end - this->__input_ptr < 32) [[unlikely]] {
this->__refill();
while (*this->__input_ptr <= ' ') {
++this->__input_ptr;
if (this->__input_ptr == this->__input_end) [[unlikely]] {
if (!this->__refill())
return;
}
}
if (this->__input_end - this->__input_ptr < 32) [[unlikely]]
this->__refill();
}
}
template <typename T>
[[gnu::always_inline]] inline fastio &__read_unsigned(T &p) {
this->__skip_empty();
p = *this->__input_ptr++ ^ 48;
while (*this->__input_ptr >= '0')
p = p * 10 + (*this->__input_ptr++ ^ 48);
return *this;
}
template <typename T>
[[gnu::always_inline]] inline fastio &__read_signed(T &p) {
this->__skip_empty();
bool neg = false;
if (*this->__input_ptr == '-') {
neg = true;
++this->__input_ptr;
}
typename std::make_unsigned<T>::type tmp = *this->__input_ptr++ ^ 48;
while (*this->__input_ptr >= '0')
tmp = tmp * 10 + (*this->__input_ptr++ ^ 48);
p = neg ? -static_cast<T>(tmp) : static_cast<T>(tmp);
return *this;
}
template <typename T>
[[gnu::always_inline]] inline fastio &__write_unsigned(T p) {
if (this->__output_end - this->__output_ptr < 32) [[unlikely]]
this->flush();
std::uint8_t tmp[24], cnt = 0;
do {
tmp[cnt++] = (p % 10) ^ 48;
p /= 10;
} while (p);
while (cnt--)
*this->__output_ptr++ = tmp[cnt];
return *this;
}
template <typename T>
[[gnu::always_inline]] inline fastio &__write_signed(T p) {
if (p < 0) {
this->put_char('-');
return this->__write_unsigned(static_cast<typename std::make_unsigned<T>::type>(-(p + 1)) + 1);
}
return this->__write_unsigned(static_cast<typename std::make_unsigned<T>::type>(p));
}
public:
[[gnu::always_inline]] inline void flush() {
std::fwrite(this->__output_buffer, sizeof(std::uint8_t),
this->__output_ptr - this->__output_buffer, this->__output);
this->__output_ptr = this->__output_buffer;
}
fastio(const char *in, const char *out) {
this->__input = std::fopen(in, "r");
this->__output = std::fopen(out, "w");
*this->__input_end = '!';
}
fastio(std::FILE *in, std::FILE *out) {
this->__input = in;
this->__output = out;
*this->__input_end = '!';
}
fastio() : fastio(stdin, stdout) {}
fastio(const fastio &) = delete;
fastio &operator=(const fastio &) = delete;
~fastio() { this->flush(); }
fastio &operator<<(unsigned short p) { return this->__write_unsigned(p); }
fastio &operator<<(unsigned int p) { return this->__write_unsigned(p); }
fastio &operator<<(unsigned long p) { return this->__write_unsigned(p); }
fastio &operator<<(unsigned long long p) { return this->__write_unsigned(p); }
fastio &operator<<(short p) { return this->__write_signed(p); }
fastio &operator<<(int p) { return this->__write_signed(p); }
fastio &operator<<(long p) { return this->__write_signed(p); }
fastio &operator<<(long long p) { return this->__write_signed(p); }
fastio &operator<<(char p) {
this->put_char(p);
return *this;
}
fastio &operator<<(const char *p) {
if (!p)
return *this;
while (*p)
this->put_char(*p++);
return *this;
}
fastio &operator<<(const std::string &p) { return this->operator<<(p.c_str()); }
fastio &operator<<(std::nullptr_t) { return *this; }
fastio &operator>>(unsigned short &p) { return this->__read_unsigned(p); }
fastio &operator>>(unsigned int &p) { return this->__read_unsigned(p); }
fastio &operator>>(unsigned long &p) { return this->__read_unsigned(p); }
fastio &operator>>(unsigned long long &p) { return this->__read_unsigned(p); }
fastio &operator>>(short &p) { return this->__read_signed(p); }
fastio &operator>>(int &p) { return this->__read_signed(p); }
fastio &operator>>(long &p) { return this->__read_signed(p); }
fastio &operator>>(long long &p) { return this->__read_signed(p); }
fastio &operator>>(char &c) {
this->__skip_empty();
c = *this->__input_ptr++;
return *this;
}
fastio &operator>>(char *c) {
this->__skip_empty();
while (true) {
std::int16_t ch = this->get_char();
if (ch <= ' ') {
*c = '\0';
return *this;
}
*c++ = static_cast<char>(ch);
}
}
};
}
oi::fastio io;
#define int long long
const int inf = 2e18;
int TEST,typ,n,Q,a[500005],b[500005],sum[500005];
vector <int> tx[2000005],tadd[2000005];
int tmp[500005],tmp_add[500005];
void build(int rt,int l,int r){
if (l == r) return tx[rt] = {0,b[l]}, tadd[rt] = {0,a[l]}, void();
int mid = l + r >> 1;
build(rt << 1,l,mid);
build(rt << 1 | 1,mid + 1,r);
if (rt == 1) return ;
int i = 0,j = 0,szl = tx[rt << 1].size(),szr = tx[rt << 1 | 1].size();
int curx = 0,len = 0;
while (1){
while (j + 1 < szr && tx[rt << 1 | 1][j + 1] <= curx + tadd[rt << 1][i])
++j;
int curadd = tadd[rt << 1][i] + tadd[rt << 1 | 1][j];
if (!len || tmp_add[len - 1] != curadd){
tmp[len] = curx;
tmp_add[len] = curadd;
++len;
}
int nxt = min((i + 1 < szl ? tx[rt << 1][i + 1] : inf),(j + 1 < szr ? tx[rt << 1 | 1][j + 1] - tadd[rt << 1][i] : inf));
if (nxt == inf) break;
curx = nxt;
if (nxt == (i + 1 < szl ? tx[rt << 1][i + 1] : inf)) ++i;
}
tx[rt].assign(tmp,tmp + len);
tadd[rt].assign(tmp_add,tmp_add + len);
}
void qry(int rt,int L,int R,int l,int r,int &x){
if (l <= L && R <= r){
if (x >= tx[rt].back()) x += tadd[rt].back();
else{
int p = upper_bound(tx[rt].begin(),tx[rt].end(),x) - tx[rt].begin() - 1;
x += tadd[rt][p];
}
return ;
}
int mid = L + R >> 1;
if (l <= mid) qry(rt << 1,L,mid,l,r,x);
if (mid < r ) qry(rt << 1 | 1,mid + 1,R,l,r,x);
}
signed main(){
io >> TEST >> typ >> n >> Q;
for (int i = 1;i <= n;++i) io >> a[i], sum[i] += sum[i - 1] + a[i];
for (int i = 1;i <= n;++i) io >> b[i];
build(1,1,n);
int lst = 0; while (Q--){
int l,r,k; io >> l >> r >> k;
l ^= lst * typ;
r ^= lst * typ;
k ^= lst * typ;
int p = lower_bound(sum + l,sum + r + 1,sum[l - 1] + k) - sum;
int x = min(b[p],k);
if (x >= b[p]) x += sum[p] - sum[l - 1] - k;
if (p < r) qry(1,1,n,p + 1,r,x);
io << (lst = x) << '\n';
}
}
:::
:::success[一个
| 应该会放上来的,对吧? |
|---|