bzoj3173 Splay 维护前缀中的最大值
大致题意:
有一个空序列,依次插入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 维护前缀中的最大值的更多相关文章
- 【BZOJ 3729】3729: Gty的游戏 (Splay维护dfs序+博弈)
未经博主同意不得转载 3729: Gty的游戏 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 448 Solved: 150 Description ...
- BZOJ1014[JSOI2008]火星人prefix(splay维护hash)
Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...
- bzoj3786星系探索(splay维护dfs序)
Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...
- 51Nod 1277 字符串中的最大值 ( KMP && DP )
题意 : 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd.给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值.例如:S ...
- 51Nod 1277 字符串中的最大值(KMP,裸题)
1277 字符串中的最大值 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如: ...
- 51nod 1277 字符串中的最大值
题目链接 51nod 1277 字符串中的最大值 题解 对于单串,考虑多串的fail树,发现next数组的关系形成树形结构 建出next树,对于每一个前缀,他出现的次数就是他子树的大小 代码 #inc ...
- 51nod 1277字符串中的最大值(拓展kmp)
题意: 一个字符串的前缀是指包含该字符第一个字母的连续子串,例如:abcd的所有前缀为a, ab, abc, abcd. 给出一个字符串S,求其所有前缀中,字符长度与出现次数的乘积的最大值. 题解 ...
- HNOI2004宠物收养所(splay维护二叉搜索树模板题)
描述 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特殊的公式,得出该领 ...
- 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 ...
随机推荐
- required_new spring事务传播行为无效碰到的坑!
在测试事务传播行为的时候,因为用了同一个service中的方法测试,所以不管怎么设置都无效了: 原因是aop动态代理只会拦截一次执行方法,第二个方法是照搬的,只要调用其他service中的事务方法,传 ...
- Mysql_Learning_Notes_mysql系统结构_2
Mysql_Learning_Notes_mysql系统结构_2 三层体系结构,启动方式,日志类型及解析方法,mysql 升级 连接层 通信协议处理\线程处理\账号认证(用户名和密码认证)\安全检查等 ...
- Django-自动HTML转义
一.自动HTML转义 从模板生成HTML时,总会有变量包含影响最终HTML的字符风险,例如,考虑这个模板的片段: Hello, {{ name }} 起初,这是一种显示用户名的无害方式,但考虑用户输入 ...
- 数据库-mysql数据类型
MySQL 数据类型 MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准S ...
- MyBatis框架的基本使用
MyBatis框架简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名 ...
- yum安装Mysql-5.6
MySQL yum库提供了一个简单的和方便的方法来安装和更新MySQL相关的软件包到最新版本. MySQL yum库文档说明:http://dev.mysql.com/doc/mysql-yum-re ...
- sqlserver sp_spaceused用法
sp_spaceused显示行数.保留的磁盘空间以及当前数据库中的表所使用的磁盘空间,或显示由整个数据库保留和使用的磁盘空间. 语法sp_spaceused [[@objname =] 'objnam ...
- python对象学习
python对象的介绍 python使用对象模型来存储数据,构造任何类型的值都是一个对象,尽管python被当成面向对象的编程语言,但是完全编写不使用任何类和实例的脚本.所有的python对象都拥有三 ...
- Java关于数组操作函数
数组排序及元素查找 sort()方法对Java数组进行排序. binarySearch() 方法来查找数组中的元素,返回该元素所在的位置. import java.util.*; public cla ...
- MongoDB查询用法大全
转载 http://blog.163.com/lgh_2002/blog/static/440175262012052116455/ 详见官方的手册: http://www.mongodb.org/d ...