BZOJ 3173: [Tjoi2013]最长上升子序列 Splay
一眼切~
重点是按照 $1$~$n$ 的顺序插入每一个数,这样的话就简单了.
#include <cstdio>
#include <algorithm>
#define N 100004
#define lson t[x].ch[0]
#define rson t[x].ch[1]
#define setIO(s) freopen(s".in","r",stdin) , freopen(s".out","w",stdout)
using namespace std;
int cnt,root,n;
struct Node
{
int ch[2],f,maxv,siz,val;
}t[N];
inline void pushup(int x)
{
t[x].siz=t[lson].siz+t[rson].siz+1;
t[x].maxv=max(t[x].val,max(t[lson].maxv,t[rson].maxv));
}
inline int get(int x) { return t[t[x].f].ch[1]==x; }
inline void rotate(int x)
{
int old=t[x].f,fold=t[old].f,which=get(x);
t[old].ch[which]=t[x].ch[which^1],t[t[old].ch[which]].f=old;
t[x].ch[which^1]=old,t[old].f=x,t[x].f=fold;
if(fold) t[fold].ch[t[fold].ch[1]==old]=x;
pushup(old),pushup(x);
}
inline void splay(int x,int &tar)
{
int u=t[tar].f;
for(int fa;(fa=t[x].f)!=u;rotate(x))
if(t[fa].f!=u)
rotate(get(fa)==get(x)?fa:x);
tar=x;
}
void insert(int &x,int ff,int pos,int v)
{
if(!x)
{
x=++cnt,t[x].f=ff,t[x].val=v,pushup(x);
return;
}
int lsize=t[lson].siz;
if(pos<=lsize+1) insert(lson,x,pos,v);
else insert(rson,x,pos-lsize-1,v);
pushup(x);
}
inline int find(int kth)
{
int x=root;
while(1)
{
if(t[lson].siz+1==kth) return x;
if(t[lson].siz>=kth) x=lson;
else kth-=(t[lson].siz+1), x=rson;
}
}
void debug(int x) {
if(lson) debug(lson);
printf("%d ",t[x].val);
if(rson) debug(rson);
}
int main()
{
int i,j,ans=0;
// setIO("input");
scanf("%d",&n);
for(i=1;i<=n;++i)
{
int k,lst;
scanf("%d",&k);
if(k==0) lst=1;
else if(k==i-1) lst=t[root].maxv+1;
else
{
splay(find(k+1),root);
lst=t[t[root].ch[0]].maxv+1;
}
insert(root,0,k+1,lst), splay(cnt,root);
// printf("%d\n",t[root].val);
printf("%d\n",t[root].maxv);
}
return 0;
}
BZOJ 3173: [Tjoi2013]最长上升子序列 Splay的更多相关文章
- BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]
3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1613 Solved: 839[Submit][St ...
- BZOJ 3173: [Tjoi2013]最长上升子序列( BST + LIS )
因为是从1~n插入的, 慢插入的对之前的没有影响, 所以我们可以用平衡树维护, 弄出最后的序列然后跑LIS就OK了 O(nlogn) --------------------------------- ...
- BZOJ 3173: [Tjoi2013]最长上升子序列
3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1524 Solved: 797[Submit][St ...
- Bzoj 3173: [Tjoi2013]最长上升子序列 平衡树,Treap,二分,树的序遍历
3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1183 Solved: 610[Submit][St ...
- bzoj 3173 [Tjoi2013]最长上升子序列 (treap模拟+lis)
[Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2213 Solved: 1119[Submit][Status] ...
- BZOJ 3173 [Tjoi2013] 最长上升子序列 解题报告
这个题感觉比较简单,但却比较容易想残.. 我不会用树状数组求这个原排列,于是我只好用线段树...毕竟 Gromah 果弱马. 我们可以直接依次求出原排列的元素,每次找到最小并且最靠右的那个元素,假设这 ...
- BZOJ 3173: [Tjoi2013]最长上升子序列 (线段树+BIT)
先用线段树预处理出每个数最终的位置.然后用BIT维护最长上升子序列就行了. 用线段树O(nlogn)O(nlogn)O(nlogn)预处理就直接倒着做,每次删去对应位置的数.具体看代码 CODE #i ...
- bzoj 3173: [Tjoi2013]最长上升子序列【dp+线段树】
我也不知道为什么把题看成以插入点为结尾的最长生生子序列--还WA了好几次 先把这个序列最后的样子求出来,具体就是倒着做,用线段树维护点数,最开始所有点都是1,然后线段树上二分找到当前数的位置,把这个点 ...
- 3173: [Tjoi2013]最长上升子序列
原题:http://www.lydsy.com/JudgeOnline/problem.php?id=3173 题解:促使我写这题的动力是,为什么百度遍地是Treap,黑人问号??? 这题可以用线段树 ...
随机推荐
- 天勤考研数据结构笔记—栈的C语言实现
栈的基本概念 栈的定义:栈是一种只能在一端进行插入或删除操作的线性表.其中允许进行插入或删除的一端称为栈顶(top).栈顶是由一个称为栈顶指针的位置指示器(其实就是一个变量,对于顺序栈,就是数组索引, ...
- @Transient注解的使用(不被序列化和作为临时变量存储)
转自:https://blog.csdn.net/sinat_29581293/article/details/51810805 java 的transient关键字的作用是需要实现Serilizab ...
- mysql拆分逗号一列变多行
需求: SELECT ), ) FROM TABLE a INNER JOIN mysql.help_topic b )
- hashMap怎样解决hash冲突
通过链表的方式处理: java1.7是单向链表 jvav1.8在数量小于8时是单向链表,大于8就是红黑树,查找方式遍历判断 解决冲突的方式很多,例如再hash,再散列(开放地址法,探测再散列)
- 纯CSS实现tag彩色标签
利用纯CSS实现彩色tag标签,效果如下图 代码如下: .items a:nth-child(9n){background-color: #4A4A4A;} .items a:nth-child(9n ...
- jq上滑加载更多
html 结构 <div id="main"> <ul class="order-list" id="list_box"& ...
- 第二十二篇 jQuery 学习4 内容和属性
jQuery 内容和属性 这节课,我们学习使用jQuery来控制元素的内容.值和属性. html() 控制所选元素的内容(包括HTML标记): text() 控制所选元素的内容: val() ...
- N4_75条语法
1. コ/ソ/ア/ド体系 -(こ.そ.あ.ど)れ/-(こ.そ.あ.ど)の A:-(こ.そ.あ.ど)れ 接续: 指示代词和场所代词,分近称.中称.远称.疑问称. 意思: 这个,那个,那个,哪个 例:これ ...
- Spring Boot整合actuator实现监控管理
Spring Boot使用actuator监控管理 1.在pom文件中导入相关的依赖 <dependency> <groupId>org.springframework.boo ...
- contenteditable兼容问题
正常情况下用contenteditable,IE下有兼容性问题需要将个别字母变成大写的contentEditable. 获取contenteditable的内容 对html进行处理 兼容 chrome ...