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,黑人问号??? 这题可以用线段树 ...
随机推荐
- Django在使用Mysql迁移数据库时,会报的错
settings : DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'firstproject', ...
- python多进程,并获取每个进程的返回值
pool = multiprocessing.Pool(processes=10) row = [...] for row in rows: task_id = row[1] img_id = row ...
- 安装gitlab ce
切换到root用户,安装相关依赖 yum install curl policycoreutils openssh-server openssh-clients service sshd restar ...
- win10上面快捷方式如何放到桌面上
1.win+I 快捷方式调出windows设置 ,点击个性化. 2.进入个性化设置界面,点击主题 3.进入主题设置后,点击右边的相关的设置下的桌面图标设置 4.在弹出的窗口中,将我们希望看到 ...
- 07 MySQL之索引原理
一.介绍 为什么有索引:使用索引可快速访问数据库表中的特定信息.索引是对数据库表中一列或多列的值进行排序的一种结构. 作用: 1. 快速查询数据 2. 保证数据的唯一性 3 ...
- 【Activiti】crm与工作流的整合,一个完整的流程实例创建到任务完成的过程
1.建立任务列表页面--根据用户的nickName作为assignee查询其所拥有的任务列表 2.在任务后添加办理按钮 3.点击办理按钮,出现流程办理框,其中详细显示了该任务的相关详细信息,本实例中为 ...
- git取消操作命令
1,移除git add . 的内容 git reset HEAD 2,移除git commit 的内容(commit_A是文件名) git rebase -i commit_A
- vue + mixin混入对象使用
vue提供的混入对象mixin,类似于一个公共的组件,其他任何组件都可以使用它.我更经常的是把它当成一个公共方法来使用 在项目中有些多次使用的data数据,method方法,或者自定义的vue指令都可 ...
- 关于fastJson的几个问题
1.将对象中为null的属性也给序列化出来 可以使用SerializaerFeature实现 JSON.toJSONString(gas, SerializerFeature.WriteMapNull ...
- 利用shell脚本做一个用户登录系统
效果图如下: #!/bin/bash# while truedocat << EOF//======================\\\\| 用户登录系统 |-------------- ...