public void CanUpdateValue() { var sut = new MinSegmentTree(new[] { 1, 3, 5, 7, 9, 11 }); Assert.Equal(3, sut.GetSegmentValue(1, 3)); sut.Update(1, 8); Assert.Equal(5, sut.GetSegmentValue(1, 3)); }
public void Build(int root) { id = new int[G.Length]; depth = new int[G.Length]; parent = new int[G.Length]; for (int i = 0; i < parent.Length; i++) { parent[i] = -1; } mask = new long[G.Length * 2 - 1]; DFS(root); seg = new MinSegmentTree(mask); }
public void GetMin() { var sut = new MinSegmentTree(new[] { 1, 3, 5, 7, 9, 11 }); Assert.Equal(3, sut.GetSegmentValue(1, 3)); }
public void LightTreesMustWork() { var rnd = new Random(123); // min tree for (int tests = 0; tests < 100; ++tests) { int n = rnd.Next(100) + 1; var arr = new int[n]; for (int i = 0; i < n; ++i) { arr[i] = rnd.Next(); } var tree = new MinSegmentTree <int>(arr, int.MaxValue); for (int op = 0; op < 100; ++op) { if (rnd.Next(2) == 0) { int idx = rnd.Next(n); int newVal = rnd.Next(); arr[idx] = newVal; tree[idx] = newVal; } else { int l = rnd.Next(n); int r = rnd.Next(n); if (l > r) { int tmp = l; l = r; r = tmp; } var brute = arr[l]; for (int i = l + 1; i <= r; ++i) { brute = Math.Min(brute, arr[i]); } var my = tree.GetMin(l, r); Assert.AreEqual(brute, my); } } } // max tree for (int tests = 0; tests < 100; ++tests) { int n = rnd.Next(100) + 1; var arr = new int[n]; for (int i = 0; i < n; ++i) { arr[i] = rnd.Next(); } var tree = new MaxSegmentTree <int>(arr, int.MinValue); for (int op = 0; op < 100; ++op) { if (rnd.Next(2) == 0) { int idx = rnd.Next(n); int newVal = rnd.Next(); arr[idx] = newVal; tree[idx] = newVal; } else { int l = rnd.Next(n); int r = rnd.Next(n); if (l > r) { int tmp = l; l = r; r = tmp; } var brute = arr[l]; for (int i = l + 1; i <= r; ++i) { brute = Math.Max(brute, arr[i]); } var my = tree.GetMax(l, r); Assert.AreEqual(brute, my); } } } }