Lie about a segment tree

%%% Lee Ultra

Due to Watch J oe Great God decadent study found that high-level data structures

Attracted to the disaster (my first change did not change completely finished T2 T1

Instructions:

Purpose: to maintain the optimal time Functions

Complexity: $ \ Theta (n log_ {2} n ^ {2}) $

Food Guide:

We first segment tree node information stored in memory instead of a line

The segments are stored are not necessarily the most at each point range, but should be the best in the interval

There is no feeling like a two-dimensional MLE segment tree

Of course there can be so different in the two lines are the best locations in the same range

At this excellent maintenance interval longer

(On the assumption that the maximum maintenance

Interval Review:

  We consider two line segments

  First, we want the segment positioned line segment tree, then there will be $ log_ {2} n $ intervals need to be modified

  Next, we only consider a range of Kazakhstan

  By the relative position of the two segments was strong Category talk:

  A total of four cases

  1.

    

 

 

     Line $ a $ (blue) than the current slope of the best line $ b $ (perhaps orange) small

  And the right side of the midpoint of the intersection of two segments

  Then obviously the interval for the current interval and the left line $ a $ better,

  Here it can recursively to stop (current section has been modified and then modify the left side of the interval to be meaningless

  But for the right side of the interval

  $ B $ segments may still be better

  At this time, the segment should be $ b $ recursive parameter modification right section

  2.

   

 

     $ A $ line (supra) the slope of the line segment is smaller than the current optimal $ B $ (supra)

  And the intersection of two line segments in the left midpoint

  $ A $ line interval in left only possible better

  At this time, the segment should be $ A $ recursive parameter modification left section

  3.

  

 

       Line $ a $ (ibid.) Slope is greater than the current optimal line $ b $ (ibid)

  And the right side of the midpoint of the intersection of two segments

  $ A $ line section on the right side only may more preferably

  At this time, the segment should be $ A $ recursive parameter modification right section

  4.

  

 

   

       Line $ a $ (ibid.) Slope is greater than the current optimal line $ b $ (ibid)

  And the intersection of two line segments in the left midpoint

  For the current section and the left section line $ b $ better,

  At this time, the segment should be $ b $ recursive parameter modification left section

A single point of inquiry:

  Queries can be normal

  Note should be taken at the minimum and maximum values ​​for each node

  Because the optimal solution and may come from a leaf node

  It is also possible from the ancestor node

Attached The examination T1 Code:

#include<bits/stdc++.h>
using namespace std; #define int long long #define cin(k) scanf("%lld",&k) #define l(k) ((k)<<1) #define r(k) (l(k)|1) const int maxn=64646,base=32323; struct line{int k,b;}; struct Tree{ struct tree{ int bo; line li; }t[base<<3]; bool cmp(int p,line x,line y) { p-=base; return x.k*p+x.b>=y.k*p+y.b; } void add(int k,int l,int r,line li) { if(l==r) { if(cmp(l,li,t[k].li)) t[k].li=li; return; } int mid=(l+r)>>1; if(t[k].li.k<=li.k) { if(cmp(mid,li,t[k].li)) { add(l(k),l,mid,t[k].li); t[k].li=li; } else add(r(k),mid+1,r,li); } else { if(cmp(mid,li,t[k].li)) { add(r(k),mid+1,r,t[k].li); t[k].li=li; } else add(l(k),l,mid,li); } } int query(int k,int l,int r,int p,int res) { int ans=t[k].li.k*(p-base)+t[k].li.b; if(l==r) return ans; int mid=(l+r)>>1; // if(res) { if(p<=mid) return max(ans,query(l(k),l,mid,p,res)); else return max(ans,query(r(k),mid+1,r,p,res)); } /* else  {  if(p<=mid) return min(ans,query(l(k),l,mid,p,res));  else return min(ans,query(r(k),mid+1,r,p,res));  } */ } }S,T; int n,qu; int ans[maxn+1]; signed main() { freopen("A.in","r",stdin); freopen("A.out","w",stdout); cin(n);cin(qu); for(int q=1;q<=n;q++) { int a,b;cin(a),cin(b); S.add(1,base+1,maxn,(line){a,b}); T.add(1,1,base-1,(line){-a,-b}); } for(int q=-32322;q<=32322;q++) { if(q>0) ans[q+base]=S.query(1,base+1,maxn,q+base,1)*q; else if(q<0) ans[q+base]=-T.query(1,1,base-1,q+base,0)*q; else ans[q+base]=0; } for(int q=1;q<=qu;q++) { int x;cin(x); printf("%lld\n",ans[x+base]); } } 

Range query:

  Like tree line and two-dimensional range query like you yy it (in fact, I'm lazy

Death of the author (Cause of death: due to transfer the code above

Guess you like

Origin www.cnblogs.com/ooovooo/p/11856073.html