Advertisement
Guest User

Untitled

a guest
May 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.28 KB | None | 0 0
  1. class Node{
  2.     int numberParni;
  3.     boolean paren;
  4.     int value;
  5.     public void setParen(){
  6.         if(value%2==0) {
  7.             paren = true;
  8.             numberParni=1;
  9.         }
  10.         else{
  11.             numberParni=0;
  12.         }
  13.     }
  14.  
  15. }
  16. class SegmentTree {
  17.     Node st[]; // The array that stores segment tree nodes
  18.  
  19.     /* Constructor to construct segment tree from given array. This
  20.        constructor  allocates memory for segment tree and calls
  21.        constructSTUtil() to  fill the allocated memory */
  22.     SegmentTree(int arr[], int n) {
  23.         // Allocate memory for segment tree
  24.         //Height of segment tree
  25.         int x = (int) (Math.ceil(Math.log(n) / Math.log(2)));
  26.  
  27.         //Maximum size of segment tree
  28.         int max_size = 2 * (int) Math.pow(2, x) - 1;
  29.  
  30.         st = new Node[max_size]; // Memory allocation
  31.         for (int i = 0; i < st.length; i++) {
  32.             st[i] = new Node();
  33.         }
  34.  
  35.         constructSTUtil(arr, 0, n - 1, 0);
  36.     }
  37.  
  38.     // A utility function to get the middle index from corner indexes.
  39.     int getMid(int s, int e) {
  40.         return s + (e - s) / 2;
  41.     }
  42.  
  43.     /*  A recursive function to get the sum of values in given range
  44.         of the array.  The following are parameters for this function.
  45.    
  46.       st    --> Pointer to segment tree
  47.       si    --> Index of current node in the segment tree. Initially
  48.                 0 is passed as root is always at index 0
  49.       ss & se  --> Starting and ending indexes of the segment represented
  50.                     by current node, i.e., st[si]
  51.       qs & qe  --> Starting and ending indexes of query range */
  52.     int getParniUtil(int ss, int se, int qs, int qe, int si) {
  53.         // If segment of this node is a part of given range, then return
  54.         // the sum of the segment
  55.         if (qs <= ss && qe >= se)
  56.             return st[si].numberParni;
  57.  
  58.         // If segment of this node is outside the given range
  59.         if (se < qs || ss > qe)
  60.             return 0;
  61.  
  62.         // If a part of this segment overlaps with the given range
  63.         int mid = getMid(ss, se);
  64.         int leftNumber = getParniUtil(ss, mid, qs, qe, 2 * si + 1);
  65.         int rightNumber = getParniUtil(mid + 1, se, qs, qe, 2 * si + 2);
  66.         return leftNumber + rightNumber;
  67.     }
  68.  
  69.     /* A recursive function to update the nodes which have the given  
  70.        index in their range. The following are parameters
  71.         st, si, ss and se are same as getSumUtil()
  72.         i    --> index of the element to be updated. This index is in
  73.                  input array.
  74.        diff --> Value to be added to all nodes which have i in range */
  75.     Node updateValueUtil(int ss, int se, int i, int value, int si) {
  76.         // Base Case: If the input index lies outside the range of  
  77.         // this segment
  78.         if (i < ss || i > se)
  79.             return st[si];
  80.  
  81.         // If the input index is in range of this node, then update the
  82.         // value of the node and its children
  83.  
  84.         if (se != ss) {
  85.             int mid = getMid(ss, se);
  86.             Node left = updateValueUtil(ss, mid, i, value, 2 * si + 1);
  87.             Node right = updateValueUtil(mid + 1, se, i, value, 2 * si + 2);
  88.             st[si].numberParni = left.numberParni + right.numberParni;
  89.             return st[si];
  90.         } else {
  91.             st[si].value=value;
  92.             st[si].setParen();
  93.             return st[si];
  94.         }
  95.     }
  96.  
  97.     // The function to update a value in input array and segment tree.
  98.     // It uses updateValueUtil() to update the value in segment tree
  99.     void updateValue(int arr[], int n, int i, int new_val) {
  100.         // Check for erroneous input index
  101.         if (i < 0 || i > n - 1) {
  102.             System.out.println("Invalid Input");
  103.             return;
  104.         }
  105.  
  106.         // Get the difference between new value and old value
  107.  
  108.         // Update the value in array
  109.         arr[i] = new_val;
  110.  
  111.         // Update the values of nodes in segment tree
  112.         updateValueUtil(0, n - 1, i, new_val, 0);
  113.     }
  114.  
  115.     // Return sum of elements in range from index qs (quey start) to
  116.     // qe (query end).  It mainly uses getSumUtil()
  117.     int getParni(int n, int qs, int qe) {
  118.         // Check for erroneous input values
  119.         if (qs < 0 || qe > n - 1 || qs > qe) {
  120.             System.out.println("Invalid Input");
  121.             return -1;
  122.         }
  123.         return getParniUtil(0, n - 1, qs, qe, 0);
  124.     }
  125.  
  126.     // A recursive function that constructs Segment Tree for array[ss..se].
  127.     // si is index of current node in segment tree st
  128.     Node constructSTUtil(int arr[], int ss, int se, int si) {
  129.         // If there is one element in array, store it in current node of
  130.         // segment tree and return
  131.         if (ss == se) {
  132.             st[si].value = arr[ss];
  133.             st[si].setParen();
  134.             return st[si];
  135.         }
  136.         // If there are more than one elements, then recur for left and
  137.         // right subtrees and store the sum of values in this node
  138.         int mid = getMid(ss, se);
  139.         Node left = constructSTUtil(arr, ss, mid, si * 2 + 1);
  140.         Node right = constructSTUtil(arr, mid + 1, se, si * 2 + 2);
  141.         st[si].numberParni = left.numberParni + right.numberParni;
  142.         return st[si];
  143.     }
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement