感觉没有见过这种分块优化的题目啊。。
dp转移很容易就能得出来, 然后分块维护块内信息, 在末尾加入一个数的时候更新块内信息, 更新当前合法的dp值得和。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #define PLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 1e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; const int mod = 998244353; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;} template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;} mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int B = 350; int n, k, a[N]; int belong[N], bl[N], br[N]; int prePos[N], Map[N]; int c[400][N], lazy[400], g[N]; int dp[N], sumdp; void changeUp(int L, int R) { if(L > R) return; if(belong[L] == belong[R]) { for(int i = L; i <= R; i++) { if(g[i] + lazy[belong[i]] == k) sub(sumdp, dp[i]); sub(c[belong[i]][g[i]], dp[i]); g[i]++; add(c[belong[i]][g[i]], dp[i]); } } else { for(int i = L; i <= br[belong[L]]; i++) { if(g[i] + lazy[belong[L]] == k) sub(sumdp, dp[i]); sub(c[belong[i]][g[i]], dp[i]); g[i]++; add(c[belong[i]][g[i]], dp[i]); } for(int i = bl[belong[R]]; i <= R; i++) { if(g[i] + lazy[belong[R]] == k) sub(sumdp, dp[i]); sub(c[belong[i]][g[i]], dp[i]); g[i]++; add(c[belong[i]][g[i]], dp[i]); } for(int i = belong[L] + 1; i <= belong[R] - 1; i++) { sub(sumdp, c[i][k - lazy[i]]); lazy[i]++; } } } void changeDn(int L, int R) { if(L > R) return; if(belong[L] == belong[R]) { for(int i = L; i <= R; i++) { if(g[i] + lazy[belong[i]] == k + 1) add(sumdp, dp[i]); sub(c[belong[i]][g[i]], dp[i]); g[i]--; add(c[belong[i]][g[i]], dp[i]); } } else { for(int i = L; i <= br[belong[L]]; i++) { if(g[i] + lazy[belong[L]] == k + 1) add(sumdp, dp[i]); sub(c[belong[i]][g[i]], dp[i]); g[i]--; add(c[belong[i]][g[i]], dp[i]); } for(int i = bl[belong[R]]; i <= R; i++) { if(g[i] + lazy[belong[R]] == k + 1) add(sumdp, dp[i]); sub(c[belong[i]][g[i]], dp[i]); g[i]--; add(c[belong[i]][g[i]], dp[i]); } for(int i = belong[L] + 1; i <= belong[R] - 1; i++) { add(sumdp, c[i][k + 1 - lazy[i]]); lazy[i]--; } } } int main() { memset(bl, 0x3f, sizeof(bl)); memset(br, 0xc0, sizeof(br)); scanf("%d%d", &n, &k); for(int i = 0; i <= n; i++) { belong[i] = i / B; chkmin(bl[belong[i]], i); chkmax(br[belong[i]], i); } dp[0] = 1; c[0][0] = 1; sumdp = 1; for(int i = 1; i <= n; i++) { scanf("%d", &a[i]); prePos[i] = Map[a[i]]; Map[a[i]] = i; } for(int i = 1; i <= n; i++) { int pos = prePos[i]; int ppos = prePos[pos]; changeUp(pos, i - 1); changeDn(ppos, pos - 1); dp[i] = sumdp; add(sumdp, dp[i]); add(c[belong[i]][0], dp[i]); } printf("%d\n", dp[n]); return 0; } /* */