Blog of RuSun

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

CF1436E Complicated Computations

LuoGu: CF1436E Complicated Computations

CF: E. Complicated Computations

考虑枚举 $i = 1, 2, \ldots , n$ ,考察 $i$ 是否为答案,即是否能构成 $mex$ 为 $i$ 的区间,那么这意味着不能有 $i$ 这个数,那么根据 $i$ 的位置,将序列划分为若干个片段,那么问题转化为这些区间是否包含 $[1, i - 1]$ 的所有数,即对于 $[l, r], w _ p < i, pre _ p < l$ 的数有多少个,其中 $pre _ i$ 表示和位置 $i$ 数相同的上一个位置。这是一个三维偏序问题,考虑扫描线减少一维,实际上我们枚举 $i$ 即为扫描线,每次只将 $i$ 数字加入数据结构维护。这里选择分块套树状数组完成。

查看代码
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
#include <cstdio>
#include <vector>
#define pb push_back
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);
}
const int N = 1e5 + 10, B = 350;
int n, w[N], v[N], tot, id[N], L[N], R[N], tr[B][N];
vector <int> h[N];
void add (int x, int y)
{
for (int i = x; i <= tot; i += i & -i)
for (int j = y + 1; j <= n + 1; j += j & -j)
++tr[i][j];
}
int ask (int x, int y)
{
int res = 0;
for (int i = x; i; i -= i & -i)
for (int j = y + 1; j; j -= j & -j)
res += tr[i][j];
return res;
}
int query (int l, int r, int a, int b)
{
if (l > r) return -1;
int res = 0;
if (id[l] == id[r])
for (int i = l; i <= r; ++i)
res += v[i] <= a && w[i] <= b;
else
{
for (int i = l; i <= R[id[l]]; ++i)
res += v[i] <= a && w[i] <= b;
res += ask(id[r] - 1, a) - ask(id[l], a);
for (int i = L[id[r]]; i <= r; ++i)
res += v[i] <= a && w[i] <= b;
}
return res;
}
int main ()
{
read(n);
for (int i = 1; i <= n; ++i)
id[i] = (i - 1) / B + 1;
tot = id[n];
for (int i = 1; i <= tot; ++i)
L[i] = R[i - 1] + 1, R[i] = R[i - 1] + B;
R[tot] = n;
for (int i = 1; i <= n + 1; ++i) h[i].pb(0);
for (int i = 1; i <= n; ++i)
read(w[i]), v[i] = h[w[i]].back(), h[w[i]].pb(i);
for (int i = 1; i <= n + 1; ++i) h[i].pb(n + 1);
for (int i = 1; i <= n + 1; ++i)
{
bool flag = false;
for (int j = 1; j < h[i].size(); ++j)
flag |= query(h[i][j - 1] + 1, h[i][j] - 1, h[i][j - 1], i - 1) == i - 1;
if (!flag) return write(i), 0;
for (int j = 1; j + 1 < h[i].size(); ++j)
add(id[h[i][j]], h[i][j - 1]);
}
write(n + 2);
return 0;
}