Monotonous stack segment tree maintenance

  Foreword

    One day changed three tree line. True tm stimulation.

    Data structures like a girlfriend, not a few days ignore me.

    Nothing to do to write a summary.

  serious

    Consequently capable segment tree series.

    Let's say there is a malignant tumor DP JZOJGod Knows

    We ask you to maintain a maximum search range to a value within the stack monotonic linear less complexity.

    Or the length of the longest monotonic sequence. Then compare the dog feces.

    At this time you need some magic to maintain the operation.

    What kind of things can be considered maintenance.

    I found that this time around the tree line son influence each other.

    Take as an example the query sequence length increases monotonously.

    Interval general segment tree is not guaranteed to be monotonic. But there is a relatively injury.

    Right Son: Do you think you left a son is higher than I appropriate it.

    Left Son: .......

    So the son was left cut the right son.

    Specifically, it is to achieve a cal (rump) function.

    First, this thing can make your son the right node cut in his father left his son (Harmony)

    Consider this function cal cal (k, w)

    segment tree is node k, w is a value representing the length of the sequences increase the interval I is greater than the number w flicked.

    Then the next must pay attention to the complexity, log query tree line are guaranteed by chain extension.

    We can only go on one side recursive, so is the log of a cal.

    Well, this time we can consider the relationship between the right and the son of a minimum value w, the minimum if the right son

    W value than small, less direct GG, the right son is certainly not the whole shells, recursive left.

    If the minimum value is greater than the right son w, then you can recursively on the right, but also how to count the left?

    Must not be recursive, you need to maintain such information on a node,

    dw represents the left and right son after his son was cut lengths.  

    Cal then this function is perfect.

    When the minimum value is larger than the right son w, then recursively on the right and left together with his son dw

  

int calfr(int k,int l,int r,int w)
{
	if(l==r) return tr[k].w>w;
	int mid=l+r>>1;
	if(tr[k<<1|1].w<w) return calfr(k<<1,l,mid,w);
	else return tr[k<<1].dw+calfr(k<<1|1,mid+1,r,w);
}

 

    Q: So by doing this cal function.

    A: Maintenance dw.

    Be careful not to hit the face of this.

    Let me explain. When cal use of this function is when a retrospective, this time below the dw should have been updated over,

    Dw then used to update the current node.

    So how do the query?

    Given an interval [x, y], the length of the interrogation zone of increasing sequence from the right to do.

    Or that the idea first section hit a tree line on the log interval, and then take up the indirect area. . .

    There is a rump function does not do

    Think about the words should be connected from right to left,

    The answer is found in the specific minimum and the right, to the right of the minimum interval as a parameter,

    Both the left and then query the value range, check the left side of the left.

    The first time a recursive call to pass a maximum value on the line.

    So write so you should first segment tree to the right of the recursion.

    Recommend a segment tree writing.

    Then gone.

    Tajiri is a function of $ $ log, overall complexity $ (nlog ^ 2n) O $

  

st asknw(int k,int l,int r,int x,int y,int w)
{
    if(x>y) return (st){0,0};
    if(l==x&&r==y)
    {
        int sw=calfr(k,l,r,w);
        return (st){sw,tr[k].w};
    }
    int mid=l+r>>1;
    if(x>mid) return asknw(k<<1|1,mid+1,r,x,y,w);
    else if(y<=mid) return asknw(k<<1,l,mid,x,y,w);
    else
    {
        st tmp=asknw(k<<1|1,mid+1,r,mid+1,y,w);
        st now=asknw(k<<1,l,mid,x,mid,max(w,tmp.id));
        return (st){tmp.w+now.w,max(tmp.id,now.id)};
    }
}
View Code

 

  

  

Guess you like

Origin www.cnblogs.com/starsing/p/11758961.html