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

题意:求多少个LIS,其长度为K+1。

思路:用dp[k][x]表示以x结尾的长度为k的LIS。那么暴力的DP复杂度是O(N^2)。

建立k棵线段树,每棵树N个叶子节点,第i棵树的第j个叶子节点表示以j为结尾的长度为i的LIS个数。 那么Tree[i][j]+=Tree[i-1][1到j-1]。

(直接写线段树也可以,这里练习一下主席树。 当然最好写的还是树状数组!

相比的话,主席树应该还是好写的。

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=;
int rt[maxn],cnt; ll ans;
struct node
{
int l,r; ll val;
node(){ l=r=;val=;}
node(int L,int R,ll V):l(L),r(R),val(V){}
}s[maxn*];
ll query(int Now,int L,int R,int l,int r)
{
if(l<=L&&r>=R) return s[Now].val;
ll res=; int Mid=(L+R)>>;
if(l<=Mid) res+=query(s[Now].l,L,Mid,l,r);
if(r>Mid) res+=query(s[Now].r,Mid+,R,l,r);
return res;
}
void update(int &Now,int L,int R,int pos,ll val)
{
if(Now==) Now=++cnt;
s[Now].val+=val;
if(L==R) return ; int Mid=(L+R)>>;
if(pos<=Mid) update(s[Now].l,L,Mid,pos,val);
else update(s[Now].r,Mid+,R,pos,val);
}
int main()
{
int N,K,i,j,x;
scanf("%d%d",&N,&K);
for(i=;i<=N;i++){
scanf("%d",&x); x+=;
update(rt[],,N+,x,);
for(j=;j<=K;j++) {
ll tmp=query(rt[j],,N+,,x-);
update(rt[j+],,N+,x,tmp);
}
}
ans=query(rt[K+],,N+,,N+);
printf("%I64d\n",ans);
return ;
}

最新文章

  1. Ubuntu 14.04 更换阿里云源
  2. python asyncio笔记
  3. poj 1985 Cow Marathon
  4. wave文件(*.wav)格式、PCM数据格式, goldwave 可以播放pcm raw audio
  5. Timestamp的作用及与字符串的相互转换 .
  6. java几种字符串反转
  7. Problem F: Exponentiation
  8. maven下的sqlserver配置jar包
  9. 音频PCM编码
  10. 通过grub-install命令把grub安装到u盘-总结
  11. ubuntu日常使用指南
  12. dp Surf
  13. js之省市区(县)三级联动效果
  14. 021.Zabbix的邮件告警-01
  15. empty是判断一个变量是否为“空”,而isset 则是判断一个变量是否已经设置
  16. python3: 字符串和文本
  17. WPF 使用OCX控件速度很慢
  18. maven常用的一些依赖
  19. Python线程指南(转)
  20. 【剑指offer】把二叉树打印成多行,C++实现

热门文章

  1. paramiko获取远程主机的环境变量
  2. excel怎么把文本格式的数字转换为数字,且把前面的撇号去掉
  3. fastjson中Map与JSONObject互换,List与JOSNArray互换的实现
  4. PS 如何用PS制作GIF图像
  5. 【Caffe代码解析】compute_image_mean
  6. AVOS Cloud 技术支持系统开源了
  7. springMVC学习之验证
  8. enter键触发的函数
  9. 编程算法 - 多重部分和问题 代码(C)
  10. mongoDB之监控工具mongostat及其参数的具体含义