用树状数组写的最长上升子序列(友好城市),nlogn。

#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;
}
原文地址:https://www.cnblogs.com/kczno1fans/p/7497524.html