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. UnicodeEncodeError: 'ascii' codec can't encode character u'\u5728' in position 1

    s = "图片picture"print chardet.detect(s) for c in s.decode('utf-8'): print c UnicodeEncodeEr ...

  2. 几种支持动作模型格式的比较(MD2,MD5,sea3d) 【转】

    最近使用了几种不同的模型格式做人物动作的表现,记录一下优缺点   1) MD2 数据内容: 记录了所有动作顶点数据 数据格式: 二进制 动作文件: 动作文件合并在一个模型文件 文件大小: 动作多时很大 ...

  3. Git学习0基础篇(下)

    server上的 Git - 协议 Git能够使用四种基本的协议传输资料:本地协议(Local).HTTP 协议.SSH(Secure Shell) 协议以及 Git 协议.眼下使用最普及的是 SSH ...

  4. 在pypy环境中运行odoo8

    PyPy是一个独立的解析器, 通过即时编译(JIT,Just-in-time)代码避免逐行解释执行来提升运行速度的(将编译过的行代码缓存起来,从而加快速度).我们一般使用的Python一般是使用C实现 ...

  5. overflow滚动条样式设置,ie和webkit内核

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. python的多线程问题

    在对文件进行预处理的时候,由于有的文件有太大,处理很慢,用python处理是先分割文件,然后每个文件起一个线程处理,启了10个线程,结果还比不起线程慢一些,改成多进程之后就好了. 使用multipro ...

  7. PCB常用单位转换 mil 英尺

    PCB常用单位转换 mil 英尺 相关常用单位     1mil   = 0.0254mm 100mil = 2.54mm 1英寸   = 1000mil = 2.54cm 1英尺   = 12英寸  ...

  8. 文件I/O相关函数

    open()和openat()函数: #include <fcntl.h> // 成功返回文件描述符,出错返回-1 int open(const char *path, int oflag ...

  9. ajax短信验证码-mvc

    <script type="text/javascript"> function SendMessage() { var phoneNumberInput = docu ...

  10. 生产追溯系统-Wifi+传感器,实现计数器以及监控机器是否停止

    物联网听上去是一个高大上的词儿,还有什么大数据.云.智能制造等等,今天我也往这方面稍微靠一靠,这篇文章主要介绍的是通过 wifi 模块与传感器组合,实现感应计数器,应用场景主要如下: 1.统计 SMT ...