niyaznigmatullin

Untitled

Oct 21st, 2015
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1.     static class PersistentArray {
  2.         static class Node {
  3.             Node left;
  4.             Node right;
  5.             int value;
  6.  
  7.             public Node(Node left, Node right, int value) {
  8.                 this.left = left;
  9.                 this.right = right;
  10.                 this.value = value;
  11.             }
  12.  
  13.             public Node(Node left, Node right) {
  14.                 this.left = left;
  15.                 this.right = right;
  16.             }
  17.  
  18.             public Node() {
  19.                 this(null, null, 0);
  20.             }
  21.  
  22.             public Node(int value) {
  23.                 this.value = value;
  24.             }
  25.  
  26.         }
  27.  
  28.         Node root;
  29.         int n;
  30.  
  31.         public PersistentArray(int n) {
  32.             this.n = n;
  33.             root = new Node();
  34.             makeArray(root, 0, n);
  35.         }
  36.  
  37.         public PersistentArray(Node root, int n) {
  38.             this.root = root;
  39.             this.n = n;
  40.         }
  41.  
  42.         static void makeArray(Node v, int left, int right) {
  43.             int mid = (left + right) >> 1;
  44.             if (left < mid) {
  45.                 v.left = new Node();
  46.                 makeArray(v.left, left, mid);
  47.             }
  48.             if (mid < right) {
  49.                 v.right = new Node();
  50.                 makeArray(v.right, mid, right);
  51.             }
  52.         }
  53.  
  54.         static Node set(Node v, int left, int right, int index, int newValue) {
  55.             if (left + 1 == right) {
  56.                 return new Node(newValue);
  57.             }
  58.             int mid = (left + right) >> 1;
  59.             if (index < mid) {
  60.                 return new Node(set(v.left, left, mid, index, newValue),
  61.                         v.right);
  62.             } else {
  63.                 return new Node(v.left, set(v.right, mid, right, index,
  64.                         newValue));
  65.             }
  66.         }
  67.  
  68.         public PersistentArray set(int x, int y) {
  69.             return new PersistentArray(set(root, 0, n, x, y), n);
  70.         }
  71.  
  72.         public int get(int x) {
  73.             int left = 0;
  74.             int right = n;
  75.             Node node = root;
  76.             while (left + 1 < right) {
  77.                 int mid = (left + right) >> 1;
  78.                 if (x < mid) {
  79.                     node = node.left;
  80.                     right = mid;
  81.                 } else {
  82.                     node = node.right;
  83.                     left = mid;
  84.                 }
  85.             }
  86.             return node.value;
  87.         }
Advertisement
Add Comment
Please, Sign In to add comment