Advertisement
Iloominatry

LCA with RMQ

Aug 4th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstring>
  4. #define MAX_N 500
  5. #define MAX (1 << 10)
  6. using namespace std;
  7.  
  8. int depth[2*MAX_N], t_sequence[2*MAX_N], first_ocurrence[MAX_N], indx;
  9. int tree[MAX], lazy[MAX];
  10. vector<vector<int> > children(MAX_N);
  11.  
  12. void build_tree(int node, int a, int b) {
  13.     if(a > b) return; // Out of range
  14.    
  15.     if(a == b) { // Leaf node
  16.             tree[node] = first_ocurrence[a]; // Init value
  17.         return;
  18.     }
  19.    
  20.     build_tree(node*2, a, (a+b)/2); // Init left child
  21.     build_tree(node*2+1, 1+(a+b)/2, b); // Init right child
  22.    
  23.     tree[node] = max(tree[node*2], tree[node*2+1]); // Init root value
  24. }
  25.  
  26. /**
  27.  * Increment elements within range [i, j] with value value
  28.  */
  29. void update_tree(int node, int a, int b, int i, int j, int value) {
  30.  
  31.     if(lazy[node] != 0) { // This node needs to be updated
  32.         tree[node] += lazy[node]; // Update it
  33.  
  34.         if(a != b) {
  35.             lazy[node*2] += lazy[node]; // Mark child as lazy
  36.                 lazy[node*2+1] += lazy[node]; // Mark child as lazy
  37.         }
  38.  
  39.         lazy[node] = 0; // Reset it
  40.     }
  41.  
  42.     if(a > b || a > j || b < i) // Current segment is not within range [i, j]
  43.         return;
  44.    
  45.     if(a >= i && b <= j) { // Segment is fully within range
  46.             tree[node] += value;
  47.  
  48.         if(a != b) { // Not leaf node
  49.             lazy[node*2] += value;
  50.             lazy[node*2+1] += value;
  51.         }
  52.  
  53.             return;
  54.     }
  55.  
  56.     update_tree(node*2, a, (a+b)/2, i, j, value); // Updating left child
  57.     update_tree(1+node*2, 1+(a+b)/2, b, i, j, value); // Updating right child
  58.  
  59.     tree[node] = max(tree[node*2], tree[node*2+1]); // Updating root with max value
  60. }
  61.  
  62. /**
  63.  * Query tree to get max element value within range [i, j]
  64.  */
  65. int query_tree(int node, int a, int b, int i, int j) {
  66.    
  67.     if(a > b || a > j || b < i) return -inf; // Out of range
  68.  
  69.     if(lazy[node] != 0) { // This node needs to be updated
  70.         tree[node] += lazy[node]; // Update it
  71.  
  72.         if(a != b) {
  73.             lazy[node*2] += lazy[node]; // Mark child as lazy
  74.             lazy[node*2+1] += lazy[node]; // Mark child as lazy
  75.         }
  76.  
  77.         lazy[node] = 0; // Reset it
  78.     }
  79.  
  80.     if(a >= i && b <= j) // Current segment is totally within range [i, j]
  81.         return tree[node];
  82.  
  83.     int q1 = query_tree(node*2, a, (a+b)/2, i, j); // Query left child
  84.     int q2 = query_tree(1+node*2, 1+(a+b)/2, b, i, j); // Query right child
  85.  
  86.     int res = max(q1, q2); // Return final result
  87.    
  88.     return res;
  89. }
  90.  
  91. void dfs(int current, int dep){
  92.     first_ocurrence[current] = indx;
  93.     t_sequence[indx] = current;
  94.     depth[indx++] = dep;
  95.  
  96.     for (int i = 0; i < children[current].size(); i++){
  97.         dfs(children[current][i], dep + 1);
  98.         t_sequence[indx] = current;
  99.         depth[indx++] = dep;
  100.     }
  101. }
  102.  
  103. void build(int root){
  104.     indx = 0;
  105.     memset(first_ocurrence, -1, sizeof(first_ocurrence));
  106.     dfs(root, 0);
  107. }
  108.  
  109.  
  110.  
  111. int main(){
  112.     //Build the tree
  113.     build(root);
  114.     build_tree(0,0,cant_nodos);
  115.  
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement