题解:P14175 【MX-X23-T5】向死存魏

· · 题解

挺一道普通的 ds。

常规思路,先开 V 个 set 存储每个数字出现的位置。先考虑从后面放数字要怎么做。对每个位置维护数组 b 表示一个位置包括它自己后面共出现过多少不同的数字,显然这个数组不增,维护数组 ans 表示第 i 个位置的答案,动态维护 last 表示最后一个非无解的位置。令数组当前最后一个位置是 len。

放入数字 x,令其放入后在 lb 位置,找到序列 a 最后一个和 x 相等的位置 pre,b 数组在 [pre+1,lb] 中的所有位置都要加 1,二分找到最后一个满足 b_{pos} = V 的位置 pos,将 [last+1,pos] 中所有值必定因为新加进来的 x 而变得有解,因此将这个区间的答案赋值为 lb,并将 last 更新为 pos。

考虑删除操作。对于 l,r,x,首先特判区间是否存在 x,令 p 为满足 a_p = x 且 p < l 的最大值,不存在则为 0,f 为 [l,r] 区间最后一个 x 的位置。首先特判 f 是不是全局最后一个 x 的位置,如果是,则 p 往后的位置(不包括它自己)都不会有答案,[p+1,len] 区间的 b 也应减 1,因此需要将 last 对 p 取最小值然后对 b 做区间减操作。如果 f 不是全局最后一个 x,则找到 f 后的第一个 x,令其位置为 q,[p+1,q-1] 的答案都要对 q 取最大值。

一系列数组操作都可以用朴素的线段树维护。码风偏史,不喜见谅。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define N 1000010
#define M 500010
#define INF 0x3f3f3f3f
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define cpx complex<double>
#define poly vector<ll>
int len,n,m,v;
namespace segment_tree{
    int lqn = 1;
    struct node{
        int lq,rq,lson,rson;
        int tag = -1;bool tp;// 线段树赋值的类型,0 为取最大,1 为赋值,tag -1 为无
        int add,maxn;// 后面节点的数量,用于线段树二分
    }tree[N<<2];
    void build(int x,int lq,int rq){
        node &now = tree[x];now.lq = lq,now.rq = rq;
        if(now.lq == now.rq){now.tag = INF;return ;}
        int mid = lq + (rq - lq >> 1);now.lson = ++lqn,now.rson = ++lqn;
        build(now.lson,lq,mid),build(now.rson,mid+1,rq);
    }
    void push_down(int x){
        node &now = tree[x];if(now.lq == now.rq)return ;
        node &lson = tree[now.lson],&rson = tree[now.rson];
        if(now.tag != -1){
            if(now.tp == 0)lson.tag = max(now.tag,lson.tag),rson.tag = max(now.tag,rson.tag);
            else lson.tag = now.tag,rson.tag = now.tag,lson.tp = 1,rson.tp = 1;
            now.tag = -1,now.tp = 0;
        }
        lson.add += now.add,lson.maxn += now.add,rson.add += now.add,rson.maxn += now.add;now.add = 0;
    }
    void assign(int x,int lq,int rq,int val){// 区间赋值
        node &now = tree[x];
        if(lq <= now.lq && now.rq <= rq){now.tp = 1,now.tag = val;return ;}
        if(lq > now.rq || now.lq > rq)return ;
        push_down(x);
        assign(now.lson,lq,rq,val),assign(now.rson,lq,rq,val);
    }
    void update(int x,int lq,int rq,int val){// 区间取 max
        node &now = tree[x];
        if(lq <= now.lq && now.rq <= rq){now.tag = max(val,now.tag);return ;}
        if(lq > now.rq || now.lq > rq)return ;
        push_down(x);
        update(now.lson,lq,rq,val),update(now.rson,lq,rq,val);
    }
    void add(int x,int lq,int rq,int k){
        node &now = tree[x];
        if(lq <= now.lq && now.rq <= rq){now.add += k,now.maxn += k;return ;}
        if(lq > now.rq || now.lq > rq)return ;push_down(x);
        add(now.lson,lq,rq,k),add(now.rson,lq,rq,k);now.maxn = max(tree[now.lson].maxn,tree[now.rson].maxn);
    }
    int query(int x,int pos){
        node &now = tree[x];return (now.lq == now.rq?now.tag:(push_down(x),query(pos<=tree[now.lson].rq?now.lson:now.rson,pos)));
    }
    int find(int x){// 在线段树上寻找最后一个 v
        node &now = tree[x];if(now.maxn != v)return 0;
        if(now.lq == now.rq)return now.lq;push_down(x);
        if(tree[now.rson].maxn == v)return find(now.rson);else return find(now.lson);
    }
}using namespace segment_tree;
set<int> st[M];
int lb = 0,last = 0;
void push_back(int x){
    ++lb;auto it = st[x].rbegin();
    int pre = (it == st[x].rend()?0:*it);
    add(1,pre+1,lb,1);int pos = find(1);if(pos != 0 && last != pos)assign(1,last+1,pos,lb),last = pos;st[x].insert(lb);
}

// 主函数
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >> n >> m >> v;len = n+m;build(1,1,len);
    for(int i = 1;i <= n;++i){int x;cin >> x;push_back(x);}
    while(m--){int op;cin >> op;
        if(op == 1){int l,r,x;cin >> l >> r >> x;
            auto lq = st[x].lower_bound(l),rq = st[x].upper_bound(r);if(lq == rq)continue;
            auto rqn = rq;--rqn;int f = *rqn;
            int p,q = (rq == st[x].end()?INF:*rq);if(lq == st[x].begin())p = 0;else{auto tmp = lq;--tmp,p = *tmp;}
            if(q == INF)add(1,p+1,f,-1),last = min(last,p);
            st[x].erase(lq,rq);update(1,p+1,q-1,q);
        }else if(op == 2){int x;cin >> x;push_back(x);}
        else{int pos;cin >> pos;int ans = query(1,pos);cout << (ans == INF?-1:ans) << '\n';}
    }
    return 0;
}