Tree-like array maintenance interval max

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.