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 ;
}

CodeForces - 597C:Subsequences (主席树+DP)的更多相关文章

  1. CodeForces - 597C Subsequences (树状数组+动态规划)

    For the given sequence with n different elements find the number of increasing subsequences with k + ...

  2. codeforces 1262D Optimal Subsequences 主席树询问第k小

    题意 给定长度为\(n\)的序列\(a\),以及m个询问\(<k,pos>\),每次询问满足下列条件的子序列中第\(pos\)位的值为多少. 子序列长度为\(k\) 序列和是所有长度为\( ...

  3. CodeForces960F:Pathwalks (主席树+DP)

    You are given a directed graph with n nodes and m edges, with all edges having a certain weight. The ...

  4. Codeforces.765F.Souvenirs(主席树)

    题目链接 看题解觉得非常眼熟,总感觉做过非常非常类似的题啊,就是想不起来=v=. 似乎是这道...也好像不是. \(Description\) 给定长为\(n\)的序列\(A_i\).\(m\)次询问 ...

  5. Codeforces 893F(主席树+dfs序)

    在子树内和距离不超过k是一个二维限制,容易想到主席树,但主席树显然没法查最小值,因为不满足区间可减.kdtree和二维线段树可以干这事,但肯定会T飞.但事实上我们的问题有一个特殊性:对某个点x,查询其 ...

  6. Optimal Subsequences(主席树)

    题意: 给定一个序列,长度为n(<=2e5),有m(<=2e5)个询问,每个询问包含k和pos,要从原序列中选出一个长度为k的子序列,要求是这个序列的和是所有长度为k的序列中最大的,且是字 ...

  7. Codeforces 597C. Subsequences (树状数组+dp)

    题目链接:http://codeforces.com/contest/597/problem/C 给你n和数(1~n各不同),问你长为k+1的上升自序列有多少. dp[i][j] 表示末尾数字为i 长 ...

  8. CodeForces - 597C Subsequences 【DP + 树状数组】

    题目链接 http://codeforces.com/problemset/problem/597/C 题意 给出一个n 一个 k 求 n 个数中 长度为k的上升子序列 有多少个 思路 刚开始就是想用 ...

  9. Codeforces 543D. Road Improvement (树dp + 乘法逆元)

    题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...

随机推荐

  1. mac mysql忘记密码解决办法

    http://www.jb51.net/article/87580.htm http://blog.csdn.net/soft2buy/article/details/50223373

  2. Excel导入的HDR=YES; IMEX=1详解

    参数HDR的值:HDR=Yes,这代表第一行是标题,不做为数据使用 ,如果用HDR=NO,则表示第一行不是标题,做为数据来使用.系统默认的是YES 参数Excel 8.0对于Excel 97以上版本都 ...

  3. DICOM:DICOM万能编辑工具之Sante DICOM Editor

    版权声明:本文为zssure原创文章,转载请注明出处,未经允许不得转载.   目录(?)[-] 背景 DICOM Service的配置 Sante DICOM Editor自启动的服务 PACS查询下 ...

  4. Solaris网络基础

      划分子网: 把大网缩小为若干个小网.修改子网掩码,划分多个网络. 那么如何确定子网的子网掩码和IP地址? 以上你会发现少了6个IP.   Ifconfig e1000g0 down down掉网卡 ...

  5. adb命令具体解释(二)——手机缺失sqlite3时操作数据库的多种解决方式

    在android应用开发无处不在SQLite数据库的身影.那么在开发中怎么使用adb命令操作数据库的功能呢? 以下我们将完整的介绍与数据库操作相关的命令集及当手机缺少sqlite3的时候的多种解决方式 ...

  6. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  7. QQ空间g_tk、bkn加密参数算法

    g_tk是腾讯在QQ空间这一领域使用的密文,有写数据包或者url参数中需要加入你计算出的g_tk才能成功! 下面是通过浏览器抓包工具抓取 访问该js内容找出 QZONE.FrontPage.getAC ...

  8. 【转】Windows2008上传大文件的解决方法(iis7解决上传大容量文件)

    2008上传大文件的解决方法:http://wenku.it168.com/d_000091739.shtml 2003上传大文件的解决方法:http://tech.v01.cn/windowsxit ...

  9. Python 模块之 ConfigParser: 用 Python 解析配置文件

    在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在 Python 里更是如此,在官方发布的库中就包含有做这件事情的库,那就是 ConfigParser,这里简单的做 ...

  10. WPF 的 MVVM

    Model——View——ViewModel http://www.cnblogs.com/fdyang/p/3877309.html