P6617 查找 Search 题解

· · 题解

数据结构部分不难,重点在修改的讨论上,有点像 P5278。

定义一下两种关系:

首先思考没有修改操作怎么做:对于每一个数记录 v_i 表示它的补前驱。询问区间 [l,r],就转化为询问 [l,r] 中 v 的最大值是否大于等于 l。这样就是一个 RMQ 问题了。

但是有修改操作后,可能有很多数的补前驱发生变化。进一步转化 v_i:对于一个数 b,若它的补前驱的补后驱仍是这个数,(即 b 的补前驱要在 b 的等前驱后面)我们才维护 v_i,否则让 v_i=0,显然此时仍然最优。

这样需要修改 v 值的数就能够大大减少。

稍微分析一下,若改变 a_x 的值为 y,则在位置 x 上,只有 a_x 的补后驱、等后驱,y 的补后驱、等后驱和 x 的 v 值发生改变。

用值域个 set 维护不同数出现的位置,即可修改 v 值(用 b 的补前驱要在 b 的等前驱后面这个性质维护,将同一件事重复五次就好了)。

#include<bits/stdc++.h>
#define lson id * 2
#define rson id * 2 + 1
#define mid (l + r) / 2
using namespace std;
const int N = 500005;
set<int> s[N];
set<int>::iterator it; 
int n,m,w,a[N],v[N],Max[4*N];
int rd(){
    int x = 0;char ch = getchar();
    while(ch < '0' || ch > '9')ch = getchar();
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x;
}
void build(int id,int l,int r){//平凡的线段树建树
    if(l == r){
        Max[id] = v[l];
        return;
    }
    build(lson,l,mid);
    build(rson,mid + 1,r);
    Max[id] = max(Max[lson],Max[rson]);
}
void update(int id,int l,int r,int x,int k){//平凡的线段树单点修改
    if(l == r){
        Max[id] = k;
        return;
    }
    if(x <= mid)
        update(lson,l,mid,x,k);
    else
        update(rson,mid + 1,r,x,k);
    Max[id] = max(Max[lson],Max[rson]);
}
int query(int id,int l,int r,int x,int y){//平凡的线段树询问区间最值
    if(x <= l && r <= y)
        return Max[id];
    int res = 0;
    if(x <= mid)
        res = max(res,query(lson,l,mid,x,y));
    if(y > mid)
        res = max(res,query(rson,mid + 1,r,x,y));
    return res;
}
void calc(int x,int y){//修改v值
    set<int>::iterator i = s[y].find(x);//i是y的等前驱
    set<int>::iterator j = s[w - y].upper_bound(x - 1);//j是y的补前驱
    if(j != s[w - y].begin()){//判断j的位置是否大于等于i,注意特判等前驱或补前驱不存在的情况
        if(i == s[y].begin())
            j--,v[x] = *j,update(1,1,n,x,*j);
        else if(*(--i) <= *(--j))
            v[x] = *j,update(1,1,n,x,*j);
        else v[x] = 0,update(1,1,n,x,0);
    }
    else v[x] = 0,update(1,1,n,x,0);
}
int main(){
    n = rd(),m = rd(),w = rd();
    for(int i = 1;i <= n;i++){
        a[i] = rd();
        if(!s[w - a[i]].empty() && (s[a[i]].empty() || *(--s[w - a[i]].end()) >= *(--s[a[i]].end())))//求一开始的v值
            v[i] = *(--s[w - a[i]].end());
        s[a[i]].insert(i);
    }
    build(1,1,n);
    int op,x,y,t = 0;
    while(m--){
        op = rd();
        if(op == 1){
            x = rd(),y = rd();
            set<int>::iterator k = ++s[a[x]].find(x);
            set<int>::iterator l = s[w - a[x]].upper_bound(x);//k是a[x]的等后驱,l是a[x]的补后驱
            s[a[x]].erase(x),s[y].insert(x);//set修改不能调换位置
            if(k != s[a[x]].end())//修改k
                calc(*k,a[x]);
            if(l != s[w - a[x]].end())//修改l
                calc(*l,w - a[x]);
            a[x] = y;
            k = ++s[a[x]].find(x);
            l = s[w - a[x]].upper_bound(x);//k是y的等后驱,l是y的补后驱
            if(k != s[a[x]].end())
                calc(*k,a[x]);
            if(l != s[w - a[x]].end())
                calc(*l,w - a[x]);
            calc(x,a[x]);
        }
        else{
            x = rd() ^ t,y = rd() ^ t;
            if(query(1,1,n,x,y) >= x)//询问
                puts("Yes"),t++;
            else puts("No");
        }
    }
    return 0;
}