题解:CF2071F Towering Arrays
这是一篇可持久化平衡树题解,精细实现完全不卡常!时间不到时限的
暴力链接大脑,可持代替思考。第忘了多少期。
我不盲从,我选的是国产数据结构,\ 暴力也要更适合中国宝宝体质。
Duel 遇到了,瞬间想到,别人想完正解调完我还没调完。\/ll
考虑最基本的 DP 如何设计。
定义
转移
还得求一个逆着的 DP,称刚才的 DP 为
然后如何合并?
二分
然后直接上可持久化平衡树,因为没有重复节点,所以可以用 treap,直接写 fhq 带 insert 优化的,注意不要 pushdown,要标记永久化,非常难写但是常数奇小。
拿随机数据(或者说 test 3)跑,dfs 输出出来每一个版本的
代码只有 2.5KB。
空间
// Problem: F. Towering Arrays
// Contest: Codeforces - Codeforces Round 1007 (Div. 2)
// URL: https://codeforces.com/problemset/problem/2071/F
// Memory Limit: 1024 MB
// Time Limit: 6000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define multiple_cases(T) signed T; cin >> T; while (T--)
#define all(x) (x).begin(), (x).end()
using namespace std;
namespace my_random {
template<typename T = unsigned long long>
struct xorshift {
T seed;
unsigned char a, b, c;
xorshift(const T &_seed,
unsigned char _a = 5,
unsigned char _b = 11,
unsigned char _c = 54)
: seed(_seed),
a(_a),
b(_b),
c(_c) {
operator()();
operator()();
operator()();
operator()();
}
const T &operator()() {
return seed ^= (seed ^= (seed ^= seed << a) >> b) << c;
}
};
}
namespace fast_ios {
// #define fast_ios_buffer_enough
// #define fast_is_buffer_enough
// #define fast_os_buffer_enough
#define fast_ios_jump_invisible
// #define fast_is_jump_invisible
// #define fast_os_jump_invisible
#ifdef fast_ios_buffer_enough
#define fast_is_buffer_enough
#define fast_os_buffer_enough
#endif
#ifdef fast_ios_jump_invisible
#define fast_is_jump_invisible
#define fast_os_jump_invisible
#endif
template<typename T>
struct fast_make_unsigned : make_unsigned<T> {};
template<>
struct fast_make_unsigned<__int128> {
typedef unsigned __int128 type;
};
template<>
struct fast_make_unsigned<unsigned __int128> {
typedef unsigned __int128 type;
};
template<size_t buffer_size = 65536>
struct fast_reader_t {
// short, int, long, long long, __int128
// unsigned short, int, long, long long, __int128
// very safe if there are no overflow
size_t cursor;
unsigned char c;
unsigned char buffer[buffer_size];
inline fast_reader_t() : cursor(buffer_size), c(EOF) {}
inline unsigned char &get() {
#ifndef fast_is_buffer_enough
if (cursor < buffer_size) {
#endif
return c = buffer[cursor++];
#ifndef fast_is_buffer_enough
}
for (size_t i = fread(buffer, 1, buffer_size, stdin); i < buffer_size; i++) {
buffer[i] = EOF;
}
return c = buffer[(cursor = 0)++];
#endif
}
inline void unget() {
cursor--;
}
template<typename T>
typename enable_if<(is_integral<T>::value
&& is_unsigned<T>::value
|| is_same<T, unsigned __int128>::value)
&& !is_same<T, unsigned char>::value
, fast_reader_t&>::type
inline operator>>(T &x) {
x = 0;
while (get() < '0' || c > '9');
do {
x = (x << 3) + (x << 1) + (c ^ '0');
} while (get() >= '0' && c <= '9');
#ifndef fast_is_jump_invisible
unget();
#endif
return *this;
}
template<typename T>
typename enable_if<(is_integral<T>::value
&& is_signed<T>::value
|| is_same<T, __int128>::value)
&& !is_same<T, char>::value
, fast_reader_t&>::type
inline operator>>(T &x) {
typename fast_make_unsigned<T>::type x_ = 0;
bool sign = false;
while ((get() < '0' || c > '9') && c != '-' && c != '+');
if (c == '-') {
sign = true;
get();
} else if (c == '+') {
get();
}
do {
x_ = (x_ << 3) + (x_ << 1) + (c ^ '0');
} while (get() >= '0' && c <= '9');
if (sign && x_) {
x = -T(x_ - 1) - 1;
} else {
x = T(x_);
}
#ifndef fast_is_jump_invisible
unget();
#endif
return *this;
}
inline fast_reader_t &operator>>(char &x) {
while (!isgraph(get()));
x = c;
return *this;
}
inline fast_reader_t &operator>>(unsigned char &x) {
while (!isgraph(get()));
x = c;
return *this;
}
inline fast_reader_t &operator>>(string &x) {
x = "";
while (get() <= 32);
do {
x += c;
} while (get() > 32);
#ifndef fast_is_jump_invisible
unget();
#endif
return *this;
}
inline fast_reader_t &operator>>(char *x) {
while (get() <= 32);
do {
*(x++) = c;
} while (get() > 32);
*x = 0;
#ifndef fast_is_jump_invisible
unget();
#endif
return *this;
}
template<typename T>
typename enable_if<is_floating_point<T>::value, fast_reader_t&>::type
inline operator>>(T &x) {
x = 0;
bool sign = false;
while ((get() < '0' || c > '9') && c != '-' && c != '+' && c != '.');
if (c == '-') {
sign = true;
get();
} else if (c == '+') {
get();
} else if (c == '.') {
unget();
}
if (c != '.') do {
x = x * 10 + (c ^ '0');
} while (get() >= '0' && c <= '9');
if (c == '.') {
T f = 0.1;
while (get() >= '0' && c <= '9') {
x += (c ^ '0') * f;
f /= 10;
}
}
if (sign) {
x = -x;
}
unget();
return *this;
}
};
template<size_t buffer_size = 65536>
struct fast_writer_t {
// short, int, long, long long, __int128
// unsigned short, int, long, long long, __int128
size_t cursor;
unsigned char buffer[buffer_size];
inline fast_writer_t() : cursor(0) {}
inline ~fast_writer_t() {
if (cursor) {
flush();
}
}
inline void flush() {
__builtin_fwrite(buffer, 1, cursor, stdout);
cursor = 0;
}
inline void put(unsigned char c) {
buffer[cursor++] = c;
#ifndef fast_os_buffer_enough
if (cursor == buffer_size) {
flush();
}
#endif
}
template<typename T>
typename enable_if<(is_integral<T>::value
&& is_unsigned<T>::value
|| is_same<T, unsigned __int128>::value)
&& !is_same<unsigned char, T>::value,
fast_writer_t&>::type
inline operator<<(const T &x) {
if (x >= T(10)) {
*this << x / T(10);
}
put('0' ^ x % T(10));
return *this;
}
template<typename T>
typename enable_if<(is_integral<T>::value
&& is_signed<T>::value
|| is_same<T, __int128>::value)
&& !is_same<char, T>::value,
fast_writer_t&>::type
inline operator<<(const T &x) {
typename fast_make_unsigned<T>::type x_ = x;
if (x < T(0)) {
put('-');
x_ = 0 - x_;
}
return *this << x_;
}
inline fast_writer_t &operator<<(char x) {
put(x);
return *this;
}
inline fast_writer_t &operator<<(unsigned char x) {
put(x);
return *this;
}
inline fast_writer_t &operator<<(const string &x) {
for (string::const_iterator it = x.cbegin(); it != x.cend(); it++) {
put(*it);
}
return *this;
}
inline fast_writer_t &operator<<(const char *x) {
while (*x) {
put(*(x++));
}
return *this;
}
inline fast_writer_t &operator<<(const unsigned char *x) {
while (*x) {
put(*(x++));
}
return *this;
}
};
fast_reader_t<> ewin;
fast_writer_t<> ewout;
}
using namespace my_random;
using fast_ios::ewin;
using fast_ios::ewout;
#define cin ewin
// #define cout ewout
#define MAXN 200005
int n, m, a[MAXN];
struct node {
node *ls, *rs;
int tag, x, sz, w;
} pool[MAXN * 130], *pool_ptr;
xorshift<> rnd(89472378923879ll);
node *newnode(int x) {
*pool_ptr = {nullptr, nullptr, 0, x, 1, (int)rnd()};
return pool_ptr++;
}
node *newnode(node *t) {
*pool_ptr = *t;
return pool_ptr++;
}
node *mark(node *t, int tag) {
if (!tag) return t;
t = newnode(t);
t->tag += tag;
t->x += tag;
return t;
}
void split(node *t, int k, node *&l, node *&r, int ltag, int rtag) {
if (!t) {
l = r = nullptr;
return;
}
if (t->x < k) {
split(t->rs, k - t->tag, l, r, 0, rtag + t->tag);
t = newnode(t);
t->rs = l;
t->x += ltag;
t->tag += ltag;
t->sz = (t->ls ? t->ls->sz : 0) + 1 + (t->rs ? t->rs->sz : 0);
l = t;
} else {
split(t->ls, k - t->tag, l, r, ltag + t->tag, 0);
t = newnode(t);
t->ls = r;
t->x += rtag;
t->tag += rtag;
t->sz = (t->ls ? t->ls->sz : 0) + 1 + (t->rs ? t->rs->sz : 0);
r = t;
}
}
node *update(node *t, node *u, int tag) {
if (!t) {
u->x += tag;
return u;
}
if (u->w > t->w) {
node *L, *R;
split(t, u->x, L, R, 1, 0);
u->ls = L;
u->rs = R;
u->sz = (u->ls ? u->ls->sz : 0) + 1 + (u->rs ? u->rs->sz : 0);
u->tag += tag;
u->x += tag;
return u;
}
u->x -= t->tag;
if (u->x <= t->x - t->tag) {
t = newnode(t);
t->ls = update(t->ls, u, 0);
t->sz++;
t->tag += tag;
t->x += tag;
return t;
} else {
t = newnode(t);
t->rs = update(t->rs, u, -1);
t->tag += 1 + tag;
t->x += 1 + tag;
t->sz++;
return t;
}
}
int query(node *t, int k) {
if (!t) return 0;
if (t->x < k) {
return (t->ls ? t->ls->sz : 0) + 1 + query(t->rs, k - t->tag);
} else {
return query(t->ls, k - t->tag);
}
}
node *dpp[MAXN], *dps[MAXN];
signed main() {
multiple_cases(T) {
pool_ptr = pool;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
dpp[i] = update(dpp[i - 1], newnode(a[i]), 0);
}
dps[n + 1] = nullptr;
for (int i = n; i >= 1; i--) {
dps[i] = update(dps[i + 1], newnode(a[i]), 0);
}
int l = 1, r = *max_element(a + 1, a + n + 1);
while (l < r) {
int p = (l + r + 1) >> 1;
bool flag = 0;
for (int i = 1; i <= n; i++) {
if (a[i] >= p) {
int lr = query(dpp[i - 1], p - 1);
int rr = query(dps[i + 1], p - 1);
if (lr + rr <= m) {
flag = 1;
break;
}
}
}
if (flag) {
l = p;
} else {
r = p - 1;
}
}
ewout << l << '\n';
}
return 0;
}