树状数组求LIS模板

Posted qieqiemin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树状数组求LIS模板相关的知识,希望对你有一定的参考价值。

如果数组元素较大,需要离散化。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)return b?gcd(b,a%b):a;
ll lcm(ll a,ll b)return a/gcd(a,b)*b;
ll powmod(ll a,ll b,ll MOD)ll ans=1;while(b)if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;return ans;
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll tree[maxn];
int n;
ll a[maxn];
ll lowbit(ll x)

    return x&(-x);

ll query(ll x)

    ll res=0ll;
    while(x)
    
        res=max(res,tree[x]);
        x-=lowbit(x);
       
    return res;


void modify(ll id,ll x)

    while(id<maxn)
    
        tree[id]=max(tree[id],x);
        id+=lowbit(id);
    


int main()

    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\code_stream\\out.txt","w",stdout);
    
    gbtb;
    cin>>n;
    ll ans=0ll;
    ll x;
    repd(i,1,n)
    
        cin>>a[i];
        x=query(a[i])+1;
        ans=max(ans,x);
        modify(a[i],x);
        
    cout<<ans<<endl;
    
    
    return 0;


inline void getInt(int* p) 
    char ch;
    do 
        ch = getchar();
     while (ch == ' ' || ch == '\n');
    if (ch == '-') 
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') 
            *p = *p * 10 - ch + '0';
        
    
    else 
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') 
            *p = *p * 10 + ch - '0';
        
    




以上是关于树状数组求LIS模板的主要内容,如果未能解决你的问题,请参考以下文章

poj1631——树状数组求LIS

大数据LIS (贪心+二分优化/树状数组优化)

bzoj5157: [Tjoi2014]上升子序列(树状数组LIS)

CodeforcesGym 101156E Longest Increasing Subsequences LIS+树状数组

POJ 3378 Crazy Thairs(树状数组+DP)

[noip科普]关于LIS和一类可以用树状数组优化的DP