由于题目对最终序列种类数计数,所以考虑如何刻画最终序列。
由于最终序列需要满足不存在相邻逆序对,那么其一定是一个上升子序列。
不妨考虑按照子序列 DP 的惯例,设 f_i 表示,以 i 结尾的合法最终子序列数量。
转移枚举前一个存在的点 j,那么就是需要判断:能否把 j < x < i 的元素都删除。
分析一下发现,对于一个三元组 (l, r, k),要删去 [l, r] 中 k 以外的所有元素,当且仅当 a_l \ge a_k \ge a_r。
证明:
- 必要性
- 考虑合并证明对于 l = r = x,首先有 a_x \ge a_x \ge a_x 成立。
- 然后合并两个 (l, r, x) 和 (L, R, X),若 x, X 构成逆序对,则 a_x > a_X,那么 a_l > a_x > a_X > a_R,即 a_l > a_R,对于 (l, R, x) 或 (l, R, X) 命题都成立。
- 因此可以推出所有合法 (l, r, x) 都满足 a_l \ge a_x \ge a_r。
- 充分性
- 先证明一个引理,当 a_l > a_r 时,可以仅保留下任意一个端点。
- 由于此时区间内必然存在相邻逆序对(否则区间单调递增与 a_l > a_r 矛盾)不断删去相邻逆序对,最后一定可以删完,然后此时 (l, r) 可以任意删除。
- 当 a_l > a_k > a_r,分别考虑 [l, x] 和 [x, r] 区间,运用一次上述引理即可推出最后一定可以剩下 x。
- 若不等号取等,因为 a 是排列,则 k 一定和 l / r 重叠,退化成引理形式,一定可以剩下 x。
- 先证明一个引理,当 a_l > a_r 时,可以仅保留下任意一个端点。
那么要删去 j < x < i,必然存在一个分割点 t 满足 a_j \ge a_t, a_{t + 1} \ge a_i。
记 h_{j, i} 表示满足 a_t \le a_j 的最大的 a_{t + 1},即判断 h_{j, i} \ge a_i。
类似于三维偏序,于是考虑 CDQ 分治优化 DP 转移。
假设处理到 [l, r],[l, mid] 已经计算完毕。
考虑分割点的位置在 [l, mid] 还是 [mid + 1, r]:
-
若处于 [l, mid],等价于 h_{j, mid} \ge a_i,此时右端点固定可以用树状数组倒着扫一下求出来。
- 转移条件:h_{j, mid} \ge a_i, a_j < a_i,将两侧分别按照 a_i 排序,扫描线 + 树状数组可以维护;
-
若处于 [mid + 1, r],则 h_{j, mid} < a_i,且满足 a_{t + 1} \ge a_i 的 t 中,\min(a_t) \le a_j。记 g_i = \min(a_t | a_{t + 1} \ge a_i),这个同样可以用树状数组扫一下求出来。
- 转移条件:h_{j, mid} < a_i, a_j \ge g_i,同样可以用扫描线 + 树状数组维护。
于是就做完了。
#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
char buf[1 << 23] , * p1 = buf , * p2 = buf , obuf[1 << 23] , * O = obuf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#endif
inline int read() {
int x = 0 , f = 1;char ch = getchar();
while (!isdigit(ch)) { if (ch == '-') f = -1;ch = getchar(); }
while (isdigit(ch)) x = x * 10 + (ch ^ 48) , ch = getchar();
return x * f;
}
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pi;
const int N = 2e5 + 10;
const int mod = 998244353;
int n;
int f[N], a[N];
int h[N], g[N];
inline void Add (int &x, int y) {
x += y;
if (x >= mod) x -= mod;
if (x < 0) x += mod;
}
namespace bit {
int c[N];
void add (int x, int d) {
for (; x <= n; x += x & -x) c[x] = max(c[x], d);
}
int qry (int x) {
int res = 0;
for (; x; x -= x & -x) res = max(res, c[x]);
return res;
}
void clean (int x) {
for (; x <= n; x += x & -x) c[x] = 0;
}
};
namespace bit2 {
int c[N];
void add (int x, int d) {
for (; x <= n; x += x & -x) Add(c[x], d);
}
int qry (int x) {
int res = 0;
for (; x; x -= x & -x) Add(res, c[x]);
return res;
}
}
namespace bit3 {
int c[N];
void add (int x, int d) {
for (; x <= n; x += x & -x) c[x] = min(c[x], d);
}
int qry (int x) {
int res = n + 1;
for (; x; x -= x & -x) res = min(res, c[x]);
return res;
}
void clean (int x) {
for (; x <= n; x += x & -x) c[x] = n + 1;
}
};
void cdq (int l, int r) {
if (l == r) return;
int mid = l + r >> 1;
//cerr << l << ' ' << r << endl;
cdq (l, mid);
vector<int> id, id2;
for (int i = mid; i >= l; i--) {
bit::add(a[i], a[i + 1]);
h[i] = bit::qry(a[i]);
id.push_back(i);
}
//cerr << "H[i] = ";
// for (int i = l; i <= mid; i ++) //cerr << h[i] << ' ';
//cerr << endl;
for (int i = l; i <= mid; i ++) bit::clean(a[i]);
for (int i = mid + 1; i <= r; i++) id2.push_back(i);
sort (id.begin(), id.end(), [&](int x, int y) {return a[x] < a[y];});
sort (id2.begin(), id2.end(), [&](int x, int y){return a[x] < a[y];});
int j = 0;
for (int x : id2) {
while (j < id.size() && a[id[j]] < a[x]) bit2::add(h[id[j]], f[id[j]]), j ++;
Add(f[x], bit2::qry(n) - bit2::qry(a[x] - 1));
}
for (int t = 0; t < j; t ++) bit2::add(h[id[t]], -f[id[t]]);
//cerr << "F[i] = ";
// for (int i = mid + 1; i <= r; i ++) //cerr << f[i] << ' ';
//cerr << endl;
//cerr << "G[i] = ";
for (int i = mid + 1; i <= r; i++) {
g[i] = bit3::qry(n - a[i] + 1);
//cerr << g[i] << ' ';
if (i != r) bit3::add(n - a[i + 1] + 1, a[i]);
}
//cerr << endl;
for (int i = mid + 1; i < r; i ++) bit3::clean(n - a[i + 1] + 1);
sort (id.begin(), id.end(), [&](int x, int y) {return h[x] < h[y];});
sort (id2.begin(), id2.end(), [&](int x, int y){return a[x] < a[y];});
j = 0;
for (int x : id2) {
while (j < id.size() && h[id[j]] < a[x]) bit2::add(a[id[j]], f[id[j]]), j ++;
if (g[x] != n + 1 && g[x] <= a[x]) Add(f[x], bit2::qry(a[x]) - bit2::qry(g[x] - 1));
}
for (int t = 0; t < j; t ++) bit2::add(a[id[t]], -f[id[t]]);
cdq (mid + 1, r);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i], bit3::c[i] = n + 1, f[i] = (a[1] >= a[i]);
cdq(1, n);
int ans = 0;
for (int i = 1; i <= n; i ++) if (a[i] >= a[n]) Add(ans, f[i]);
cout << ans;
return 0;
}