BZOJ_3173_[Tjoi2013]最长上升子序列_splay
BZOJ_3173_[Tjoi2013]最长上升子序列_splay
Description
Input
Output
Sample Input
0 0 2
Sample Output
1
2
HINT
100%的数据 n<=100000
分析:由于数字是升序插入的,因此每次操作只会对它一个数产生影响。
问题转化为:插入一个数,查询某个位置前面的最大值。
直接用平衡树维护即可。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define N 100050
#define ls ch[p][0]
#define rs ch[p][1]
#define get(x) (ch[f[x]][1]==x)
int f[N],rt,ch[N][2],siz[N],cnt,val[N],mx[N],n,ans;
void pushup(int p) {
if(p) {
siz[p]=siz[ls]+siz[rs]+1;
mx[p]=max(max(mx[ls],mx[rs]),val[p]);
}
}
void rotate(int x) {
int y=f[x],z=f[y],k=get(x);
ch[y][k]=ch[x][!k]; f[ch[y][k]]=y;
ch[x][!k]=y; f[y]=x; f[x]=z;
if(z) ch[z][ch[z][1]==y]=x;
pushup(y); pushup(x);
if(rt==y) rt=x;
}
void splay(int x,int y) {
for(int fa;(fa=f[x])!=y;rotate(x))
if(f[fa]!=y)
rotate(get(fa)==get(x)?fa:x);
}
int find(int x) {
int p=rt;
while(1) {
if(x<=siz[ls]) p=ls;
else {
x-=siz[ls]+1;
if(!x) return p;
p=rs;
}
}
}
int main() {
scanf("%d",&n);
int i,x,y,p;
for(i=1;i<=n;i++) {
scanf("%d",&x);
if(x==0) {
if(!rt) {
rt=i; val[i]=1; siz[i]=1; pushup(i);
}
else {
p=find(1);
splay(p,0);
val[i]=1; siz[i]=1; f[i]=p; ls=i;
pushup(i); pushup(p);
}
}else if(x==i-1) {
p=find(i-1);
splay(p,0);
val[i]=mx[p]+1; siz[i]=1; f[i]=p; rs=i;
pushup(i); pushup(p);
}else {
int tmp=x;
x=find(x); p=find(tmp+1);
splay(x,0); splay(p,x);
val[i]=max(mx[ch[x][0]],val[x])+1; siz[i]=1; f[i]=p; ls=i;
pushup(i); pushup(p); pushup(x);
}
ans=max(ans,val[i]);
printf("%d\n",ans);
}
}
BZOJ_3173_[Tjoi2013]最长上升子序列_splay的更多相关文章
- [BZOJ3173][Tjoi2013]最长上升子序列
[BZOJ3173][Tjoi2013]最长上升子序列 试题描述 给定一个序列,初始为空.现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置.每插入一个数字,我们都想知道此时最长上 ...
- 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]最长上升子序列( BST + LIS )
因为是从1~n插入的, 慢插入的对之前的没有影响, 所以我们可以用平衡树维护, 弄出最后的序列然后跑LIS就OK了 O(nlogn) --------------------------------- ...
- BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]
3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1613 Solved: 839[Submit][St ...
- bzoj3173[Tjoi2013]最长上升子序列 平衡树+lis
3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2253 Solved: 1136[Submit][S ...
- 【LG4309】【BZOJ3173】[TJOI2013]最长上升子序列
[LG4309][BZOJ3173][TJOI2013]最长上升子序列 题面 洛谷 BZOJ 题解 插入操作显然用平衡树就行了 然后因为后面的插入对前面的操作无影响 就直接在插入完的序列上用树状数组求 ...
- P4309 [TJOI2013]最长上升子序列
题目 P4309 [TJOI2013]最长上升子序列 做法 最长上升序列的求法肯定是烂大街了 水题是肯定的,确定出序列的位置然后套个树状数组就好了(强制在线的话改成线段树维护前缀最值也行) 所以说这题 ...
- bzoj 3173 [Tjoi2013]最长上升子序列 (treap模拟+lis)
[Tjoi2013]最长上升子序列 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2213 Solved: 1119[Submit][Status] ...
随机推荐
- 电商网站开发记录(三) Spring的引入,以及配置详解
1.web.xml配置注解<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=& ...
- scons脚本示例
import os def list_dir(dir): all_dirs = [] for root, dirs, files in os.walk('./', True): for name in ...
- 一篇文章带你了解Cloud Native
背景 Cloud Native表面看起来比较容易理解,但是细思好像又有些模糊不清:Cloud Native和Cloud关系是啥?它用来解决什么问题?它是一个新技术还是一个新的方法?什么样的APP符合“ ...
- PHP 中的 __FILE__ 和__DIR__常量
__DIR__ :当前内容写在哪个文件就显示这个文件目录 __FILE__ : 当前内容写在哪个文件就显示这个文件目录+文件名 比如文件 b.php 包含如下内容: <?php $basedir ...
- Spring JTA multiple resource transactions in Tomcat with Atomikos example
http://www.byteslounge.com/tutorials/spring-jta-multiple-resource-transactions-in-tomcat-with-atomik ...
- redis安装、使用
官网:http://redis.io/ github地址:https://github.com/antirez/redis 简介: redis是一个key-value存储系统.和Mem ...
- ArcticCore重构-问题列表1
基于官方arc-stable-9c57d86f66be,AUTOSAR版本3.1.5 基本问题 Arctic Core中的代码组织有很多有待改进的地方,这里先提出几点: 1. 头文件引用混乱,所有头文 ...
- htmlparser 学习
htmlparser 学习系列 htmlparser 使用法使用与详解
- Cookie、Session、jsp、EL、JSTL
会话技术 Cookie Session 从访问一个站点,到关闭不继续访问 称为一次会话过程.会话技术就是记录本次会话中客户端的状态与数据的. 会话技术分为cookie.session. cooki ...
- 从 源码 谈谈 redux compose
compose,英文意思 组成,构成. 它的作用也是通过一系列的骚操作,实现任意的.多种的.不同的功能模块的组合,用来加强组件. 看看源码 https://github.com/reactjs/red ...