因为是从1~n插入的, 慢插入的对之前的没有影响, 所以我们可以用平衡树维护, 弄出最后的序列然后跑LIS就OK了 O(nlogn)

--------------------------------------------------------------------

#include<bits/stdc++.h>
 
#define rep(i, n) for(int i = 0; i < n; ++i)
#define clr(x, c) memset(x, c, sizeof(x))
#define foreach(i, x) for(__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
 
using namespace std;
 
const int maxn = 100009;
 
struct Node {
Node *ch[2], *p;
int s, v;
inline void upd() {
s = ch[0]->s + ch[1]->s + 1;
}
inline void setc(Node* t, int d) {
ch[d] = t;
t->p =this;
}
inline bool d() {
return this == p->ch[1];
}
} pool[maxn], *pt = pool, *root, *null;
 
Node* newNode(int _ = 0) {
pt->v = _; pt->s = 1;
pt->ch[0] = pt->ch[1] = pt->p = null;
return pt++;
}
 
void rot(Node* t) {
Node* p = t->p;
int d = t->d();
p->p->setc(t, p->d());
p->setc(t->ch[d ^ 1], d);
t->setc(p, d ^ 1);
p->upd();
if(p == root) root = t;
}
  
void splay(Node* t, Node* f = null) {
while(t->p != f) {
if(t->p->p != f)
   t->d() != t->p->d() ? rot(t) : rot(t->p);
rot(t);
}
t->upd();
}
 
Node* select(int k) {
for(Node* t = root; ;) {
int s = t->ch[0]->s;
if(k == s) return t;
if(k > s)
   k -= s + 1, t = t->ch[1];
else
   t = t->ch[0];
}
}
 
void init() {
null = newNode();
null->ch[0] = null->ch[1] = null->p = null;
null->s = 0;
root = newNode(maxn);
root->setc(newNode(-maxn), 0);
root->upd();
}
 
int seq[maxn], N = 0, g[maxn], dp[maxn];
 
void dfs(Node* t) {
if(t == null) return;
dfs(t->ch[0]);
if(t->v != maxn && t->v != -maxn) seq[N++] = t->v;
dfs(t->ch[1]);
}
 
int main() {
freopen("test.in", "r", stdin);
init();
int n;
cin >> n;
rep(i, n) {
int v;
scanf("%d", &v);
Node *L = select(v), *R = select(v + 1);
splay(L); splay(R, L);
R->setc(newNode(i), 0);
R->upd(); L->upd();
   g[i] = maxn;
}
dfs(root);
rep(i, n) {
int p = lower_bound(g, g + n, seq[i]) - g;
dp[seq[i]] = p + 1;
g[p] = seq[i];
}
int ans = 0;
rep(i, n) {
   ans = max(ans, dp[i]);
printf("%d\n", ans);
}
return 0;
}

--------------------------------------------------------------------

3173: [Tjoi2013]最长上升子序列

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 806  Solved: 432
[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

3
0 0 2

Sample Output

1
1
2

HINT

100%的数据 n<=100000

Source

BZOJ 3173: [Tjoi2013]最长上升子序列( BST + LIS )的更多相关文章

  1. BZOJ 3173: [Tjoi2013]最长上升子序列

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1524  Solved: 797[Submit][St ...

  2. Bzoj 3173: [Tjoi2013]最长上升子序列 平衡树,Treap,二分,树的序遍历

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1183  Solved: 610[Submit][St ...

  3. BZOJ 3173: [Tjoi2013]最长上升子序列 [splay DP]

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1613  Solved: 839[Submit][St ...

  4. bzoj 3173 [Tjoi2013]最长上升子序列 (treap模拟+lis)

    [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2213  Solved: 1119[Submit][Status] ...

  5. BZOJ 3173 [Tjoi2013] 最长上升子序列 解题报告

    这个题感觉比较简单,但却比较容易想残.. 我不会用树状数组求这个原排列,于是我只好用线段树...毕竟 Gromah 果弱马. 我们可以直接依次求出原排列的元素,每次找到最小并且最靠右的那个元素,假设这 ...

  6. BZOJ 3173: [Tjoi2013]最长上升子序列 (线段树+BIT)

    先用线段树预处理出每个数最终的位置.然后用BIT维护最长上升子序列就行了. 用线段树O(nlogn)O(nlogn)O(nlogn)预处理就直接倒着做,每次删去对应位置的数.具体看代码 CODE #i ...

  7. bzoj 3173: [Tjoi2013]最长上升子序列【dp+线段树】

    我也不知道为什么把题看成以插入点为结尾的最长生生子序列--还WA了好几次 先把这个序列最后的样子求出来,具体就是倒着做,用线段树维护点数,最开始所有点都是1,然后线段树上二分找到当前数的位置,把这个点 ...

  8. BZOJ 3173: [Tjoi2013]最长上升子序列 Splay

    一眼切~ 重点是按照 $1$~$n$ 的顺序插入每一个数,这样的话就简单了. #include <cstdio> #include <algorithm> #define N ...

  9. bzoj3173[Tjoi2013]最长上升子序列 平衡树+lis

    3173: [Tjoi2013]最长上升子序列 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2253  Solved: 1136[Submit][S ...

随机推荐

  1. js去除首尾空格

    简单的:str = jQuery.trim(str); var temp = " aa b "; console.log("cc" + temp); temp ...

  2. System.Web.Security 在winform中是什么命名空间呢

    des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStorin ...

  3. Introduction to Json

    什么是Json 是Javascript·对象的一种表示,属于轻量级数据,它比XMl小,快,易解析 作用: 用于存储和交换(转换)信息的语言,还可以将各种数据类型放在json中并进行数据传输 整理的章节 ...

  4. A + B Problem II 大数加法

    题目描述: Input The first line of the input contains an integer T(1<=T<=20) which means the number ...

  5. struts2 DMI问题

    最新开始学习struts2,在官网上下载的最新的struts2(2.3.15.2), jar包,在使用动态方法调用的时候老是报错,错误代码如下HTTP Status 404 - There is no ...

  6. linux禁ping和允许ping的方法

    一.系统禁止ping [root@linuxzgf ~]# echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all 二.系统允许ping [root@li ...

  7. perl post 带上请求头

    my $url='https://www.zjcap.cn/business/dispatch_post.do?action=submitAdminLogin'; my $res = $ua-> ...

  8. C# Setup package Uninstaller

    安裝的部分就不介紹了,網上一搜一大堆,這裡只介紹下卸載的部分. 1.在C:\Windows\System32 目录下找到 msiexec.exe 拷贝到相应的地方,可修改名称为 Uninstall.e ...

  9. 【centos6 , 6】linux 查看帮助文档:

    1. 使用   命令 -h 或 命令 --help ,  例: ls -h 2. man命令  : man  命令              例:man ls 3.info命令:           ...

  10. ZOJ 3329 One Person Game 【概率DP,求期望】

    题意:有三个骰子,分别有k1,k2,k3个面. 每次掷骰子,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和. 当分数大于n时结束.求游戏的期望步数.初始分数为0 设dp[i]表示达到 ...