P5978 [CEOI2018] Global Warming 题解

· · 题解

搬到了校内当打卡题被辱骂素质低下呜呜呜。

显然,如果我修改了区间 [l,r],那么全局的 LIS 应该由三部分组成:

上图中表示了答案 LIS 的组成,黄色表示的是区间减一个值的情况,蓝色表示的是区间加一个值的情况。

直接统计似乎很麻烦,我们观察两种情况能否转化:

由上图(注意箭头表示数值的增减)可以知道:

所以我们只需要考虑让一个前缀减去一个值即可,由于我们希望两个 LIS 更好的对接,所以我们肯定是狠狠地减满 x。

因此可以直接树状数组,维护 F_{i,0/1} 表示当前处理到第 i 项,当前是否使用了减操作,情况下的 LIS。

代码很好写,我好像也就八百字节。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const LL N=2e5+5;
LL n,x,A[N],Ans;
struct BIT
{
    LL t[N*2];
    inline int LB(int x){return x&-x;}
    inline LL Qry(int x){LL Ans=0;for(int i=x;i;i-=LB(i))Ans=max(Ans,t[i]);return Ans;}
    inline void Upd(int x,LL k){for(int i=x;i<N*2;i+=LB(i))t[i]=max(t[i],k);}
}T0,T1;
vector<LL>Lsh;
inline int Gt(LL x)
{
    return lower_bound(Lsh.begin(),Lsh.end(),x)-Lsh.begin()+1;
}
#define pb push_back
int main()
{
    scanf("%lld%lld",&n,&x);
    for(int i=1;i<=n;i++)scanf("%lld",&A[i]),Lsh.pb(A[i]),Lsh.pb(A[i]+x);
    sort(Lsh.begin(),Lsh.end());
    Lsh.erase(unique(Lsh.begin(),Lsh.end()),Lsh.end());
    for(int i=1;i<=n;i++)
    {
        int t0=Gt(A[i]),t1=Gt(A[i]+x);
        LL K0=T0.Qry(t0-1)+1,K1=max(T0.Qry(t1-1),T1.Qry(t0-1))+1;
        T0.Upd(t0,K0),T1.Upd(t0,K1);
        Ans=max({Ans,K0,K1});
    }
    printf("%lld",Ans);
}