When the interval is summed, we only need the[1, R], < Span class= "Mopen" >[1,l 1 [l,r] " and.
But the interval maximum does not satisfy this property.
We can split the interval [l,R] into several sub-ranges, and then merge the answers.
Drawing shows thatmax_i needs max only max_{i-2^0}, max_{i-2^1}, max_{i-2^2} ... max_{i-lowbit (i) +1}.
Modify
void Change (int r) { = a[r]; for (int11) = Max (C[r], c[r-i]);}
Enquiry
We're looking for[L, R]is the maximum value of the sub-interval.Max, that is, decrementsR, there can be an optimization, that is, when you find aMax_i< Span class= "Reset-textstyle scriptstyle cramped mtight" > ?, < Span class= "Katex-mathml" >i? Lowbi< Span class= "Mord mathit" >t (i) ≥l , more
new, i = I-lowbit (i) < Span class= "Mord mathit" > l>r just jump out of the loop.
int getmax (intint r) { int ret = a[r]; while (l <= r) { = max (ret, a[r]); for (--r; r-l >= lowbit (R); R-= Lowbit (r) ) = Max (ret, c[r]); } return ret;}
It should be noted that it only supports end-of-insertion and does not support single-point modification operations.
#include <iostream>#include<stdio.h>#include<memory.h>using namespacestd;inta[200005];intc[200005];intLowbit (intx) { returnx& (-x);}voidUpdateintx) {C[x]=A[x]; for(intI=1; I<lowbit (x); i<<=1) C[x]=max (c[x],c[x-i]);}intQueryintLintR) { intans=A[r]; while(l<=R) {ans=Max (Ans,a[r]); for(--r; R-l>=lowbit (r); r-=Lowbit (R)) Ans=Max (Ans,c[r]); } returnans;}intMain () {intm,d; scanf ("%d%d",&m,&d); intCNT =0, t=0; for(inti =0; i<m; i++) { Charop; intN; scanf ("%c%d",&op,&N); if(op=='A') { inttemp = (n+t)%D; a[++cnt]=temp; Update (CNT); } Else{printf ("%d\n", T = query (cnt-n+1, CNT)); } } return 0;}
Tree-like array maintenance interval max