Advertisement
dipBRUR

Marge Sort Tree Implementation

Aug 8th, 2017
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. /**
  2.  * Author           : Dipu Kumar Mohanto (Dip)
  3.  *                    CSE, BRUR.
  4.  *                    Batch - 6
  5.  *
  6.  * Problem          : Marge Sort Tree Implementation
  7.  * Algorithm        : Marge Sort Tree
  8.  * Compleixty       : build_tree     -> O(logn^2)
  9.  *                    query_tree     -> O(logn^2)
  10.  * Category         : Data Structure
  11.  *
  12.  * Difficulty       : Medium
  13.  *
  14.  * Source           : commonlounge.com
  15.  *                    codechef.com - merge-sort-tree-tutorial
  16.  *
  17.  * Verdict          : OK
  18.  *
  19.  * Date             :
  20.  * E-mail           : dipukumarmohanto1@gmail.com
  21. **/
  22.  
  23. #include <bits/stdc++.h>
  24.  
  25. using namespace std;
  26.  
  27. #define VI                     vector <int>
  28. #define pb                     push_back
  29. #define all(v)                 v.begin(), v.end()
  30.  
  31. const int mx = 1e5+5;
  32.  
  33. int arr[mx];
  34.  
  35. struct nodeTree {
  36.     VI data;
  37.  
  38.     void assignLeafNode(int num) {
  39.         data.pb(num);
  40.     }
  41.  
  42.     void marge(nodeTree& leftTree, nodeTree& rightTree) { // marge part, same as  
  43.         int len_left = leftTree.data.size();              // marge sort
  44.         int len_right = rightTree.data.size();
  45.  
  46.         int index_left = 0;
  47.         int index_right = 0;
  48.  
  49.         while (index_left<len_left && index_right<len_right) {
  50.             if (leftTree.data[index_left] < rightTree.data[index_right]) {
  51.                 data.pb(leftTree.data[index_left]);
  52.                 index_left++;
  53.             }
  54.  
  55.             else {
  56.                 data.pb(rightTree.data[index_right]);
  57.                 index_right++;
  58.             }
  59.         }
  60.  
  61.         while (index_left<len_left) {
  62.             data.pb(leftTree.data[index_left]);
  63.             index_left++;
  64.         }
  65.  
  66.         while (index_right<len_right) {
  67.             data.pb(rightTree.data[index_right]);
  68.             index_right++;
  69.         }
  70.     }
  71.  
  72.     int getRes(int k) {
  73.         return lower_bound(all(data), k) - data.begin();
  74.     }
  75. }tree[mx<<2];
  76.  
  77.  
  78.  
  79. void build_tree(int node, int a, int b)
  80. {
  81.     if (a>b)
  82.         return;
  83.  
  84.     if (a==b)
  85.     {
  86.         tree[node].assignLeafNode(arr[a]);
  87.         return;
  88.     }
  89.     int left = node<<1;
  90.     int right = node<<1|1;
  91.     int mid = (a+b)>>1;
  92.  
  93.  
  94.     build_tree(left, a, mid);
  95.     build_tree(right, mid+1, b);
  96.  
  97.     tree[node].marge(tree[left], tree[right]);
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117. int query_tree(int node, int a, int b, int i, int j, int k)
  118. {
  119.     if (a>b || a>j || i>b)
  120.         return 0;
  121.  
  122.     if (a>=i && b<=j)
  123.     {
  124.         return tree[node].getRes(k);
  125.     }
  126.     int left = node<<1;
  127.     int right = node<<1|1;
  128.     int mid = (a+b)>>1;
  129.  
  130.     int p1 = query_tree(left, a, mid, i, j, k);
  131.     int p2 = query_tree(right, mid+1, b, i, j, k);
  132.  
  133.     return p1+p2;
  134. }
  135.  
  136.  
  137.  
  138.  
  139. int main()
  140. {
  141.     //freopen("in.txt", "r", stdin);
  142.     //freopen("out.txt", "w", stdout);
  143.  
  144.     int n;
  145.     cin >> n;
  146.  
  147.     for (int i=1; i<=n; i++)
  148.         cin >> arr[i];
  149.  
  150.     build_tree(1, 1, n);
  151.  
  152.     cerr << "--------------------------------" << endl;
  153.     for (int i=1; i<n<<1; i++)
  154.     {
  155.         cerr << i << " vector -> ";
  156.         for (auto it : tree[i].data)
  157.             cerr << it << " ";
  158.         cerr << endl;
  159.     }
  160.     cerr << "--------------------------------" << endl;
  161.  
  162.     int q;
  163.     cin >> q;
  164.  
  165.     while (q--)
  166.     {
  167.         int x, y, k;
  168.         cin >> x >> y >> k;
  169.  
  170.         cout << query_tree(1, 1, n, x, y, k) << endl;
  171.     }
  172.     return 0;
  173. }
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183. /****** Input Output    **********/
  184. 5
  185. 1 2 5 3 4
  186. 5
  187. 1 2 3
  188. 1 5 10
  189. 1 5 2
  190. 3 5 4
  191. 2 2 2
  192.  
  193. --------------------------------
  194. 1 vector -> 1 2 3 4 5
  195. 2 vector -> 1 2 5
  196. 3 vector -> 3 4
  197. 4 vector -> 1 2
  198. 5 vector -> 5
  199. 6 vector -> 3
  200. 7 vector -> 4
  201. 8 vector -> 1
  202. 9 vector -> 2
  203. --------------------------------
  204.  
  205. 2
  206. 5
  207. 1
  208. 1
  209. 0
  210. /**********************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement