大致题意:

有一个空序列,依次插入1~N到该序列中,每次指定插入的位置,每次插入完成返回当前序列的LIS的长度。

题解:

设dp[i]表示 前缀1~i的最长上升子序列的长度。

因为是按照递增顺序插入的,所以当刚插入完某个数到i位置(此时能保证该数是当前序列的最大值)dp[i] = max{ dp[j] | j<i },答案 ans=max{ dp[i] | i in [1,len] }

因为要支持动态插入,所以要用BST来做,每个节点代表一个位置(即树的中序遍历就是该序列),每个节点维护dp[i]和 dpmax[i] = max{ dp[i] | i in the subtree }

代码:

 #include <cstdio>
#include <iostream>
#define maxn 100010
using namespace std; struct Splay {
int pre[maxn], son[maxn][], siz[maxn], ntot, root;
int dp[maxn], dmax[maxn]; Splay() {
root = ntot = ;
}
void update( int nd ) {
siz[nd] = siz[son[nd][]] + siz[son[nd][]] + ;
dmax[nd] = max( dp[nd], max( dmax[son[nd][]], dmax[son[nd][]] ) );
}
void rotate( int nd, int d ) {
int p = pre[nd];
int s = son[nd][!d];
int ss = son[s][d]; son[nd][!d] = ss;
son[s][d] = nd;
if( !p ) root = s;
else son[p][ nd==son[p][] ] = s; pre[s] = p;
pre[nd] = s;
if( ss ) pre[ss] = nd; update(nd);
update(s);
}
void splay( int nd, int top ) {
while( pre[nd]!=top ) {
int p = pre[nd];
if( pre[p]==top ) {
rotate( p, son[p][]==nd );
} else {
int pp = pre[p];
int pl = p == son[pp][];
int nl = nd == son[p][];
if( pl==nl ) {
rotate( pp, pl );
rotate( p, nl );
} else {
rotate( p, nl );
rotate( pp, pl );
}
}
}
}
int find( int pos ) { // pos in [1,sz]
int nd = root;
while() {
int ls = siz[son[nd][]];
if( pos<=ls ) {
nd = son[nd][];
} else if( pos>=ls+ ) {
nd = son[nd][];
pos -= ls+;
} else {
return nd;
}
}
}
int premax( int pos ) {
int nd = root;
int rt = ;
while() {
int ls = siz[son[nd][]];
if( pos<=ls ) {
nd = son[nd][];
} else if( pos>=ls+ ) {
rt = max( rt, max( dp[nd], dmax[son[nd][]] ) );
nd = son[nd][];
pos -= ls+;
} else {
rt = max( rt, max( dp[nd], dmax[son[nd][]] ) );
break;
}
}
return rt;
}
int newnode( int p, int v ) {
int nd = ++ntot;
pre[nd] = p;
son[nd][] = son[nd][] = ;
siz[nd] = ;
dp[nd] = dmax[nd] = v;
return nd;
}
void insert( int pos ) {
if( !root ) {
root = newnode( , );
return;
}
if( pos== ) {
int nd = root;
while( son[nd][] ) nd=son[nd][];
son[nd][] = newnode( nd, );
update( nd );
splay( nd, );
return;
}
int nd = find( pos );
int nw = newnode( nd,premax(pos)+ );
int s = son[nd][];
son[nd][] = nw;
son[nw][] = s;
pre[nw] = nd;
if( s ) pre[s] = nw;
update( son[nd][] );
update( nd );
splay( nd, );
}
void print( int nd ) {
if( !nd ) return;
print( son[nd][] );
printf( "%d(%d,%d,%d,%d,%d) ", dp[nd], nd, pre[nd], son[nd][], son[nd][], nd==son[pre[nd]][] );
print( son[nd][] );
}
}; Splay T;
int n;
int main() {
//freopen( "input", "r", stdin );
scanf( "%d", &n );
for( int i=,pos; i<=n; i++ ) {
scanf( "%d", &pos );
T.insert( pos );
// T.print(T.root);
// printf( "\n" );
printf( "%d\n", T.dmax[T.root] );
}
}

bzoj3173 Splay 维护前缀中的最大值的更多相关文章

  1. 【BZOJ 3729】3729: Gty的游戏 (Splay维护dfs序+博弈)

    未经博主同意不得转载 3729: Gty的游戏 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 448  Solved: 150 Description ...

  2. BZOJ1014[JSOI2008]火星人prefix(splay维护hash)

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  3. bzoj3786星系探索(splay维护dfs序)

    Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...

  4. 51Nod 1277 字符串中的最大值 ( KMP && DP )

    题意 : 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd.给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值.例如:S ...

  5. 51Nod 1277 字符串中的最大值(KMP,裸题)

    1277 字符串中的最大值 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如: ...

  6. 51nod 1277 字符串中的最大值

    题目链接 51nod 1277 字符串中的最大值 题解 对于单串,考虑多串的fail树,发现next数组的关系形成树形结构 建出next树,对于每一个前缀,他出现的次数就是他子树的大小 代码 #inc ...

  7. 51nod 1277字符串中的最大值(拓展kmp)

    题意: 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd. 给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值.   题解 ...

  8. HNOI2004宠物收养所(splay维护二叉搜索树模板题)

    描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...

  9. SPOJ - GSS1-Can you answer these queries I 线段树维护区间连续和最大值

    SPOJ - GSS1:https://vjudge.net/problem/SPOJ-GSS1 参考:http://www.cnblogs.com/shanyr/p/5710152.html?utm ...

随机推荐

  1. innobackupex 相关语法讲解【转】

    innobackupex 相关语法讲解 连接服务器 The database user used to connect to the server and its password are speci ...

  2. asp.net 伪静态实现(UrlRewritingNet)

    UrlRewritingNet.UrlRewriter源码地址 https://github.com/aspnetde/UrlRewritingNet部署步骤: 步骤一: <!--只允许存在一个 ...

  3. xshell5 优化方案

    有道云笔记链接-> grep: 过滤 过滤的速度是最快的(相对于另外两个) -v -n -o   显示grep匹配到了什么 grep .  -o -i   --ignore-case -E == ...

  4. vue+vuex+axios+echarts画一个动态更新的中国地图

    一. 生成项目及安装插件 # 安装vue-cli npm install vue-cli -g # 初始化项目 vue init webpack china-map # 切到目录下 cd china- ...

  5. PL/SQL开发中动态SQL的使用方法

    一般的PL/SQL程序设计中,在DML和事务控制的语句中可以直接使用SQL,但是DDL语句及系统控制语句却不能在PL/SQL中直接使用,要想实现在PL/SQL中使用DDL语句及系统控制语句,可以通过使 ...

  6. 安装mysql-python报错解决办法

    报错: 按照网上的办法,安装mysql-connector-c-6.1.10-winx64.msi和MySQL-python-1.2.3.win-amd64-py2.7 .exe都不行,又源码安装My ...

  7. mysql 导出数据库命令

    mysqldump --socket=/data/mysql/mysql.sock -uroot -pfanzhuo -d stat1> stat1.sql

  8. GitHub正式启用声明

    0x00 说明 GitHub账号很早之前就已经注册,几经常识都没有能够好好的将这座宝库应用好,实在是汗颜.今天特地将GitHub重新拾起,以后一定要好好使用这个利器,不要荒废了. 0x01 内容 Gi ...

  9. (转)最短路算法 -- Floyd算法

    转自:http://blog.51cto.com/ahalei/1383613        暑假,小哼准备去一些城市旅游.有些城市之间有公路,有些城市之间则没有,如下图.为了节省经费以及方便计划旅程 ...

  10. python 学习之dict和set类型

    什么是dict 我们已经知道,list 和 tuple 可以用来表示顺序集合,例如,班里同学的名字: ['Adam', 'Lisa', 'Bart'] 或者考试的成绩列表: [95, 85, 59] ...