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 ≤ 10^5, 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
大致题意:告诉你n个不同的数 范围1到n,让你统计有多少个长度为k+1的递增数列.
思路:很容易可以想到dp方程:dp[ num[i] ][ len ]=sum(dp[ num[j] ][ len-1 ])(j < i&&num[i] >num[j],1<=len<=k+1),但是这样的转移时间是O(n),总的时间复杂度为O(k*n^2),肯定gg。所以我们考虑如何去优化转移。这里很巧妙的用到了线段树(当然用树状数组也可以)。我们以这n个数为下标建立k+1棵线段树,按照输入的先后顺序,当我们输入一个数num时,我们就可以在logn的时间内求出dp{num][len]的值,即在第len-1棵树中,查询区间1到num-1的和,然后用所求得的值去更新第len棵树上下标为num位置上的值。最后查询一下第k+1棵树中区间1到n的和即可。这样时间复杂度就降到了O(k*nlogn)
代码如下
代码1.
/*
线段树
*/
#include<cstdio>
#include<iostream>
#define ll long long int
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1