跳到正文

题解

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

nsp约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年10月7日
收录
2025年12月12日
更新
2026年11月10日
篇幅
约 9 分钟
题目
P14175

版本

  1. v1当前

挺一道普通的 ds。


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

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

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

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

C++
#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;
}
原文
存档评论4 条抓取于