The precursor of $i$ was $p_i$, followed by $q_i$, who regarded the inquiry as a point $ (l,r) $, a contributing $i$ satisfying $l\in (p_i,i]$ and $r\in[i,q_i) $, and asked for the maximum value of the rectangle that covered the point. Then you can use a durable tree nesting heap, one-dimensional can be persisted when inserting a rectangle, one-dimensional interval is inserted, and the maximum value is maintained with the heap. Note that the "persistent heap" here requires only a query history, so it is only necessary to write down the maximum value.
#include <algorithm> #include <cstdio> #include <queue> #define I (j+1) #define J (i+j>>1) #define P (k<<1) #define S (p^1) using namespace Std;const int n=1e5+5;int n,m;typedef int Arr[n];arr p,q,f,a;struct Heap{prio rity_queue<int> s,t;void ins (int j) {S.push (j);} void del (int j) {T.push (j);} int Top () {while (T.size () &&s.top () ==t.top ()) S.pop (), T.pop (); return s.size ()? S.top (): 0;}} c[1<<18];typedef struct node* ptr;struct node{ptr i,j;int s;} E[N*136];p tr z=e+1,r[n];int eval (int v,ptr s,int i=1,int j=n) {if (i==j) return S->s;return Max (S->s,v<i?eval (V, S->I,I,J): eval (v,s->j,i,j));} struct Info{void (heap::* foo) (int); int f,s,t;}; void vary (info f,ptr& s,int i=1,int j=n,int k=1) {s=& (*z++=*s); if (f.s<=i&&j<=f.t) {(C[k].*f.foo) (F.F); S->s=c[k].top ();} Else{if (f.s<i) vary (f,s->i,i,j,p), if (f.t>j) vary (f,s->j,i,j,s);}} struct edge{edge* s;info f;} E2[n*2];edge *z2=e2,*y[n];int Main () {scanf ("%d%d", &n,&m); for (int i=1;I<=n;++i) {scanf ("%d", a+i);p [I]=f[a[i]],f[a[i]]=i;} Fill (f+1,f+n+1,n+1), for (int i=n;i;--i) q[i]=f[a[i]],f[a[i]]=i;for (int i=n;i;--i) {int S=p[i]+1,t=i+1;edge u={y[s], &heap::ins,a[i],i,q[i]-1};edge v={y[t],&heap::d el,a[i],i,q[i]-1};* (y[s]=z2++) =u;* (y[t]=z2++) =v;} * (*r=e) = (node) {e,e};for (int i=1;i<=n;++i) {r[i]=r[i-1];for (edge* j=y[i];j;j=j->s) vary (j->f,r[i]);} int K=0,s,t;while (m--) {scanf ("%d%d", &s,&t), if (s= (s+k)%n+1) > (t= (t+k)%n+1)) Swap (s,t);p rintf ("%d\n", k= Eval (T,r[s]));}}
Bzoj3489:a Simple RMQ problem