BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]
3173: [Tjoi2013]最长上升子序列
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1613 Solved: 839
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
0 0 2
Sample Output
1
2
HINT
100%的数据 n<=100000
插入的数字是递增的,也就是说插入数字后不会改变其他元素的dp值
有一个离线做法:用treap动态把序列弄出来然后求lis
http://hzwer.com/6254.html
在线用splay,f表示以当前元素结尾的LIS长度,mx表示他的子树里最大的f
把新插入的元素转到根后,f就是左子树mx+1(不可能有非法的因为递增啊)
注意:
1.别忘哨兵
2.别忘更新size时还要+1
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
#define lc t[x].ch[0]
#define rc t[x].ch[1]
#define pa t[x].fa
const int N=1e5+,INF=1e9;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
} int n,k;
struct node{
int ch[],fa,size,v,f,mx;
node():fa(){ch[]=ch[]=;}
}t[N];
int sz,root;
inline int wh(int x){return t[pa].ch[]==x;}
inline void update(int x){
t[x].size=t[lc].size+t[rc].size+;
t[x].mx=max(t[x].f,max(t[lc].mx,t[rc].mx));
}
inline void rotate(int x){
int f=t[x].fa,g=t[f].fa,c=wh(x);
if(g) t[g].ch[wh(f)]=x;t[x].fa=g;
t[f].ch[c]=t[x].ch[c^];t[t[f].ch[c]].fa=f;
t[x].ch[c^]=f;t[f].fa=x;
update(f);update(x);
}
inline void splay(int x,int tar){
for(;t[x].fa!=tar;rotate(x))
if(t[pa].fa!=tar) rotate(wh(x)==wh(pa)?pa:x);
if(tar==) root=x;
}
inline int kth(int k){
int x=root,lsize=;
while(x){
int _=lsize+t[lc].size;
if(_<k&&k<=_+) return x;
else if(k<=_) x=lc;
else lsize=_+,x=rc;
}
return ;
}
inline int nw(int v){
sz++;t[sz].v=v;t[sz].size=;
return sz;
}
inline void Sol(int k,int v){//printf("Sol %d %d\n",k,v);
int f=kth(k+);splay(f,);//puts("hi");
int x=kth(k+);splay(x,f);//printf("ran %d %d\n",f,x);
lc=nw(v);t[lc].fa=x;
update(x);update(f);
splay(sz,);
t[sz].f=t[t[sz].ch[]].mx+;update(sz); //printf("f %d %d %d\n",sz,t[sz].ch[0],t[sz].f);
printf("%d\n",t[sz].mx);
}
int main(){
//freopen("in.txt","r",stdin);
n=read();
root=nw();t[root].ch[]=nw(N);t[sz].fa=root;
for(int i=;i<=n;i++){
k=read();
Sol(k,i);
}
}
BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]的更多相关文章
- BZOJ 3173: [Tjoi2013]最长上升子序列 Splay
一眼切~ 重点是按照 $1$~$n$ 的顺序插入每一个数,这样的话就简单了. #include <cstdio> #include <algorithm> #define N ...
- bzoj 3173: [Tjoi2013]最长上升子序列【dp+线段树】
我也不知道为什么把题看成以插入点为结尾的最长生生子序列--还WA了好几次 先把这个序列最后的样子求出来,具体就是倒着做,用线段树维护点数,最开始所有点都是1,然后线段树上二分找到当前数的位置,把这个点 ...
- 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 ...
- 3173: [Tjoi2013]最长上升子序列
原题:http://www.lydsy.com/JudgeOnline/problem.php?id=3173 题解:促使我写这题的动力是,为什么百度遍地是Treap,黑人问号??? 这题可以用线段树 ...
随机推荐
- 关于atom
以前老听别人说atom这款编辑器如何如何的好用,今天特地试了下,结果一不小心将顶部的工具栏给隐藏了,弄了半天都没弄出来.后来就在网上到处寻找帮助,试试这个试试那个,终于弄好了,其实是这样的. 首先在任 ...
- dede添加会员功能听语音
http://jingyan.baidu.com/article/363872ec36d33f6e4ba16fb7.html 其实 dede里面的 会员功能就是圈子模版啦 圈子 安装了 基本上有 1, ...
- 豹哥嵌入式讲堂:ARM Cortex-M开发之文件详解(8)- 镜像文件(.bin/.hex/.s19)
大家好,我是豹哥,猎豹的豹,犀利哥的哥.今天豹哥给大家讲的是嵌入式开发里的image文件(.bin, .hex, .s19). 今天这节课是豹哥<ARM Cortex-M开发之文件详解>主 ...
- @synchronized(self)
@synchronized 的作用是创建一个互斥锁,保证此时没有其它线程对self对象进行修改.这个是objective-c的一个锁定令牌,防止self对象在同一时间内被其 它线程访问,起到线程的保护 ...
- char,varchar,nvarchar,text区别与联系
CHAR,NCHAR 定长,速度快,占空间大,需处理VARCHAR,NVARCHAR,TEXT 不定长,空间小,速度慢,无需处理NCHAR.NVARCHAR.NTEXT处理Unicode码
- shell 学习四十五天---xargs
当 find 产生一个文件列表时,该列表提供给另一个命令有时是很有用的.案例: $touch abc.c erd.c oiy.c $ll ./erd.c ./abc.c ./oiy.c $find - ...
- scrapy_图片下载
需要安装第三方库: 安装 pillow库 pip install -i https://pypi.doubanio.com/simple pillow 如何对图片进行自动下载? 首先明白,图片去哪下? ...
- Java的IO系统
Java IO系统 "对语言设计人员来说,创建好的输入/输出系统是一项特别困难的任务." 由于存在大量不同的设计方案,所以该任务的困难性是很容易证明的.其中最大的 ...
- java IO(五):字节流、字符流的选择规律
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- 04_Javascript初步第三天
事件 内联模型.脚本模型,DOM2级模型 <!--内联模型--> <input type="button" value="bt1" oncli ...