Template: heuristic merge

First point: the merger is not a segment tree heuristic merge.

Heuristic merged content is about: the small data structure in accordance with the normal method of inserting this data structure, one by one go into violence.

The segment tree merger is clearly not this thing.

Solution to a problem of solving the problem sucks, so the delay for a long time.

For each operation, it only has three parameters: the starting position, duration of action, color.

The discrete color and let their numbers distributed within 1e5. It may not be discrete, some of it a little troublesome. Note that there are negative numbers.

Now we maintain a time interval for the subject under tree line, which maintains two weights, one is the number of the first occurrence of the ball, and the other is the number of all small ball.

We opened with a set of vector operations is stored in this node occurs, the storage time required / Color two parameters.

We just started reading all operations directly put it into vector's starting position.

Then you can answer it again dfs solved. Considered to be doing at each point.

1) recursive solution all subtrees

2) solve this operation on all nodes of the present node answers obtained

3) all the events associated with their uploading to the parent node

This is actually a violent ideas.

Consider, if in accordance with the above ideas, or is the need to empty the tree line, or is open 100 000 tree burst memory.

Only the first can be considered optimized.

If node 1 has two sons, including operations No. 99,995 nodes 2, there are five nodes No. 3.

Also empty the tree line? You have any ideas?

For No. 3 node that violence upload complexity is not high, the key is how to deal with the 2nd node.

If the first sweep of No. 3 nodes, emptied, and then No. 2 node, the information is not cleared, the event also temporarily not to upload.

So back to the 1st node, we already have a ready-made save for a segment tree 99 995 operations, but there are only 5 vector operations need to be inserted. How wonderful

Consider this question: because the parent is just one point, while the sub-tree may have a lot of points, so a son could gather a lot of action.

Then for each node, we call it "the most subtree operand son" for its heavy son.

Well, according to this line of thought just an example of sub-optimize the process at the top of the dfs.

1) sweep all sons of light, the light all the time his son into the parent vector in the amount of vector, the empty tree line.

2) solving heavy son, retaining segment tree, temporarily re-join his son vector inside the event.

3) the vector is inserted inside all the tree line is inserted into the tree line of events, solving answer.

But my code to achieve the emptying and information uploaded on the last, for a unified solution to a problem with the code, change it a little of dfs.

1) Esau despised his son.

2) heavy sweep son.

3) the vector is inserted inside all the tree line is inserted into the tree line of events, solving answer.

4) If the current node is not a heavy son of its parent node, it is clear the tree line, the vector uploaded.

In fact, the essence is the same, my personal feeling very good understanding.

However, above this line of thought is still not mature enough, there are some specific implementation complexity have some time to make a difference.

The first is how to obtain heavy son. This is quite simple, dfs sweep again. Remember finish this function to be called! Do not like the name of a named Wu is the same as the word fool.

. 1  void pre_dfs ( int P, int FA) {
 2      SIZ [P] = V [P] .size ();
 . 3      for ( int I = FIR [P]; I; I = L [I]) IF (to [ ! I] = FA) {
 . 4          pre_dfs (to [I], P), SIZ [P] + = SIZ [to [I]];
 . 5          IF (SIZ [to [I]]> SIZ [HSON [P]] ) IHS [HSON [p]] = 0 , HSON [p] = to [I], IHS [to [I]] = . 1 ; // HSON [p] p deposit weight son, ihs [p] represents whether p son weight of parent node
 6      }
 7 }

Then there is how the vector data upload. Consider the extreme case: a chain 100000, there are 100,000 event on the last point.

Obviously a violent event will be uploaded to the dog T, which is why heuristic merge, so there is a guarantee nlog merger of the complexity.

. 1  void up ( int P, int FA) {
 2      IF (V [ REF [P]]. Size () <V [ REF [FA]]. Size ()) {// the small inside inserted one by one to a large
 . 3          for ( int I = 0 ; I <V [ REF . [P]] size (); I ++) V [ REF [FA]] push_back (V [. REF [P]] [I]);
 . 4          V [ REF [P]] Clear ();. // empty, to prevent the explosion of memory, but if necessary
 . 5      }
 . 6      the else { for ( int I = 0 ; I <V [ REF . [FA]] size (); + I +) V [ REF[P]] push_back (V [. REF [FA]] [I]); V [ REF [FA]] Clear ();. REF [FA] = REF [P];} 
    // if it is a parent node vector smaller than the son, then the father after the vector node poured into child nodes, we should amend the representative element, that is, the node is no longer a fa corresponding vector v [fa] but v [the p-]
7 }

The next segment is the need to implement the tree. Let t represent the total number of ball array, c represents the array (i.e., the number of balls in each of the first occurrence of color) contributes to the number of balls

The first is modified, we still have to clear the tree line (specific and so will say), you need to be increased or decreased

1 void insert(int p,int pos,int tt,int cc){
2     if(cl[p]==cr[p]){t[p]+=tt;c[p]+=cc;return;}
3     if(pos<=cr[p<<1])insert(p<<1,pos,tt,cc);
4     else insert(p<<1|1,pos,tt,cc);
5     t[p]=t[p<<1]+t[p<<1|1];c[p]=c[p<<1]+c[p<<1|1];
6 }

 

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/11270149.html