bzoj3489 A simple rmq problem (K-D tree three-dimensional subspace maximization)

Firstly, we deal with the problem that only once is useful. First, we deal with the effective interval L[i]~R[i] of each point. Here, we define L[i] as the last occurrence of a[i], and R[i] as the next occurrence of a[i]. For each question l,r, what number of positions is satisfied? Three conditions need to be met, namely:
1.l<=i<=r
2.L[i]< l
3.R[i]> r
If we consider the number at each position as a point in a three-dimensional space, that is, P[i] (i,L[i],R[i]), with a weight of a[i], we ask for the maximum value in a subspace every time. It can be maintained with K-D tree. Complexity O(m_n23)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 100010
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m,a[N],last[N],L[N],R[N],rt=0,D=0,ans=0,X,Y;
struct point{
    int d[3],v;
    int& operator[](int x){return d[x];}
    friend bool operator<(point a,point b){return a[D]<b[D];}
}P[N];
struct node{
    point x;int lc,rc,mx[3],mn[3],mxv;
}tr[N];
inline void update(int p){
    int l=tr[p].lc,r=tr[p].rc;
    for(int i=0;i<3;++i) tr[p].mn[i]=min(tr[p].mn[i],min(tr[l].mn[i],tr[r].mn[i]));
    for(int i=0;i<3;++i) tr[p].mx[i]=max(tr[p].mx[i],max(tr[l].mx[i],tr[r].mx[i]));
    tr[p].mxv=max(tr[p].x.v,max(tr[l].mxv,tr[r].mxv));
}
inline void build(int &p,int l,int r,int op){
    int mid=l+r>>1;p=mid;D=op;
    nth_element(P+l,P+mid,P+r+1);tr[p].x=P[mid];tr[p].mxv=P[mid].v;
    for(int i=0;i<3;++i) tr[p].mx[i]=tr[p].mn[i]=tr[p].x[i];
    if(l<mid) build(tr[p].lc,l,mid-1,(op+1)%3);
    if(r>mid) build(tr[p].rc,mid+1,r,(op+1)%3);update(p);
}
inline bool jud(point a){
    return a[0]>=X&&a[0]<=Y&&a[1]<X&&a[2]>Y;
}
inline bool calc(int p){
    if(!p) return 0;
    if(tr[p].mn[0]>Y||tr[p].mx[0]<X) return 0;
    if(tr[p].mn[1]>=X||tr[p].mx[2]<=Y) return 0;return 1; 
}
inline void ask(int p){
    if(jud(tr[p].x)) ans=max(ans,tr[p].x.v);
    int dl=0,dr=0;
    if(calc(tr[p].lc)) dl=tr[tr[p].lc].mxv;
    if(calc(tr[p].rc)) dr=tr[tr[p].rc].mxv;
    if(dl>dr){
        if(dl>ans) ask(tr[p].lc);
        if(dr>ans) ask(tr[p].rc);
    }else{
        if(dr>ans) ask(tr[p].rc);
        if(dl>ans) ask(tr[p].lc);
    }
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();m=read();
    for(int i=0;i<3;++i) tr[0].mx[i]=-inf,tr[0].mn[i]=inf;
    for(int i=1;i<=n;++i){
        a[i]=read();R[last[a[i]]]=i;L[i]=last[a[i]];last[a[i]]=i;
    }for(int i=1;i<=n;++i) if(!R[i]) R[i]=n+1;
    for(int i=1;i<=n;++i) P[i][0]=i,P[i][1]=L[i],P[i][2]=R[i],P[i].v=a[i];
    build(rt,1,n,0);
    while(m--){
        X=(read()+ans)%n+1;Y=(read()+ans)%n+1;if(X>Y) swap(X,Y);
        ans=0;ask(rt);printf("%d\n",ans);
    }return 0;
}

Posted by phpflixnewbie on Wed, 13 Feb 2019 15:30:18 -0800