Description:
Xiao Ya recently learned the line tree, but because her IQ is relatively low, the use is not very familiar
Practice. So little R gave her a little practice training, one of which was this.
This is a code of achievement for the line tree of small R writing:
Just call Buildtree (1,0,n) to get a tree segment. Clearly, a line of tree one
There are O (n) nodes, because each node represents a different interval, so the segment tree
There are a total of O (n) different intervals.
Now small R gives you an interval "L; r", and wants you to tell him a minimum of N to make the interval "L;"
Appears in the tree of segments built with Buildtree (1,0n).
Input
The first line enters a positive integer that represents the number of data groups.
Next Ding three integer l per line; R Lim represents a set of inquiries, if for all 0 <= N
<= Lim does not have a solution that satisfies the conditions, and the output is 11.
Output
For each set of queries output an answer.
Sample Input
2
0 5 10
6 7 10
Sample Output
5
7
I did not test the problem, so I do not know whether it can all over the data, but the idea is certainly correct.
You can arbitrarily give a 0 to N, draw the line of the tree, you will find that this question is only the interval from where the division, and so on.
We have found that there are four possible ((left-1) *2-right,right) (((left-1) *2-right+1,light) of the parent interval for any one of the sub-ranges.
(Left,right*2-left), (left,right*2-left+1), so is a search, of course, will not time out. Some small details ah, border ah, oneself to consider, more easily thought.
1#include <iostream>2#include <fstream>3#include <cstdlib>4#include <cstring>5 using namespacestd;6 7Ifstream Fin ("tree.in");8Ofstream Fout ("Tree.out");9 Ten intcnt_zu=0; One A Long Long intDfsintLeftintRightintLim) { - if(left==0)returnRight ; - if(Right>lim)returnlim+1; the intL=0, r=0; - Long Longans=0x7fffffff; -L= (left-1)*2-Right ; -R=Right ; + if((r+l)/2==left-1) ans=min (Ans,dfs (L,r,lim)); - if((r+l+1)/2==left-1) Ans=min (Ans,dfs (l +1, R,lim)); +L=Left ; Ar=right*2-Left ; at if((r+l)/2==right) ans=min (Ans,dfs (L,r,lim)); - if((r+l+1) ==right) ans=min (Ans,dfs (l,r+1, Lim)); - returnans; - } - - intMain () { inFin>>Cnt_zu; - for(intx=1; x<=cnt_zu;x++){ to intleft=0, right=0; + intlim=0; -Fin>>left>>right>>Lim; the Long Long intans=DFS (Left,right,lim); * if(Ans>lim) ans=-1; $cout<<ans<<Endl;Panax Notoginsengfout<<ans<<Endl; - } the return 0; +}
"Pseudo-puzzle" segment tree What hates it most (DFS)