树状数组解决LIS---O(nlogn)

时间:2023-03-09 00:01:42
树状数组解决LIS---O(nlogn)

树状数组解决LIS---O(nlogn)
之前写过二分查找的LIS,现在不怎么记得了,正好用Bit来搞一波。
f[i]表示以a[i]结尾的LIS的长度。
t[x]表示以数值x结尾的LIS的长度。即t[x]=max(f[j]),a[j]==x,j<i。
f[i]=max(t[x])+1,x<a[i]或x<=a[i](这取决于上升还是不下降)。
//绝大多数要要离散化后离线操作

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.17
using namespace std;
int n;
int t[];
int ans;
int f[];
int x;
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} int getmax(int k)
{
int Max=-inf;
for(;k>;k-=(-k)&k)
Max=max(Max,t[k]);
return Max;
} void modify(int k,int Max)
{
for(;k<=;k+=(-k)&k)
t[k]=max(t[k],Max);
} int main()
{
in(n);
For(i,,n)
{
in(x);
f[i]=getmax(x)+;
ans=max(ans,f[i]);
modify(x,f[i]);
}
o(ans);
return ;
}