bzoj 3173: [Tjoi2013]最长上升子序列(splay)
2016-05-09 11:15
405 查看
3173: [Tjoi2013]最长上升子序列
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1315 Solved: 682
[Submit][Status][Discuss]
Description
给定一个序列,初始为空。现在我们将1到N的数字插入到序列中,每次将一个数字插入到一个特定的位置。每插入一个数字,我们都想知道此时最长上升子序列长度是多少?Input
第一行一个整数N,表示我们要将1到N插入序列中,接下是N个数字,第k个数字Xk,表示我们将k插入到位置Xk(0<=Xk<=k-1,1<=k<=N)Output
N行,第i行表示i插入Xi位置后序列的最长上升子序列的长度是多少。Sample Input
30 0 2
Sample Output
11
2
HINT
100%的数据 n<=100000Source
[Submit][Status][Discuss]
题解:splay维护区间最大值,对于splay中的每个节点,维护两个值,一个是到当前点的最长上升子序列长度,一个是他以及他的子树中最长上升子序列的最大值,更新当前点的最长上升子序列长度时,因为插入时是按照从小到大的方式插入的,所以要用他前驱中的最大值来更新答案,因为他是序列中最大的所以不会影响他后面点的答案。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define N 100003 using namespace std; int n,root; int key ,size ,ch [3],fa ,sz,maxn ,g ; int get(int x) { return ch[fa[x]][1]==x; } void update(int x) { size[x]=size[ch[x][0]]+size[ch[x][1]]+1; maxn[x]=g[x]; maxn[x]=max(maxn[x],maxn[ch[x][0]]); maxn[x]=max(maxn[x],maxn[ch[x][1]]); if (key[x]==1000000000||key[x]==0) maxn[x]=0,g[x]=0; } void rotate(int x) { int y=fa[x]; int z=fa[y]; int which=get(x); if (z) ch[z][ch[z][1]==y]=x; fa[x]=z; ch[y][which]=ch[x][which^1]; fa[ch[y][which]]=y; ch[x][which^1]=y; fa[y]=x; update(y); update(x); } void splay(int x,int tar) { for (int f;(f=fa[x])!=tar;rotate(x)) if (fa[f]!=tar) rotate(get(x)==get(f)?f:x); if (!tar) root=x; } int find(int x) { int now=root; while (true) { if (x<=size[ch[now][0]]) now=ch[now][0]; else { int tmp=size[ch[now][0]]+1; if (tmp==x) return now; x-=tmp; now=ch[now][1]; } } } int main() { scanf("%d",&n); root=++sz; key[sz]=1000000000; size[root]=2; fa[root]=0; ch[root][1]=++sz; key[sz]=0; size[sz]=1; ch[sz][1]=ch[sz][0]=0; fa[sz]=root; for (int i=1;i<=n;i++) { int x; scanf("%d",&x); x++; int aa=find(x); int bb=find(x+1); splay(aa,0); splay(bb,aa); int t=ch[root][1]; ch[t][0]=++sz; fa[sz]=t; key[sz]=i; ch[sz][1]=ch[sz][0]=0; size[sz]=1; splay(t,0); splay(sz,0); g[sz]=maxn[ch[root][0]]+1; update(sz); printf("%d\n",maxn[sz]); } }
相关文章推荐
- failed to open file mysql,导入mysql数据库打不开的解决办法
- spring配置文件详细解读
- C++第5次作业
- [字母控] 层次选择器的详细说明,祝你早日熟练javascript
- 基于微信的域名交易系统(进度汇报之功能实现现状)
- Android--SQLite的使用
- iOS有关截图的操作
- Session简介
- PHP 使用 mcrypt 扩展中的 mcrypt_encrypt() 和 mcrypt_decrypt() 对数据进行加密和解密
- 51单片机中断学习
- C#网络/检测某端口是否被占用的方法
- mdev和udev
- UITabBarController使用
- php 面向对象编程的高级技术
- 【C语言】找出数组中的最大值,最小值,之差
- linux命令--useradd/userdel
- 分隔符和标识符规则
- 浅拷贝与深度拷贝(原型模式)
- android中的Selector的用法---主要是改变ListView和Button控件的默认背景
- 百度网盘爬虫(如何爬取百度网盘)