1. 程式人生 > >用樹狀數組寫的最長上升子序列(友好城市),nlogn。

用樹狀數組寫的最長上升子序列(友好城市),nlogn。

log stream blog sum spa 最長上升子序列 () += def

#include<iostream>
#include<algorithm>
#define maxn 100000
#define lb(x) x&-x
using namespace std;
int f[maxn],n,m,k,tot;
struct st{
    int a,b;
}s[maxn];
void update(int x,int val)
{
    while(x<maxn)
    {
        f[x]=max(f[x],val);
        x+=lb(x);
    }
}
int sum(int x)
{
    int
ans=0; while(x) { ans=max(ans,f[x]); x-=lb(x); } return ans; } bool com(st x,st y) { return x.a<y.a; } main(){ cin>>n; for(int i=1;i<=n;i++) cin>>s[i].a>>s[i].b; sort(s+1,s+1+n,com); for(int i=1;i<=n;i++) { k
=sum(s[i].b); update(s[i].b,k+1); tot=max(tot,k+1); } cout<<tot; }

用樹狀數組寫的最長上升子序列(友好城市),nlogn。