跳到正文

题解

题解:P2075 区间 LIS

yangzichen1203约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年11月4日
收录
2026年11月5日
更新
2026年11月21日
篇幅
约 8 分钟
获赞
1
题目
P2075

版本

  1. v1当前

O(nlog⁡2+qlog⁡n)O(n\log^2+q\log n) 做法。

设答案为 f(l,r)f(l,r),根据 https://www.luogu.com.cn/problem/P9109 的结论:

f(l−1,r)−f(l,r)=[r≤xl]=1−[r>xl]f(l-1,r)-f(l,r)=[r\le x_l]=1-[r>x_l]

只要预处理出 xx 序列,就可以二维数点 O(qlog⁡n)O(q\log n) 解决原问题。

把 (l,r)(l,r) 放到二维平面上考虑,每个左端点位于点 (p,xp−1)(p,x_p-1),其对一个左上角的矩形产生 11 的贡献,所以对于 g(l,r)=r−l+1−f(l,r)g(l,r)=r-l+1-f(l,r),其满足四边形不等式,是具有凸性的。

对值域分治,求出值域两侧点集,然后在值域两侧补上对侧的位置。对于任意一个序列区间 (l,r)(l,r),其一定是左侧在值域小的一侧,右侧在值域大的一侧,需要求

min⁡k∈(l,r]C(l,k]+C(k,r]\min_{k\in(l,r]}C_{(l,k]}+C_{(k,r]}

分界位置 kk 关于 (l,r)(l,r) 具有决策单调性。对离散化后的位置再次分治,值域小侧分界线为横轴方向,值域大侧分界线为纵轴方向,然后补上分界线另一侧对分界线内的贡献。

现在只需要求两个平面取 min⁡\min 后产生的新点位置,即二维差分后不为 00 的位置。发现这些位置同样具有单调性,可以双指针求得。

归并套归并的时间复杂度显然为 O(nlog⁡2n)O(n\log^2n),总时间复杂度 O(nlog⁡2n+qlog⁡n)O(n\log^2n+q\log n)。

空间复杂度 O(n+m)O(n+m),轻松打爆 Retroactive Priority Queue。

提交记录:https://www.luogu.com.cn/record/308003391,轻松通过,甚至比大部分 O(nnlog⁡n)O(n\sqrt n\log n) 快。

Code
C++
#include<bits/stdc++.h>
#define For(i,j,k) for(int i=(j);i<=(k);i++)
#define dFor(i,j,k) for(int i=(j);i>=(k);i--)
using namespace std;
#define MAXN 100005
#define INF (1<<30)
int n,q,a[MAXN],pos[MAXN];
struct Node{
    int u,v;
};
int lv[MAXN],rv[MAXN],lu[MAXN],ru[MAXN];
vector<Node> merge(int L,int R,const vector<Node> &vl,const vector<Node> &vr,const vector<int> &now){
    if(vl.empty()||vr.empty()){
        return {};
    }
    if(L==R){
        return {{vl[0].u,vr[0].v}};
    }
    int mid=(L+R)>>1;
    int pos=now[mid];
    vector<Node> al,ar,bl,br;
    for(auto [u,v]:vl){
        (v<=pos?al:ar).push_back({u,v});
    }
    for(auto [u,v]:vr){
        (u<=pos?bl:br).push_back({u,v});
    }
    vector<Node> tl=merge(L,mid,al,bl,now);
    vector<Node> tr=merge(mid+1,R,ar,br,now);
    for(auto [u,v]:vl){
        lv[u]=INF;
        rv[u]=(v<=pos?0:INF);
    }
    for(auto [u,v]:vr){
        lu[v]=(u>pos?INF:0);
        ru[v]=0;
    }
    for(auto [u,v]:tl){
        lv[u]=v;
        lu[v]=u;
    }
    for(auto [u,v]:tr){
        rv[u]=v;
        ru[v]=u;
    }
    int i=0,j=vr.size();
    int cl=tl.size()+br.size();
    int cr=tr.size()+al.size();
    vector<Node> ans;
    for(Node p:vl){
        while(j>0){
            int v=vr[j-1].v;
            int ncl=cl-(lu[v]>i);
            int ncr=cr-(ru[v]>i);
            if(ncl<ncr) break ;
            cl=ncl;
            cr=ncr;
            j--;
        }
        int col=(j==0?0:vr[j-1].v);
        int fst=(cr<=cl?col:INF);
        int v=(p.v<=pos?min(lv[p.u],fst):max(rv[p.u],fst));
        if(v!=INF){
            ans.push_back({p.u,v});
        }
        cl-=(lv[p.u]<=col);
        cr-=(rv[p.u]<=col);
        i=p.u;
    }
    return ans;
}
vector<Node> solve(int L,int R){
    if(L==R){
        return {};
    }
    int mid=(L+R)>>1;
    vector<Node> vl=solve(L,mid);
    vector<Node> vr=solve(mid+1,R);
    For(i,mid+1,R){
        vl.push_back({pos[i],pos[i]});
    }
    For(i,L,mid){
        vr.push_back({pos[i],pos[i]});
    }
    sort(vl.begin(),vl.end(),[&](Node a,Node b){
        return a.u<b.u;
    });
    sort(vr.begin(),vr.end(),[&](Node a,Node b){
        return a.v<b.v;
    });
    vector<int> now;
    For(i,L,R){
        now.push_back(pos[i]);
    }
    sort(now.begin(),now.end());
    return merge(0,R-L,vl,vr,now);
}
vector<pair<int,int>> e[MAXN];
int ans[MAXN];
namespace BIT{
    int tr[MAXN];
    void add(int x,int k){
        while(x){
            tr[x]+=k;
            x-=x&-x;
        }
    }
    int query(int x){
        int ans=0;
        while(x<=n){
            ans+=tr[x];
            x+=x&-x;
        }
        return ans;
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>q;
    For(i,1,n){
        cin>>a[i];
        pos[a[i]]=i;
    }
    vector<Node> p=solve(1,n);
    for(auto [u,v]:p){
        e[v].push_back({u,0});
    }
    For(i,1,q){
        int l,r;
        cin>>l>>r;
        e[r].push_back({l,i});
        ans[i]=r-l+1;
    }
    For(i,1,n){
        for(auto [l,id]:e[i]){
            if(id==0){
                BIT::add(l,1);
            }else{
                ans[id]-=BIT::query(l);
            }
        }
    }
    For(i,1,q){
        cout<<ans[i]<<'\n';
    }
    return 0;
}
原文
存档评论4 条抓取于