题解 - P4119 [Ynoi2018] 未来日记

· · 题解

给出了一个很暴力的做法。

题意

给定一个长度和值域均 \le{}10^5 的序列,在这个序列上进行以下操作:

解法

考虑值域分块+bitset 维护。

对于修改操作,将 x 对应的 bitset 的对应区间提取,与 x 所对应的值域块上的 bitset 取差,并且并到 y 所对应的 bitset 和 y 所对应的值域块的 bitset 上。

每一次修改都要重新维护一遍整个 bitset 上的前缀和。

对于查询操作,直接在值域块上枚举即可。

注意实现上要比较精细。

AC Code

#include<bits/stdc++.h>
using namespace std;
using ull=unsigned long long;
#define lowbit(x) ((x)&-(x))

inline ull low_mask(int x){ //x in [0,63]
    return -1ull>>(63^x);
}
inline ull high_mask(int x){ //x in [0,63]
    return -1ull<<x;
}

constexpr bool is_debug=0;

void debug(vector<int> v){
    if(!is_debug) return;
    cout<<'[';
    for(int i=0;i<(int)v.size()-1;i++) cout<<v[i]<<',';
    if(v.size()) cout<<v.back();
    cout<<']'<<endl;
}

template<int N> struct bset{
    static constexpr int M=(N>>6)+3;
    ull bmp[M];
    int ps[M];
    bool mod;
    bset(){
        fill(bmp,bmp+M,0);
        fill(ps,ps+M,0);
        mod=0;
    }
    void clear(){
        fill(bmp,bmp+M,0);
        fill(ps,ps+M,0);
        mod=0;
    }
    void update(){
        if(!mod) return;
        ps[0]=__builtin_popcountll(bmp[0]);
        for(int i=1;i<M;i++) ps[i]=ps[i-1]+__builtin_popcountll(bmp[i]);
        mod=0;
    }
    void operator|= (bset& other){
        mod=1;
        for(int i=0;i<M;i++) bmp[i]|=other.bmp[i];
    }
    void operator^= (bset& other){
        mod=1;
        for(int i=0;i<M;i++) bmp[i]^=other.bmp[i];
    }
    int sum(int x){
        update();
        int y=x&63;
        x>>=6;
        return (x?ps[x-1]:0)+__builtin_popcountll(bmp[x]&low_mask(y));
    }
    void copy(int l,int r,bset& other){ //assume to split to an empty bitset
        mod=other.mod=1;
        int x_l=l>>6,y_l=l&63,x_r=r>>6,y_r=r&63;
        if(x_l==x_r){
            other.bmp[x_l]|=bmp[x_l]&low_mask(y_r)&high_mask(y_l);
        }
        else{
            other.bmp[x_l]|=bmp[x_l]&high_mask(y_l);
            std::copy(bmp+x_l+1,bmp+x_r,other.bmp+x_l+1);
            other.bmp[x_r]|=bmp[x_r]&low_mask(y_r);
        }
    }
    void clear(int l,int r){
        mod=1;
        int x_l=l>>6,y_l=l&63,x_r=r>>6,y_r=r&63;
        if(x_l==x_r){
            bmp[x_l]&=~(high_mask(y_l)&low_mask(y_r));
        }
        else{
            bmp[x_l]&=~(high_mask(y_l));
            fill(bmp+x_l+1,bmp+x_r,0);
            bmp[x_r]&=~(low_mask(y_r));
        }
    }
    vector<int> extract(int l,int r){
        vector<int> res;
        int x_l=l>>6,y_l=l&63,x_r=r>>6,y_r=r&63;
        auto sg_extract=[&res](int x,ull tmp){
            for(int y;tmp;){
                y=__builtin_ctzll(tmp);
                res.emplace_back(x<<6|y);
                tmp^=1ull<<y;
            }
        };
        debug({x_l,x_r});
        if(x_l==x_r){
            if(!bmp[x_l]) return res;
            sg_extract(x_l,bmp[x_l]&low_mask(y_r)&high_mask(y_l));
        }
        else{
            sg_extract(x_l,bmp[x_l]&high_mask(y_l));
            for(int i=x_l+1;i<x_r;i++){
                if(!bmp[i]) continue;
                sg_extract(i,bmp[i]);
            }
            sg_extract(x_r,bmp[x_r]&low_mask(y_r));
        }
        return res;
    }
    int size(){
        update();
        return ps[M-1];
    }
    void flip(int x){
        mod=1;
        bmp[x>>6]^=1ull<<(x&63);
    }
    void set(){
        mod=1;
        fill(bmp,bmp+M,-1ull);
    }
    bool operator[] (int x) const{
        return bmp[x>>6]&1ull<<(x&63);
    }
};

constexpr int N=1e5+9,b_rsv=2048,recon_lim=128;
bset<N> b[b_rsv];

list<int> b_alloc;

struct seq_wrapper{
    int pt;
    bool has_recon;
    vector<int> tr;
    seq_wrapper(){
        pt=has_recon=0;
        tr=vector<int>();
    }
    void upgrade(){
        if(has_recon) return;
        has_recon=1;
        pt=b_alloc.front();
        b_alloc.pop_front();
        for(auto& at:tr) b[pt].flip(at);
        tr.clear();
    }
    void degrade(){
        if(!has_recon) return;
        has_recon=0;
        b_alloc.push_back(pt);
        tr=b[pt].extract(0,N-1);
        b[pt].clear();
    }
    ~seq_wrapper(){
        if(has_recon){
            has_recon=0;
            b_alloc.push_back(pt);
            b[pt].clear();
        }
        tr.clear();
    }
    void insert(int x){
        if(has_recon) b[pt].flip(x);
        else{
            tr.insert(upper_bound(tr.begin(),tr.end(),x),x);
            if(tr.size()>=recon_lim) upgrade();
        }
    }
    int sum(int x){
        if(has_recon) return b[pt].sum(x);
        else return upper_bound(tr.begin(),tr.end(),x)-tr.begin();
    }
    int size(){
        if(has_recon) return b[pt].size();
        else return tr.size();
    }
    void copy(int l,int r,seq_wrapper& other){ //split to an empty one
        if(!has_recon){ //brute split
            for(auto& at:tr){
                if(at>=l&&at<=r) other.insert(at);
            }
        }
        else{
            int s=sum(r)-sum(l-1);
            if(s>recon_lim){ //upgrade other one immediately
                other.upgrade();
                b[pt].copy(l,r,b[other.pt]);
            }
            else{
                other.tr=b[pt].extract(l,r);
            }
        }
    }
    void clear(int l,int r){
        if(!has_recon){
            tr.erase(lower_bound(tr.begin(),tr.end(),l),upper_bound(tr.begin(),tr.end(),r));
        }
        else{
            b[pt].clear(l,r);
        }
        if(size()<recon_lim) degrade();
    }
    bool operator[] (int x){
        if(has_recon) return b[pt][x];
        else{
            auto it=lower_bound(tr.begin(),tr.end(),x);
            return it!=tr.end()&&*it==x;
        }
    }
    void differ(seq_wrapper& other){ //assume that *this contains other
        if(has_recon){
            if(other.has_recon){
                b[pt]^=b[other.pt];
            }
            else{
                for(auto& at:other.tr){
                    b[pt].flip(at);
                }
            }
        }
        else{
            for(auto it=tr.begin();it!=tr.end();){
                if(other[*it]) it=tr.erase(it);
                else it++;
            }
        }
        if(size()<recon_lim) degrade();
    }
    void merge(seq_wrapper& other){
        if(other.size()+size()>=recon_lim) upgrade();
        if(has_recon){
            if(other.has_recon){
                b[pt]|=b[other.pt];
            }
            else{
                for(auto& at:other.tr) insert(at);
            }
        }
        else{
            if(other.has_recon){
                //???
            }
            else{
                for(auto& at:other.tr) insert(at);
            }
        }
    }
};

constexpr int outer_move=8;
constexpr int outer_range=(N>>outer_move)+9,inner_range=1<<outer_move,inner_mask=inner_range-1;
int n,m;

void init_b_alloc(){
    for(int i=1;i<b_rsv;i++) b_alloc.push_back(i);
}

seq_wrapper a[N],block[outer_range];

void transport(int l,int r,int x,int y){
    seq_wrapper temp;
    a[x].copy(l,r,temp);
    a[x].clear(l,r);
    int x_b=x>>outer_move,y_b=y>>outer_move; //block x,y
    if(x_b!=y_b){
        block[x_b].differ(temp);
        block[y_b].merge(temp);
    }
    a[y].merge(temp);
}

int find_by_order(int l,int r,int k){
    int x=0,res=0;
    while(x<outer_range){
        int delta=block[x].sum(r)-block[x].sum(l-1);
        if(delta>=k) break;
        k-=delta;
        x++;
    }
    res=x<<outer_move;
    while(res>>outer_move==x){
        int delta=a[res].sum(r)-a[res].sum(l-1);
        if(delta>=k) break;
        k-=delta;
        res++;
    }
    return res;
}

signed main(){
    cin.tie(0)->sync_with_stdio(0);
    init_b_alloc();
    cin>>n>>m;
    for(int i=1,x;i<=n;i++){
        cin>>x;
        a[x].insert(i);
        block[x>>outer_move].insert(i);
    }
    for(int i=1,opt,l,r,x,y;i<=m;i++){
        cin>>opt>>l>>r>>x;
        if(opt==1){
            cin>>y;
            transport(l,r,x,y);
        }
        else{
            cout<<find_by_order(l,r,x)<<'\n';
        }
    }
    return 0;
}

复杂度分析

通过调整块长和 recon_lim 可以做到时间复杂度 O(\frac{n^2}{w}),空间复杂度 O(\frac{n\sqrt{n}}{w})。