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. DevExpress控件GridControl使用 z

    设置选中行的背景色.而不改变前景色. EnableAppearanceFocusedCell = False, EnableAppearanceFocusedRow = False private v ...

  2. EasyHook库系列使用教程之四钩子的启动与停止

    此文的产生花费了大量时间对EasyHook进行深入了解同一时候參考了大量文档 先来简单比較一下EasyHook与Detour钩取后程序流程 Detours:钩取API函数后.产生两个地址,一个地址相应 ...

  3. 【Nginx】事件驱动框架和异步处理

    Nginx对请求的处理是通过事件触发的,模块作为事件消费者,仅仅能被事件收集.分发器调用.这与传统的Webserver是不同的. 传统的Webserver下,一个请求由一个进程消费.请求在建立连接后将 ...

  4. Android自己定义ViewGroup打造各种风格的SlidingMenu

    看鸿洋大大的QQ5.0側滑菜单的视频课程,对于側滑的时的动画效果的实现有了新的认识,似乎打通了任督二脉.眼下能够实现随意效果的側滑菜单了.感谢鸿洋大大!! 鸿洋大大用的是HorizontalScrol ...

  5. django 实现下载功能

    from django.http import FileResponse def download(request): if..... file=open([path],'rb') response= ...

  6. Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  7. javascript 高级编程系列 - 基本数据类型

    javascript中的基本数据类型包括: Undefined, Null, Boolean, Number, String 5种数据类型 1. Undefined 类型 (只有一个值 undefin ...

  8. iOS移动开发周报-第20期

    iOS移动开发周报-第20期iOS移动开发周报-第20期 [摘要]:本期iOS移动开发周报带来如下内容:iOS 通知中心扩展制作入门,iOS APP可执行文件的组成,objc非主流代码技巧等. 教程 ...

  9. Active Directory的LDAP协议与DN(Distinguished Name)详解

    前言 光copy几段代码的文章没什么意思,本章上最基础的代码,主要是为了从编程方面聊LDAP和DN,其它的后面聊,一步步慢慢来吧. Active Directory编程须知 1.域控服务器: Wind ...

  10. Sass编译css/Grunt压缩文件

    Sass安装(mac) $ sudo gem install sass scss编译成css文件 $ sass ui.scss ui.css CLI安装 $ npm install -g grunt- ...