一个数字如果所有区间都出现就不用在 数组添加,否则要添加在 数组。
一个数字 在这些区间出现过且并没有在这些区间都出现,在 数组加入 时要保证 数组内有 ,不然就不能保证所有 一样了。
所以我们只要统计在所有区间都出现过的数和在区间中出现过的数即可。
在区间中出现过的数:当给定区间 时,给它差分,最后前缀和统计,统计数组内 的数即可。
在所有区间都出现过的数:这个就没法差分了,因为一个数可能会在序列中出现多次,我们只能知道每个数在所有区间出现几次,而不能知道在多少个区间内出现,暴力统计是 ,不能接受。
考虑一个区间包含另一个区间,这个区间就没有用了,我们可以考虑删除这个区间,因为被包含内区间内的数肯定在包含它的区间里,但包含它的区间里的数不一定在被包含内区间内,所以我们要删除所有包含别的区间的区间,我们可以用单调栈来维护所有不被包含的区间。
我们可以将所有区间以 为第 优先级, 为第 优先级从小到大排序,对于每一个l相等的区间,我们只取 最小的那一个区间,并将这个序列压入栈中,如果接下来的区间 比栈内区间的 小,就把栈内区间弹出,直到栈内区间的 当前区间的 为止,最后栈内的区间就是我们想要的所有不被包含的区间。
所有不被包含的区间组成的序列可以满足 有单调性,所以我们可以用单调队列统计在所有区间都出现过的数,我们用 数组统计每个数字的出现次数, 数组统计在当间 个区间中都出现过的数,先将单调队列往右扩展,再删去左边的数,注意到只有在删数时才有可能让 为 ,所以我们只有在这个时候将 设为 ,统计完所有区间后, 为 的 就是在所有区间都出现过的数。
至此,我们已经成功统计在所有区间都出现过的数和在区间中出现过的数,复杂度 。
首先 。
操作 : 如果 在所有区间都出现过的数中出现,就将 加 ,直到不能进行为止。
操作 : 进行完操作一后,将 加 ,即视为在 数组内加入 ,并再进行操作 。
若 在在区间中出现过的数出现,说明要在 数组内加入 ,才能加入 。
若 不在在区间中出现过的数出现,说明可以在 数组内加入 ,让 。
最后输出 即可。
时间复杂度 ,跑得飞快。
代码
#include <bits/stdc++.h>
using namespace std;
int n, m, a[1000010], b[1000010], c[1000010], f[1000010], vis[1000010], dis[1000010], fb[1000010],
fc[1000010], Sort[1000010], len;
struct node {
int l, r;
} ff2[1000010];
struct edge {
int i, r;
} stk[1000010];
namespace IO {
constexpr int bufsize = 230005;
char buf[bufsize], *f1, *f2;
char gtchar() { return f1 == f2 && (f2 = buf + fread(f1 = buf, 1, bufsize, stdin)) == buf ? EOF : *f1++; }
template <typename T>
void read(T &ret) {
int f = ret = 0;
char ch = gtchar();
while (!isdigit(ch)) f = ch == '-', ch = gtchar();
while (isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = gtchar();
if (f)
ret = -ret;
}
template <typename T, typename... t>
void read(T &a, t &... b) {
read(a), read(b...);
}
} // namespace IO
using IO::read;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
read(n), read(m);
for (int i = 1; i <= n; ++i) Sort[i] = 1e9;
for (int i = 1; i <= n; ++i) read(a[i]);
for (int i = 1; i <= m; ++i) {
int l, r;
read(l), read(r);
f[l]++, f[r + 1]--;
Sort[l] = min(Sort[l], r);
}
for (int i = 1; i <= n; ++i) f[i] += f[i - 1];
for (int i = 1; i <= n; ++i) {
if (f[i] >= 1) {
c[a[i]] = 1;
}
}
for (int i = 1; i <= n; ++i) {
if (Sort[i] == 1e9)
continue;
vis[i] = 1;
while (len > 0 && stk[len].r >= Sort[i]) {
vis[stk[len].i] = 0;
len--;
}
len++;
stk[len].i = i;
stk[len].r = Sort[i];
}
int t = 0;
for (int i = 1; i <= n; ++i)
if (vis[i] == 1)
t++, ff2[t].l = i, ff2[t].r = Sort[i];
if (t >= 1) {
for (int i = ff2[1].l; i <= ff2[1].r; ++i) {
dis[a[i]]++;
b[a[i]] = 1;
}
for (int i = 2; i <= t; ++i) {
int l1 = ff2[i - 1].l, r1 = min(ff2[i - 1].r, ff2[i].l - 1);
int l2 = max(ff2[i - 1].r + 1, ff2[i].l), r2 = ff2[i].r;
for (int j = l2; j <= r2; ++j) dis[a[j]]++;
for (int j = l1; j <= r1; ++j) {
dis[a[j]]--;
if (dis[a[j]] == 0)
b[a[j]] = 0;
}
}
}
int tot1 = 0, tot2 = 0;
for (int i = 0; i <= n; ++i)
if (b[i] == 1)
fb[++tot1] = i;
for (int i = 0; i <= n; ++i)
if (c[i] == 1)
fc[++tot2] = i;
int ans = -1, flag = 0, sum = 0;
for (int i = 0, tt1 = 1, tt2 = 1; i <= n; ++i) {
if (i == 0) {
while (fb[tt1] == sum && tt1 <= tot1) sum++, tt1++, tt2++;
if (fc[tt2] == sum && tt2 <= tot2) {
cout << "-1\n";
} else
flag = 1, cout << sum << '\n', ans = sum;
continue;
}
while (fb[tt1] == sum && tt1 <= tot1) sum++, tt1++, tt2++;
if (fc[tt2] == sum && tt2 <= tot2)
tt2++;
sum++;
while (fb[tt1] == sum && tt1 <= tot1) sum++, tt1++, tt2++;
if (fc[tt2] == sum && tt2 <= tot2)
cout << ans << '\n';
else
cout << sum << '\n', ans = sum;
}
return 0;
}::::