题意

给定$n$个数,求最长上升子序列的方案数


根据数据范围要求是$O(n\log n)$

朴素的dp方程式$f_i=max(f_j+1),a_i>a_j$,所以记方案数为$v_i$,则$v_i=v_i+v_j,(f_i=f_j+1)$,利用lis的$O(n\log n)$树状数组做法同时维护长度和方案数

从通酱博客里还看到更详尽的解释:stackoverflow

时间复杂度$O(n\log n)$

代码

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
int n,m,readin;
pii f[100005];
pii query(int x) {
pii ret=make_pair(0,1);
while(x) {
if(f[x].first>ret.first)ret=f[x];
else if(f[x].first==ret.first)ret.second=(ret.second+f[x].second)%m;
x-=x&(-x);
}
return ret;
}
void add(int x,pii v) {
while(x<=n) {
if(f[x].first<v.first)f[x]=v;
else if(f[x].first==v.first)f[x].second=(f[x].second+v.second)%m;
x+=x&(-x);
}
}
int main() {
scanf("%d%d",&n,&m);
for(int i=1;i<=n;++i) {
scanf("%d",&readin);
pii t=query(readin);
t.first++;
add(readin,t);
}
printf("%d\n",query(n).second);
return 0;
}

【Codeforces】Gym 101156E Longest Increasing Subsequences LIS+树状数组的更多相关文章

  1. 【二分】【动态规划】Gym - 101156E - Longest Increasing Subsequences

    求最长上升子序列方案数. 转载自:http://blog.csdn.net/u013445530/article/details/47958617,如造成不便,请博主联系我. 数组A包含N个整数(可能 ...

  2. 【bzoj2225】[Spoj 2371]Another Longest Increasing CDQ分治+树状数组

    题目描述 给定N个数对(xi, yi),求最长上升子序列的长度.上升序列定义为{(xi, yi)}满足对i<j有xi<xj且yi<yj. 样例输入 8 1 3 3 2 1 1 4 5 ...

  3. Codeforces 946G Almost Increasing Array (树状数组优化DP)

    题目链接   Educational Codeforces Round 39 Problem G 题意  给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...

  4. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  5. CodeForces - 314C Sereja and Subsequences (树状数组+dp)

    Sereja has a sequence that consists of n positive integers, a1, a2, ..., an. First Sereja took a pie ...

  6. Codeforces 960F Pathwalks ( LIS && 树状数组 )

    题意 : 给出若干个边,每条边按照给出的顺序编号,问你找到一条最长的边权以及边的编号同时严格升序的一条路径,要使得这条路径包含的边尽可能多,最后输出边的条数 分析 :  这题和 LIS 很相似,不同的 ...

  7. HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences             ...

  8. CF 314C Sereja and Subsequences(树状数组)

    题目链接:http://codeforces.com/problemset/problem/314/C 题意:给定一个数列a.(1)写出a的不同的所有非下降子列:(2)定义某个子列的f值为数列中各个数 ...

  9. Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)

    [题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...

随机推荐

  1. apue学习笔记(第四章 文件和目录)

    本章将描述文件系统的其他特性和文件的性质. 函数stat.fstat.fstatat和lstat #include <sys/stat.h> int stat(const char *re ...

  2. Ubuntu安装vncserver实现图形化远程桌面

    安装 apt-get update apt-get install vnc4server 开启vnc服务 vncserver 首次启动会要求设置密码,后面可以使用vncpasswd修改: 看到 New ...

  3. Scrapy安装向导

    原文地址 https://doc.scrapy.org/en/latest/intro/install.html 安装Scrapy Scrapy运行在python2.7和python3.3或以上版本( ...

  4. Mysql 5.7.18 利用 MySQL proxies_priv(模拟角色)实现类似用户组管理

    利用 MySQL proxies_priv(模拟角色)实现类似用户组管理 角色(Role)可以用来批量管理用户,同一个角色下的用户,拥有相同的权限. MySQL5.7.X以后可以模拟角色(Role)的 ...

  5. 【WPF】ComboBox:根据绑定选取、设置固定集合中的值

    问题场景 我有一个对象,里面有一个属性叫Limit,int类型.虽然int可取的范围很大,我想要在用户界面上限制Limit可取的值,暂且限制为5.10.15.20. 所以ComboBox绑定不是绑定常 ...

  6. python从安装与使用pip到入门

    官方下载地址:https://www.python.org/downloads/ 下载后直接安装就可以了 再配一下环境变量, cmd运行python -V (注意,这里是大写的V) 打开python跑 ...

  7. spring AOP(切面)

    AOP 基本概念cross  cutting concern横切性关注点:独立服务,遍布在系统处理流程之中 Aspect对横切关注点模块化 advice对横切关注点具体实现 pointcut定义adv ...

  8. ms人物上线

    在看MS人物上线 else if(gs2ms_add_player == pkt.cmd) { PlayerChannel* pPC = new PlayerChannel(this); //加到地图 ...

  9. 【BZOJ1316】树上的询问 点分治+set

    [BZOJ1316]树上的询问 Description 一棵n个点的带权有根树,有p个询问,每次询问树中是否存在一条长度为Len的路径,如果是,输出Yes否输出No. Input 第一行两个整数n, ...

  10. c++ get the pointer from the reference

    int x = 5; int& y = x; int* xp = &x; int* yp = &y; xp is equal to yp. 也就是说,直接对reference取 ...