public void AddTest() { SegmentTree st = new SegmentTree(0, 16); st.Add(6, 12); Assert.IsNotNull(st.Root.LeftChild); Assert.IsNotNull(st.Root.RightChild); Assert.IsNotNull(st.Root.LeftChild.RightChild); Assert.IsNotNull(st.Root.RightChild.LeftChild); Assert.IsNotNull(st.Root.LeftChild.RightChild.RightChild); Assert.IsNull(st.Root.LeftChild.LeftChild); Assert.IsNull(st.Root.RightChild.RightChild); Assert.IsNull(st.Root.LeftChild.RightChild.LeftChild); Assert.IsFalse(st.Root.Exist); Assert.IsFalse(st.Root.LeftChild.Exist); Assert.IsFalse(st.Root.RightChild.Exist); Assert.IsFalse(st.Root.LeftChild.RightChild.Exist); Assert.IsTrue(st.Root.RightChild.LeftChild.Exist); Assert.IsTrue(st.Root.LeftChild.RightChild.RightChild.Exist); // Add (0, 2) st.Add(0, 16); Assert.IsTrue(st.Root.Exist); Assert.IsNull(st.Root.LeftChild); Assert.IsNull(st.Root.RightChild); }
public void AddTest_MergeNode() { SegmentTree st = new SegmentTree(0, 16); st.Add(0, 8); st.Add(8, 16); Assert.IsNull(st.Root.LeftChild); Assert.IsNull(st.Root.RightChild); Assert.IsTrue(st.Root.Exist); }
public void TestSimpleAdd() { var collection = new SegmentTree <TreeSegment> (); collection.Add(new TreeSegment(10, 10)); collection.Add(new TreeSegment(12, 2)); collection.Add(new TreeSegment(14, 2)); collection.Add(new TreeSegment(11, 5)); Assert.AreEqual(4, collection.Segments.Count()); }
public void TestSimpleAdd () { var collection = new SegmentTree<TreeSegment> (); collection.Add (new TreeSegment (10, 10)); collection.Add (new TreeSegment (12, 2)); collection.Add (new TreeSegment (14, 2)); collection.Add (new TreeSegment (11, 5)); Assert.AreEqual (4, collection.Segments.Count ()); }
public void TestGetSegmentsOverlapping() { var collection = new SegmentTree <TreeSegment> (); collection.Add(new TreeSegment(10, 10)); collection.Add(new TreeSegment(12, 2)); collection.Add(new TreeSegment(14, 2)); collection.Add(new TreeSegment(11, 5)); Assert.AreEqual(4, collection.GetSegmentsOverlapping(12, 4).Count()); Assert.AreEqual(2, collection.GetSegmentsOverlapping(10, 1).Count()); }
public void TestGetSegmentsOverlapping () { var collection = new SegmentTree<TreeSegment> (); collection.Add (new TreeSegment (10, 10)); collection.Add (new TreeSegment (12, 2)); collection.Add (new TreeSegment (14, 2)); collection.Add (new TreeSegment (11, 5)); Assert.AreEqual (4, collection.GetSegmentsOverlapping (12, 4).Count ()); Assert.AreEqual (2, collection.GetSegmentsOverlapping (10, 1).Count ()); }
public void TestUpdateOnTextReplace() { var collection = new SegmentTree <TreeSegment> (); collection.Add(new TreeSegment(0, 89)); collection.Add(new TreeSegment(92, 51)); collection.Add(new TreeSegment(42, 77)); collection.Add(new TreeSegment(36, 128)); collection.UpdateOnTextReplace(this, new TextChangeEventArgs(0, 0, new string(' ', 355), null)); Assert.AreEqual(0, collection.Count); }
private void button1_Click(object sender, EventArgs e) { //test segment tree, //with overlapped segment ... TreeSegment t1 = new TreeSegment(0, 10); TreeSegment t2 = new TreeSegment(8, 20); SegmentTree <TreeSegment> tree1 = new SegmentTree <TreeSegment>(); tree1.Add(t1); tree1.Add(t2); foreach (var seg in tree1.GetSegmentsAt(9)) { } }
public void AddTest_RemoveChildren() { SegmentTree st = new SegmentTree(0, 16); st.Add(0, 8); Assert.IsNotNull(st.Root.LeftChild); Assert.IsNull(st.Root.RightChild); Assert.IsFalse(st.Root.Exist); Assert.IsTrue(st.Root.LeftChild.Exist); st.Add(0, 16); Assert.IsNull(st.Root.LeftChild); Assert.IsNull(st.Root.RightChild); Assert.IsTrue(st.Root.Exist); }
public IList <int> CountSmaller(int[] nums) { int[] result = new int[nums.Length]; SortedDictionary <int, List <int> > dict = new SortedDictionary <int, List <int> >(); for (int i = 0; i < nums.Length; i++) { List <int> l; if (!dict.TryGetValue(nums[i], out l)) { dict.Add(nums[i], l = new List <int>()); } l.Add(i); } SegmentTree tree = new SegmentTree(0, nums.Length - 1); foreach (var pair in dict) { foreach (int pos in pair.Value) { result[pos] = tree.CountRange(pos + 1, nums.Length - 1); } foreach (int pos in pair.Value) { tree.Add(pos); } } return(result.ToList()); }
public IList<int> CountSmaller(int[] nums) { int[] result = new int[nums.Length]; SortedDictionary<int, List<int>> dict = new SortedDictionary<int, List<int>>(); for (int i = 0; i < nums.Length; i++) { List<int> l; if (!dict.TryGetValue(nums[i], out l)) { dict.Add(nums[i], l = new List<int>()); } l.Add(i); } SegmentTree tree = new SegmentTree(0, nums.Length - 1); foreach (var pair in dict) { foreach (int pos in pair.Value) { result[pos] = tree.CountRange(pos + 1, nums.Length - 1); } foreach (int pos in pair.Value) { tree.Add(pos); } } return result.ToList(); }
public void TestGetSegmentsAt() { var collection = new SegmentTree <TreeSegment> (); collection.Add(new TreeSegment(10, 10)); collection.Add(new TreeSegment(12, 2)); collection.Add(new TreeSegment(14, 2)); collection.Add(new TreeSegment(11, 5)); Assert.AreEqual(0, collection.GetSegmentsAt(9).Count()); Assert.AreEqual(0, collection.GetSegmentsAt(21).Count()); Assert.AreEqual(1, collection.GetSegmentsAt(10).Count()); Assert.AreEqual(2, collection.GetSegmentsAt(11).Count()); Assert.AreEqual(3, collection.GetSegmentsAt(12).Count()); Assert.AreEqual(3, collection.GetSegmentsAt(15).Count()); }
public void TestGetSegmentsAt () { var collection = new SegmentTree<TreeSegment> (); collection.Add (new TreeSegment (10, 10)); collection.Add (new TreeSegment (12, 2)); collection.Add (new TreeSegment (14, 2)); collection.Add (new TreeSegment (11, 5)); Assert.AreEqual (0, collection.GetSegmentsAt (9).Count ()); Assert.AreEqual (0, collection.GetSegmentsAt (21).Count ()); Assert.AreEqual (1, collection.GetSegmentsAt (10).Count ()); Assert.AreEqual (2, collection.GetSegmentsAt (11).Count ()); Assert.AreEqual (3, collection.GetSegmentsAt (12).Count ()); Assert.AreEqual (3, collection.GetSegmentsAt (15).Count ()); }
static void Main() { var n = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split().Select(int.Parse).ToArray(); var M = 1000000007; var f = new long[n + 1]; f[0] = 1; for (int i = 0; i < n; i++) { f[i + 1] = f[i] * (i + 1) % M; } var r = 0L; var st = new SegmentTree(n + 1); for (int i = 0; i < n; i++) { var c = st.Sum(0, a[i]); r = (r + (a[i] - c - 1) * f[n - i - 1]) % M; st.Add(a[i], 1); } Console.WriteLine(r + 1); }
public void TestSimpleRemove () { var collection = new SegmentTree<TreeSegment> (); var seg1 = new TreeSegment (10, 10); collection.Add (seg1); var seg2 = new TreeSegment (12, 2); collection.Add (seg2); var seg3 = new TreeSegment (14, 2); collection.Add (seg3); var seg4 = new TreeSegment (11, 5); collection.Add (seg4); collection.Remove (seg2); Assert.AreEqual (3, collection.Segments.Count ()); collection.Remove (seg4); Assert.AreEqual (2, collection.Segments.Count ()); }
public void RemoveTest_RemoveAll() { SegmentTree st = new SegmentTree(0, 16); st.Add(2, 4); st.Remove(2, 4); Assert.IsNull(st.Root); }
public void TestInsertAtEnd () { var collection = new SegmentTree<TreeSegment> (); collection.Add (new TreeSegment (10, 0)); collection.UpdateOnTextReplace (this, new TextChangeEventArgs (10, 10, null, "\n")); var seg = collection.Segments.First (); Assert.AreEqual (10, seg.Offset); }
public void RemoveTest_SplitExistNode() { SegmentTree st = new SegmentTree(0, 16); st.Add(0, 16); st.Remove(0, 8); Assert.IsFalse(st.Root.Exist); Assert.IsNull(st.Root.LeftChild); Assert.IsNotNull(st.Root.RightChild); Assert.IsTrue(st.Root.RightChild.Exist); }
public static int Solve(int[] t) { int n = t.Length; int left, right, b; SegmentTree segmentTree = new SegmentTree(); for (int i = 0; i < n; i++) { if (t[i] <= n - 1) { right = (i - t[i] + n) % n; left = i + 1; if (left <= right) { segmentTree.Add(left, right); } else { segmentTree.Add(left, n - 1); segmentTree.Add(0, right); } } } int v = 0; int index = 0; for (int i = 0; i < n; i++) { b = segmentTree.Get(i); if (b > v) { v = b; index = i; } } return(index + 1); }
public void InvalidInputTest() { SegmentTree st = new SegmentTree(0, 16); st.Add(6, 10); try { st.Query(-3, 10); } catch (ArgumentException e) { Assert.AreEqual("Valid number must be in the interval [0, 16)", e.Message); } }
public static long Solve(long[] a, int n) { long result = 0; SegmentTree st = new SegmentTree(1, 10000000); for (int i = 0; i < n; i++) { result += st.Get(a[i]); st.Add(a[i]); } return(result); }
public void RemoveTest_RemoveLeaf() { SegmentTree st = new SegmentTree(0, 16); st.Add(2, 8); st.Remove(2, 4); Assert.IsNull(st.Root.LeftChild.LeftChild); Assert.IsNotNull(st.Root.LeftChild.RightChild); Assert.IsTrue(st.Root.LeftChild.RightChild.Exist); Assert.IsFalse(st.Root.Exist); Assert.IsFalse(st.Root.LeftChild.Exist); }
public void QueryTest() { SegmentTree st = new SegmentTree(0, 16); st.Add(6, 10); Assert.IsTrue(st.Query(6, 10)); Assert.IsTrue(st.Query(6, 8)); Assert.IsTrue(st.Query(8, 10)); Assert.IsFalse(st.Query(0, 4)); Assert.IsFalse(st.Query(0, 6)); Assert.IsFalse(st.Query(4, 10)); Assert.IsFalse(st.Query(8, 11)); Assert.IsFalse(st.Query(10, 16)); }
public void TestLenth1InsertionMove() { // use case: // {|_} // marked } with markerlength == 1 - typing the caret should move the marker according to } before it stayed in place and got overtyped. var collection = new SegmentTree <TreeSegment> (); collection.Add(new TreeSegment(216, 1)); collection.UpdateOnTextReplace(this, new TextChangeEventArgs(216, 10, null, "\n")); var seg = collection.Segments.First(); Assert.AreEqual(217, seg.Offset); Assert.AreEqual(1, seg.Length); }
public void Add(int p) { if (p <= e && p >= s) { count++; if (left != null) { left.Add(p); } if (right != null) { right.Add(p); } } }
static void Main() { var n = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split().Select(int.Parse).ToArray(); var r = 0L; var st = new SegmentTree(n + 1); for (int i = 0; i < n; i++) { r += i - st.Sum(0, a[i] + 1); st.Add(a[i], 1); } Console.WriteLine(r); }
public Projection(ITextDocument document, IReadOnlyList <ProjectedSegment> projectedSegments) { if (document == null) { throw new ArgumentNullException(nameof(document)); } this.Document = document; for (int i = 0; i < projectedSegments.Count; i++) { var p = projectedSegments [i]; var original = new ProjectedTreeSegment(p.Offset, p.Length); var projected = new ProjectedTreeSegment(p.ProjectedOffset, p.Length); original.LinkedTo = projected; projected.LinkedTo = original; originalProjections.Add(original); projectedProjections.Add(projected); } }
public ulong Solve() { SegmentTree tree = new SegmentTree() { Max = this.M }; ulong sum = 0; ulong result = 0; for (int i = 0; i < this.A.Length; i++) { sum = (sum + this.A[i]) % this.M; result = Math.Max(result, sum); ulong next = tree.Get(sum + 1); result = Math.Max(result, sum + this.M - next); tree.Add(sum); } return(result); }
public void TestUpdateOnTextReplace () { var collection = new SegmentTree<TreeSegment> (); collection.Add (new TreeSegment (0, 89)); collection.Add (new TreeSegment (92, 51)); collection.Add (new TreeSegment (42, 77)); collection.Add (new TreeSegment (36, 128)); collection.UpdateOnTextReplace (this, new DocumentChangeEventArgs (0, new string(' ', 355), null)); Assert.AreEqual (0, collection.Count); }