震惊!怎么今年你谷的 NOIP 难度好像比今年 SCP 难度小得多。
况且怎么不像往年放黑了。
题目自己看,考虑如何进行量化。
首先我们需要在线做法,其次对于 可能高达 显然需要先来量化下来搞成单次 的。
当我们只看前 个数时 (由于操作一),接下来看到 ,若 说明第 个数加到了 前面,此时 ,否则并无影响。
且因为 总是小于 ,后面第 项跟 取 不会影响它和 的大小关系,直接拿原本的 来对比值看就行了。
由于序列由 个 拼成,通过上面的想法,考虑这样:
- 若 ,则显然这一段 永远不变。
- 若 ,显然这一段全部都需要加一,则 比较显然。
具体来说,先对 的前缀和二分找出 所属的段 (设它是第 段里的第 个数),那么初始值就是 ,先把第 段剩下的 个数手动复合掉,接下来就变成了对于 依次操作,将 ,其中 。
观察到因为 ,所以对于区间的复合函数 一定是关于 单调递增的,换句话说 , 为一个单调不减的阶梯函数,至多 个台阶。
考虑合并的问题,因为两个函数都是递增的阶梯函数,于是可以直接双指针合并。
于是现在我们可以直接用线段树维护区间的分段函数,预处理时间和空间都是 。
在查询的时候,直接提出来 个区间,因为我们仅关注一个点的值,直接在上面二分就行了,时间复杂度为 ,可以通过这个题。
不是怎么做完了,感觉好简单啊。
继续考虑优化,如何少一个 log?
在线查询之所以是 ,在于把区间拆分成了 个节点,每个节点二分又需要 ,于是考虑猫树。
对于分治中心 ,预处理出左半边所有后缀 的分段函数,以及右半边所有前缀 的分段函数。
那么在任意 的查询,只需要查 就可以变成 了。
可是这一看上去就不行啊,猫树要求存每一个前缀和后缀的分段函数,单层空间 就炸掉了。
而我们考虑,从前缀 到前缀 ,分段函数真的变化很大吗?
首先考虑 。
设 为 的前缀分段函数,现在在后面接上一个 :
因为 本身就关于 严格递增,因此对于所有满足 的 ,必定是一段连续的后缀 ( 为满足 的最小 )。
于是我们可以观察到 仅在 中把包含 的那一段劈开(增加一个断点 ),并且在后面打上 的加法标记。
对于 同理。
设 为 的分段函数,在前面复合上 得到 。
注意到当 时,传给 的自变量直接从 跳到了 。这意味着原函数 中定义域在 这一段的分段点直接被跳过了(也就是被吞掉了),而原本定义域在 的部分,只需要把横坐标左移 ,函数增量再加上 即可。
于是这两边都可以直接上可持久化 FHQ Treap:
- 右半边二分求出来 ,把对应区间拆开后对后缀打 标记。
- 左半边直接按阈值把 这一段 Split 出来丢掉,在 处补一个断点,再对右边打上横坐标左移 、纵坐标加 的标记,最后 Merge 回去就行了。
这样查询被我们弄到了 !但是预处理和空间都变成了 。
我咋这么菜,怎么把 搞到了 那里去了。
好的接下来开始口胡。
猫树之所以有 的预处理,因为分治树有 层,可持久化平衡树单次操作也要 。而注意到如果我们不用平衡树,直接拿 vector 平铺数组做双指针合并,是纯线性的(没有 )。
首先进行底层分块,取块长 ,序列分成 块,在块内建一棵原本 做法的线段树。
于是所有小块预处理的时间和空间为 ,块内查询也是 。
并且我们考虑减少猫树层数,每隔 层才建一层大猫树(即把猫树改成 叉树),这样大猫树的层数就降为了 。
在每一大层中,设当前节点长度为 ,它会被分成 个儿子块:
- 在每个儿子块内部,我们依然用上面的可持久化 FHQ Treap 维护块内的前缀和后缀,单层所有节点的这部分代价是 。
- 至于跨过多个完整儿子块的部分,我们直接把这 个完整儿子块看作 个大元素,在它们之间建一棵高度为 的微型二叉猫树,并且直接用平铺数组双指针归并来维护这棵微型猫树的前后缀! 因为双指针归并是线性的,在微型猫树的第 层,平铺数组复制的总长度为 ,对 层求和是个几何级数 。于是单层所有节点的微型猫树总代价也是 了。
查询 的时候,定位到它所在的大猫树层级(设 在第 个儿子, 在第 个儿子),只需要把区间拆成至多 段:第 个儿子的后缀(查平衡树)、中间第 个完整儿子块(在微型猫树上拆成 个平铺数组直接二分)、第 个儿子的前缀(查平衡树),单次查询依然是 。
大猫树共有 层,每层时空都是 ,于是这样总空间变成了 ,总时间变成了 。
这样看起来好一点。
一个面目全非的 的代码
#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';
}
}一个 的代码
看心情写吧。
应该会放上来的,对吧?