Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4098 [HEOI2013]ALO

P4098 [HEOI2013]ALO

对于一个点,考虑其成为次大值的区间,向前找到第一个比其大的位置 $a$ ,第二个比其大的位置 $b$ ,向后找到第一个比其大的位置 $c$ ,第二个比其大的位置 $d$ 。那么区间左端点 $l \in (b, a]$ ,右端点 $r \in [c, d)$ ,那么 $[l, r]$ 都是可以和这个点贡献答案的,即 $(b, d)$ 。考虑特殊情况,如果不存在 $a$ 并且不存在 $c$ ,那么不存在区间可以贡献答案,否则如果其中一个不存在,那么就可以取到边界 $1$ 或 $n$ 。如果找到第一个比其大的位置和第二个比其大的位置,实在想不到优美的办法了,用树状数组维护两个点,表示最大值和次大值。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <cstdio>
#include <vector>
#include <algorithm>
#define pb push_back
#define fi first
#define se second
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0';
flag && (x = ~x + 1);
}
template <class Type, class ...rest>
void read (Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write (Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar('0' + x % 10);
}
typedef pair <int, int> PII;
const int N = 5e4 + 10, M = 31;
int n, m, idx, ans, w[N], rt[N], f[N], g[N];
struct Node { int s[2], v; } tr[N * (M + 1)];
namespace Init
{
int v[N];
PII tr[N];
void chkmax (PII &x, int k)
{
if (!x.fi || (k && k > x.fi)) x.se = x.fi, x.fi = k;
else if (!x.se || (k && k > x.se)) x.se = k;
}
void chkmin (PII &x, int k)
{
if (!x.fi || (k && k < x.fi)) x.se = x.fi, x.fi = k;
else if (!x.se || (k && k < x.se)) x.se = k;
}
void add (int x, int k, void (*chk)(PII &x, int k))
{
for (; x; x -= x & -x)
chk(tr[x], k);
}
int ask (int x, void (*chk)(PII &x, int k))
{
PII res { 0, 0 };
for (; x <= m; x += x & -x)
chk(res, tr[x].fi), chk(res, tr[x].se);
return res.fi ? res.se : -1;
}
void main ()
{
vector <int> ws;
for (int i = 1; i <= n; ++i) ws.pb(w[i]);
sort(ws.begin(), ws.end());
m = ws.erase(unique(ws.begin(), ws.end()), ws.end()) - ws.begin();
for (int i = 1; i <= n; ++i)
v[i] = upper_bound(ws.begin(), ws.end(), w[i]) - ws.begin();
for (int i = 1; i <= n; ++i)
f[i] = ask(v[i] + 1, chkmax), add(v[i], i, chkmax);
for (int i = 1; i <= m; ++i) tr[i] = { 0, 0 };
for (int i = n; i; --i)
g[i] = ask(v[i] + 1, chkmin), add(v[i], i, chkmin);
}
}
void insert (int &x, int k, int t = M - 1)
{
tr[++idx] = tr[x], x = idx;
++tr[x].v;
if (!~t) return;
insert(tr[x].s[k >> t & 1], k, t - 1);
}
int query (int x, int y, int k, int t = M - 1)
{
if (!~t) return 0;
int c = k >> t & 1;
if (tr[tr[y].s[c ^ 1]].v > tr[tr[x].s[c ^ 1]].v)
return query(tr[x].s[c ^ 1], tr[y].s[c ^ 1], k, t - 1) | 1 << t;
return query(tr[x].s[c], tr[y].s[c], k, t - 1);
}
int main ()
{
read(n);
for (int i = 1; i <= n; ++i)
read(w[i]), insert(rt[i] = rt[i - 1], w[i]);
Init::main();
for (int i = 1; i <= n; ++i) if (~f[i] || ~g[i])
ans = max(ans, query(rt[f[i] > 0 ? f[i] : 0], rt[g[i] > 0 ? g[i] - 1 : n], w[i]));
write(ans);
return 0;
}