Description
For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·1018.
Input
First line contain two integer values n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.
Next n lines contains one integer ai (1 ≤ ai ≤ n) each — elements of sequence. All values ai are different.
Output
Print one integer — the answer to the problem.
Examples
input
5 2
1
2
3
5
4
output
7
题意:求长度为k+1的上升子序列有多少个
解法:sum=dp[0][k+1]+dp[1][k+1]+dp[2][k+1]+....dp[n][k+1]
dp[x][y]是x为上升子序列最后一个元素,长度为y的个数
用树状数组维护,更新的是num为上升子序列最后一个元素,长度为j时,加上num为上升子序列最后一个元素,长度为j-1时个数
最后求sum(n,m+1)总和
1 #include <bits/stdc++.h> .h> 2 using namespace std; 3 #define ll long long 4 ll n,m; 5 ll dp[200000][20]; 6 ll bit(ll x) 7 { 8 return x&(-x); 9 } 10 void up(ll x,ll y,ll ans) 11 { 12 while(x<200000) 13 { 14 dp[x][y]+=ans; 15 x+=bit(x); 16 } 17 } 18 ll sum(ll x,ll y) 19 { 20 ll ans=0; 21 while(x>0) 22 { 23 ans+=dp[x][y]; 24 x-=bit(x); 25 } 26 return ans; 27 } 28 int main() 29 { 30 cin>>n>>m; 31 up(1,0,1); 32 for(int i=1;i<=n;i++) 33 { 34 ll num; 35 cin>>num; 36 for(int j=m+1;j>=1;j--) 37 { 38 up(num,j,sum(num,j-1)); 39 } 40 } 41 cout<<sum(n,m+1)<<endl; 42 return 0; 43 }