51nod1376 最长上升子序列的数量

时间:2023-07-18 18:43:50

51nod1376 最长上升子序列的数量

51nod1376 最长上升子序列的数量

机房的人问我树状数组怎么做这题......

树状数组维护$len, num$表示$LIS$的长度和数量即可

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

注:$O(n \log n)$二分+单调栈才是真神仙

具体看代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; extern inline char gc() {
static char RR[], *S = RR + , *T = RR + ;
if(S == T) fread(RR, , , stdin), S = RR;
return *S ++;
}
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;
} #define mod 1000000007
#define ri register int
#define sid 50050 int n, cnp, H[sid], a[sid];
struct aha {
int len, num;
} t[sid], f[sid]; void upd(aha &x, aha y) {
if(x.len > y.len) return;
if(x.len < y.len) x = y;
else x.num += y.num, x.num %= mod;
} aha qry(int x) {
aha ret = { , };
for(ri i = x; i; i -= i &(-i)) upd(ret, t[i]);
return ret;
} aha add(int x, aha v) {
for(ri i = x; i <= cnp; i += i & (-i)) upd(t[i], v);
} int main() {
n = read();
for(ri i = ; i <= n; i ++) a[i] = H[i] = read();
sort(H + , H + n + );
cnp = unique(H + , H + n + ) - H - ;
for(ri i = ; i <= n; i ++) a[i] = lower_bound(H + , H + cnp + , a[i]) - H;
for(ri i = ; i <= n; i ++) {
f[i] = qry(a[i] - ); f[i].len ++;
add(a[i], f[i]);
}
aha ans = { , };
for(ri i = ; i <= n; i ++) upd(ans, f[i]);
printf("%d\n", ans.num);
return ;
}