题解:AT_arc125_d [ARC125D] Unique Subsequence
这题看上去很 DP,但是“仅出现一次的子序列”这个东西并不好刻画,我们考虑把它转化成另一种表现形式。\
反过来想:什么时候会出现重复的子序列呢?当前在
设
容易写出
代码中保留
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 15, mod = 998244353;
template <class type,const type mod> struct Modint{ Conan15 qwq? };
typedef Modint<long long, mod> Mint; //记得自定义 mod
int n, a[N];
int id[N], lst[N];
Mint dp[N], ans;
Mint tr[N];
void add(int x, Mint d) {
for ( ; x < N; x += x & -x) tr[x] += d;
}
Mint query(int x) {
if (x == -1) return 0;
Mint res = 0;
for ( ; x ; x -= x & -x) res += tr[x];
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
lst[i] = id[a[i]], id[a[i]] = i;
}
// for (int i = 1; i <= n; i++) {
// int pos = lst[i];
// for (int j = pos; j < i; j++) dp[i] += dp[j];
// if (!pos) dp[i]++;
// dp[pos] = 0;
// }
for (int i = 1; i <= n; i++) {
int pos = lst[i];
dp[i] = query(i - 1) - query(pos - 1);
if (!pos) dp[i]++;
else add(pos, -dp[pos]), dp[pos] = 0;
add(i, dp[i]);
}
for (int i = 1; i <= n; i++) ans += dp[i];
printf("%lld\n", ans);
return 0;
}