CF597C Subsequences 树状数组 + 动态规划

CF597C Subsequences 树状数组 + 动态规划

设$f(i, j)$表示以$i$结尾的,长为$j$的上升子序列的数量

转移时用树状数组维护即可

复杂度为$O(kn \log n)$

注:特判0

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
namespace remoon {
#define ri register int
#define ll long long
#define tpr template <typename ra>
#define rep(iu, st, ed) for(ri iu = st; iu <= ed; iu ++)
#define drep(iu, ed, st) for(ri iu = ed; iu >= st; iu --)
#define gc getchar
inline int read() {
int p = , w = ; char c = gc();
while(c > '' || c < '') { if(c == '-') w = -; c = gc(); }
while(c >= '' && c <= '') p = p * + c - '', c = gc();
return p * w;
}
int wr[], rw;
#define pc(iw) putchar(iw)
tpr inline void write(ra o, char c = '\n') {
if(!o) pc('');
if(o < ) o = -o, pc('-');
while(o) wr[++ rw] = o % , o /= ;
while(rw) pc(wr[rw --] + '');
pc(c);
}
}
using namespace std;
using namespace remoon; #define sid 100050 int n, k, a[sid];
ll t[sid][]; inline void upd(int o, ll v, int k) {
for(ri i = o; i <= n + ; i += i & (-i))
t[i][k] += v;
} inline ll qry(int o, int k) {
ll ret = ;
for(ri i = o; i; i -= i & (-i))
ret += t[i][k];
return ret;
} inline void DP() {
ll ans = ;
if(k == ) ans = ;
rep(i, , n) {
upd(a[i], , );
rep(j, , k + ) {
ll S = qry(a[i] - , j - );
if(j == k + ) { ans += S; break; }
upd(a[i], S, j);
}
}
write(ans);
} int main() {
n = read(); k = read();
rep(i, , n) a[i] = read() + ;
DP();
return ;
}
上一篇:centos禁ping


下一篇:2021一位Android中级程序员的跳槽面经,实现原理分析