题解:P10756 [COI 2023] Sličnost

· · 题解

首先考虑无修改的情况。

我们枚举排列 p 上的一段区间,区间内的数在排列 q 上存在一些位子,要求交际最大就是能覆盖这些位置。可以线段树维护。

每次修改都重复一遍的时间复杂度为 O(q n \log n),显然不能过。

这时我们发现题目的修改很特别,不是随便的两个位置,而是相邻的两个位置。而这会在上面做法中存在相当多重复计算相同的地方,很浪费。

如下图,当枚举的区间移动时,修改操作会影响到的只有这些。 而实际上会与修改前不同的就是框起来的两个区间。

于是我们可以针对每个区间位置计算在修改后的影响,离线计算方案。而相同的结果在答案序列中是连续的,可以再用一个线段树进行区间修改。最后把线段树上的点下放到答案上输出。时间复杂度为 O(n \log n)。

一些细节:

::::info[参考代码]

#include<bits/stdc++.h>
#define int long long
namespace yycio{
inline int read(){
    char c=getchar();
    int x=0,f=1;
    while(c<'0'||c>'9'){
        if(c=='-')f=-1;
        c=getchar();
    }
    while('0'<=c&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
    return x*f;
}
inline void print(int x){
    if(x==0){putchar('0');return;}
    if(x<0)putchar('-'),x=-x;
    char num[40];
    int len=0;
    while(x)num[len++]='0'+(x%10),x/=10;
    while(len--)putchar(num[len]);
}
};
using namespace yycio;
using namespace std;
const int N=1e5+5;

struct node{
    pair<int,int> del;
    pair<int,int> add;
    int id;
    node(){}
    node(const pair<int,int> del,const pair<int,int> add,const int id):del(del),add(add),id(id){}
};
vector<node> V[N];
int a[N],b[N];
int tag[N];
int ans1[N],ans2[N];

#define ls (p<<1)
#define rs (p<<1|1)
#define m ((l+r)>>1)
struct SGT{
    int mx[N<<2],cnt[N<<2];
    int tag[N<<2];
    void build(const int p,const int l,const int r){
        cnt[p]=r-l+1;
        if(l==r)return;
        build(ls,l,m),build(rs,m+1,r);
    }
    inline void psu(const int& p){
        if(mx[ls]>mx[rs])mx[p]=mx[ls],cnt[p]=cnt[ls];
        else if(mx[rs]>mx[ls])mx[p]=mx[rs],cnt[p]=cnt[rs];
        else mx[p]=mx[ls],cnt[p]=cnt[ls]+cnt[rs];
    }
    inline void psd(const int& p){
        if(tag[p]){
            mx[ls]+=tag[p],tag[ls]+=tag[p];
            mx[rs]+=tag[p],tag[rs]+=tag[p];
            tag[p]=0;
        }
    }
    void add(const int p,const int l,const int r,const int a,const int b){
        if(r<a||l>b)return;
        if(a<=l&&r<=b){++mx[p],++tag[p];return;}
        psd(p);
        add(ls,l,m,a,b),add(rs,m+1,r,a,b);
        psu(p);
    }
    void del(const int p,const int l,const int r,const int a,const int b){
        if(r<a||l>b)return;
        if(a<=l&&r<=b){--mx[p],--tag[p];return;}
        psd(p);
        del(ls,l,m,a,b),del(rs,m+1,r,a,b);
        psu(p);
    }
    void prt(const int p,const int l,const int r){
        if(l==r){printf("%lld:{%lld,%lld}\n",l,mx[p],cnt[p]);return;}
        psd(p);
        prt(ls,l,m),prt(rs,m+1,r);
    }
}sgt;
struct SGT2{
    pair<int,int> tag[N<<2];
    void upd(const int p,const int l,const int r,const int a,const int b,const pair<int,int> x){
        if(r<a||l>b)return;
        if(a<=l&&r<=b){
            if(x.first>tag[p].first)tag[p]=x;
            else if(x.first==tag[p].first)tag[p].second+=x.second;
            return;
        }
        upd(ls,l,m,a,b,x),upd(rs,m+1,r,a,b,x);
    }
    void to_ans(const int p,const int l,const int r,pair<int,int> now){
        if(tag[p].first>now.first)now=tag[p];
        else if(tag[p].first==now.first)now.second+=tag[p].second;
        if(l==r){ans1[l]=now.first,ans2[l]=now.second;return;}
        to_ans(ls,l,m,now),to_ans(rs,m+1,r,now);
    }
}sgt2;
#undef m
#undef ls
#undef rs

signed main(){
    int n=read(),k=read(),q=read();
    if(k==1){
        for(int i=0;i<=q;i++)
            print(1),putchar(' '),print(n),putchar('\n');
        return 0;
    }
    for(int i=1;i<=n;i++)a[i]=read();
    for(int i=1;i<=n;i++)tag[b[i]=read()]=i;
    for(int t=1;t<=n;t++){
        node tt;tt.id=0;
        if(t-k>0)tt.del=make_pair(tag[a[t-k]]-k+1,tag[a[t-k]]);
        else tt.del=make_pair(-1,-1);
        tt.add=make_pair(tag[a[t]]-k+1,tag[a[t]]);
        V[t].emplace_back(tt);
    }
    for(int i=1;i<=q;i++){
        int t=read();
        node tt;tt.id=i;
        tt.del=make_pair(tag[a[t]]-k+1,tag[a[t]]);
        tt.add=make_pair(tag[a[t+1]]-k+1,tag[a[t+1]]);
        V[t].emplace_back(tt);
        if(t+k<=n){
            tt.add=make_pair(tag[a[t]]-k+1,tag[a[t]]);
            tt.del=make_pair(tag[a[t+1]]-k+1,tag[a[t+1]]);
            V[t+k].emplace_back(tt);
        }
        swap(a[t],a[t+1]);
    }
    int m=n-k+1;
    sgt.build(1,1,m);
    for(int i=1;i<=n;i++){
        node lst;
        lst.id=-1;
        for(auto j:V[i]){
            if(lst.id!=-1&&i>=k)sgt2.upd(1,0,q,lst.id,j.id-1,make_pair(sgt.mx[1],sgt.cnt[1]));
            sgt.add(1,1,m,j.add.first,j.add.second);
            sgt.del(1,1,m,j.del.first,j.del.second);
            lst=j;
        }
        if(i>=k)sgt2.upd(1,0,q,lst.id,q,make_pair(sgt.mx[1],sgt.cnt[1]));
        for(auto j:V[i])
            if(j.id){
            sgt.del(1,1,m,j.add.first,j.add.second);
            sgt.add(1,1,m,j.del.first,j.del.second);
        }
    }
    sgt2.to_ans(1,0,q,make_pair(0,0));
    for(int i=0;i<=q;i++)
        print(ans1[i]),putchar(' '),print(ans2[i]),putchar('\n');
    return 0;
}

::::