求助

P1439 【模板】最长公共子序列

我没有开挂 @ 2018-11-02 16:33:06

rt,wa了一个点,离正确答案只差1,用线段树求的最长不下降子序列(本来是听说可以用树状数组,然而本蒟蒻不会,就只能打线段树了QAQ)

#include <bits/stdc++.h>

#define MAXN 100086
#define re register
#define ls o<<1
#define rs o<<1|1

using namespace std;

struct node
{
    int w,id;
}b[MAXN];

int num[MAXN],maps[MAXN],s[MAXN<<2],tag[MAXN<<2];
int n,ans;

inline int read()
{
    re int res=0;re bool f=1;
    re char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-') f=0;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        res=(res<<3)+(res<<1)+(ch&15);
        ch=getchar();
    }
    return f?res:-res;
}

inline bool cmp(const node &a,const node &b)
{
    return a.w<b.w;
}

inline void f(re int l,re int r,re int c,re int o)
{
    if(s[o]<=c)
    {
        s[o]=c;
        tag[o]=c;
    }
}

inline void pushup(re int o)
{
    s[o]=max(s[ls],s[rs]);
}

inline void pushdown(re int l,re int r,re int o)
{
    if(!tag[o]) return;
    re int mid=l+r>>1;
    f(l,mid,tag[o],ls);
    f(mid+1,r,tag[o],rs);
    tag[o]=0;
}

void update(re int nl,re int nr,re int l,re int r,re int o,re int c)
{
    if(nl<=l&&r<=nr)
    {
        if(s[o]<=c)
        {
            s[o]=c;
            tag[o]=c;
        }
        return;
    }
    pushdown(l,r,o);
    re int mid=l+r>>1;
    if(mid>=nl) update(nl,nr,l,mid,ls,c);
    if(mid<nr) update(nl,nr,mid+1,r,rs,c);
    pushup(o);
}

int query(re int nl,re int nr,re int l,re int r,re int o)
{
    if(nl<=l&&r<=nr) return s[o];
    re int maxn=-1;
    pushdown(l,r,o);
    re int mid=l+r>>1;
    if(mid>=nl) maxn=max(maxn,query(nl,nr,l,mid,ls));
    if(mid<nr) maxn=max(maxn,query(nl,nr,mid+1,r,rs));
    return maxn;
}

signed main()
{
    n=read();
    for(re int i=1;i<=n;++i)
        maps[read()]=i;
    for(re int i=1;i<=n;++i)
        b[i].w=maps[read()],b[i].id=i;
    sort(b+1,b+n+1,cmp);
    for(re int i=1;i<=n;++i)
    {
        re int maxn=query(1,b[i].id,1,n,1);
        update(b[i].id,n,1,n,1,++maxn);
        ans=max(ans,maxn);
    }
    printf("%d\n",ans);
    return 0;
}

by BuXiangJuanLe @ 2018-11-02 16:34:12

你开个挂就会了


by 哔哩哔哩 @ 2018-11-02 16:38:41

你开个挂就会了


by 我没有开挂 @ 2018-11-02 16:43:04

我怎么能开挂呢?Ctrl c和Ctrl v真好用


by ALSaoBen @ 2018-11-02 16:48:47

你开个挂就会了


by ALSaoBen @ 2018-11-02 16:53:17

``

`cpp 你开个挂就会了


by ALSaoBen @ 2018-11-02 17:16:58

那是你太强了

|