信封的选择

· · 题解

直接套用 B1 最多可以得到 10 分,但我希望大家可以借此启发出正解。

算法 1

考虑对于某个纸条被操作的次数阈值分治,设阈值为 B。

若操作数 >B,直接套用简单版的做法,单个纸条这样做的复杂度是 \mathcal{O}(n),这部分复杂度为 \mathcal{O}\left(\dfrac{nm}{B}\right)。

若操作数 \le B,由于纸条上的折点只有 \mathcal{O}(B) 个,所以厚度构成 \mathcal{O}(B) 个连续段,暴力维护连续段进行折叠即可,复杂度为 \mathcal{O}(mB)。

取 B=\mathcal{O}(\sqrt{n}),总复杂度为 \mathcal{O}\big(m\sqrt{n}\big)。空间复杂度为 \mathcal{O}\big(n+m\big)。

期望得分:50 分。

算法 2

发现连续段数量是 \mathcal{O}(\sqrt{n}) 的。

一个粗糙的证明:可以感觉到,我们希望厚度的种类数是 \mathcal{O}(1) 的,能构造到较多段数的情况是一个波浪形的结构,而构造出一个波浪的操作次数是 \mathcal{O}(\min\{i,n-i\}) 的,这样我们只能造出 \mathcal{O}(\sqrt{n}) 个段了。

直接维护这些连续段,总复杂度为 \mathcal{O}\big(m\sqrt{n}\big)。空间复杂度为 \mathcal{O}\big(m+\sqrt{n}\big)。

期望得分:50 分。

::::info[参考代码]

#include<bits/stdc++.h>
#define rd read()
#define gc getchar()
using namespace std;
inline int read(){
    register int x=0,ss=1,s=gc;
    while(!isdigit(s)&&s!='-')s=gc;if(s=='-')ss=-1,s=gc;
    while(isdigit(s))x=(x<<1)+(x<<3)+(s^48),s=gc;
    return ss*x;
}
const int N=300005;
int n,m,cnt,tot,res,l,r,t2,pl,pr,ans[N];map<int,int> mp;
struct duan{
    int l,r,x;
    inline bool operator <(const duan &o)const{
        return l<o.l;
    }
}d[N],D[N],tmp[N];
struct query{
    int p,L,id;
};vector<query> q[N];
inline int gt(int x){
    if(!mp[x])mp[x]=++cnt;return mp[x];
}
inline void chk(int &x,int y){
    x=x<y?y:x;
}
inline void merge(){
    int i=pl,p=1;t2=0;
    while(i<=pr&&p<=tot){
        if(d[i].r<D[p].l){
            tmp[++t2]=d[i++];continue;}
        if(D[p].r<d[i].l){
            tmp[++t2]=D[p++];continue;}
        if(d[i].l<D[p].l)
            tmp[++t2]={d[i].l,D[p].l-1,d[i].x},d[i].l=D[p].l;
        if(D[p].l<d[i].l)
            tmp[++t2]={D[p].l,d[i].l-1,D[p].x},D[p].l=d[i].l;
        int L=max(d[i].l,D[p].l),R=min(d[i].r,D[p].r);
        tmp[++t2]={L,R,d[i].x+D[p].x};
        if(d[i].r==R)++i;else d[i].l=R+1;
        if(D[p].r==R)++p;else D[p].l=R+1;
    }
    while(i<=pr)tmp[++t2]=d[i++];
    while(p<=tot)tmp[++t2]=D[p++];
    pl=1,pr=0;
    for(int j=1;j<=t2;++j){
        if(tmp[j].l>tmp[j].r)continue;
        if(pr>=pl&&d[pr].r+1==tmp[j].l&&d[pr].x==tmp[j].x)d[pr].r=tmp[j].r;
        else d[++pr]=tmp[j];
        chk(res,d[pr].x);
    }
}
signed main(){
    n=rd,m=rd;
    for(int i=1,t,p,l;i<=m;++i)
        t=rd,p=rd,l=rd,q[gt(t)].push_back({p,l,i});
    for(int i=1;i<=cnt;++i){
        d[pl=pr=0]={0,n-1,1},l=0,r=n-1,res=1;
        for(auto [p,L,id]:q[i]){
            int k=l+L-1;tot=0;
            if(p==1){
                for(;pl<=pr;++pl){
                    if(d[pl].r<k){
                        D[++tot]={2*k-d[pl].r+1,2*k-d[pl].l+1,d[pl].x};
                    }else{
                        if(d[pl].l<=k){
                            D[++tot]={k+1,2*k-d[pl].l+1,d[pl].x};
                            d[pl].l=k+1;
                        }
                        break;
                    }
                }
                reverse(D+1,D+tot+1);
                if(pl>pr){
                    pl=1,pr=0;
                    for(int i=1;i<=tot;++i)d[++pr]=D[i];
                }else if(tot)merge();
            }else{
                for(;pl<=pr;--pr){
                    if(d[pr].l>k){
                        D[++tot]={2*k-d[pr].r+1,2*k-d[pr].l+1,d[pr].x};
                    }else{
                        if(d[pr].r>k){
                            D[++tot]={2*k-d[pr].r+1,k,d[pr].x};
                            d[pr].r=k;
                        }
                        break;
                    }
                }
                if(pl>pr){
                    pl=1,pr=0;
                    for(int i=1;i<=tot;++i)d[++pr]=D[i];
                }else if(tot)merge();
            }
            l=d[pl].l,r=d[pr].r,ans[id]=res;
        }
    }
    for(int i=1;i<=m;++i)cout<<ans[i]<<'\n';return 0;
}

::::