[BZOJ 3173] [TJOI 2013] increase in the longest sequence (fhq treap)
Face questions
Given a sequence, initially empty. Now we will numbers 1 to N is inserted into the sequence, each time a digit is inserted into a specific position. Each insert a number, we want to know at this time is the length of the longest subsequence rise much?
analysis
This problem has several important properties:
The first property is that the insertion of the magnitude of the number is increasing . We inserted after the i-th answer, LIS length of the current sequence number for the end of the maximum value, and according to the current sequence number in [. 1, i] inside, can maintain the value of v is in the end the LIS length len [i], then after the i th answer is inserted \ (max (len [j] ) (j \ in [1, i]) \)
The second property is that the number of insertion does not affect the length LIS number is inserted before the end . This sentence a bit abstract, let's give an example. Ends of two sequence numbers are inserted into {1,2} to the end of the LIS is a length of 2 2. We then inserted in front of 2 3, into a sequence {1,3,2}, because 3> 2, it does not affect the length of the end 2 of the LIS, LIS length of 2 therefore remains at the end of 2. If the insert 2 in the back, apparently can not be the end of the new LIS 2, no effect on the length of the end 2 of the LIS.
Thus, as long as we use a data processing structure is inserted into the final sequence after n times, the sequence of the LIS run time, to determine the end of the sequence length v, and the answer determined at max prefix. Here I chose fhq treap to maintain the sequence, and then use the template () \ O (n \ log n) \ seek LIS algorithm.
Code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define maxn 100000
using namespace std;
struct fhq_treap{
#define lson(x) tree[x].ls
#define rson(x) tree[x].rs
struct node{
int ls;
int rs;
int val;
int sz;
int dat;
}tree[maxn+5];
int root;
int ptr;
int New(int val){
ptr++;
tree[ptr].val=val;
tree[ptr].dat=rand();
tree[ptr].sz=1;
return ptr;
}
void push_up(int x){
tree[x].sz=tree[lson(x)].sz+tree[rson(x)].sz+1;
}
int merge(int x,int y){
if(!x||!y) return x+y;
if(tree[x].dat<tree[y].dat){
tree[y].ls=merge(x,tree[y].ls);
push_up(y);
return y;
}else{
tree[x].rs=merge(tree[x].rs,y);
push_up(x);
return x;
}
}
void split(int now,int k,int &x,int &y){
if(now==0){
x=y=0;
return;
}
if(k<=tree[lson(now)].sz){
y=now;
split(tree[now].ls,k,x,tree[y].ls);
}else{
x=now;
split(tree[now].rs,k-tree[lson(now)].sz-1,tree[x].rs,y);
}
push_up(now);
}
void insert(int val,int pos){
int x,y;
split(root,pos-1,x,y);
root=merge(merge(x,New(val)),y);
}
void print(int x,int *out,int &sz){
if(x==0) return;
print(lson(x),out,sz);
out[++sz]=tree[x].val;
print(rson(x),out,sz);
}
}T;
int n;
int sz=0;
int a[maxn+5];
int top=0;
int s[maxn+5];
int len[maxn+5];//以值i为结尾的LIS长度
void get_lis(){
for(int i=1;i<=n;i++){
if(a[i]>s[top]){
s[++top]=a[i];
len[a[i]]=top;
}else{
int tmp=lower_bound(s+1,s+1+top,a[i])-s;
s[tmp]=a[i];
len[a[i]]=tmp;
}
}
}
int main(){
int x;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&x);
x++;
T.insert(i,x);
}
T.print(T.root,a,sz);
// for(int i=1;i<=sz;i++) printf("%d ",a[i]);
get_lis();
int ans=0;
for(int i=1;i<=n;i++){
ans=max(ans,len[i]);
printf("%d\n",ans);
//第i个数插入的时候,序列里只有1~i的数,把以它们为结尾的lis长度取max即可
}
}