private void VerifySmall(int empty, Func <int, int, int> append) { const int Min = -100; const int Max = 100; var random = new Random(); for (var count = 1; count < 15; count++) { var array = Enumerable.Repeat(0, count).Select(_ => random.Next(Min, Max)).ToArray(); var tree = SegmentTree.Create(array, empty, append); tree.Count.Is(count); tree.IsSeq(array); Verify(array, tree); for (var s = 0; s < 20; s++) { var i = random.Next(0, count); var item = random.Next(Min, Max); tree[i] = item; array[i] = item; Verify(array, tree); } } }
public void Run() { { n = ReadLine(int.Parse)[0]; ps = ReadLine(int.Parse); } qs = new List <int>(n); allCells = ps.Select((p, i) => new Cell(p, i)).ToArray(); trees = Enumerable.Range(0, 2).Select(parity => SegmentTree.Create( allCells.Select((c, i) => i % 2 == parity ? c : Cell.Max), Cell.Max, (l, r) => Comparer <int> .Default.Compare(l.Value, r.Value) > 0 ? r : l )) .ToArray(); heap = BinaryHeap.Create <Range>( (l, r) => Comparer <int> .Default.Compare(l.EvenCell.Value, r.EvenCell.Value) ); AddRange(0, allCells.Length); while (heap.Count > 0 && qs.Count < n) { var range = heap.Dequeue(); var ec = range.EvenCell; var oc = trees[1 - range.Parity].Query(ec.Index, range.EndIndex - ec.Index); qs.Add(ec.Value); qs.Add(oc.Value); trees[ec.Index % 2][ec.Index] = Cell.Max; trees[oc.Index % 2][oc.Index] = Cell.Max; AddRange(range.Index, ec.Index); AddRange(ec.Index + 1, oc.Index); AddRange(oc.Index + 1, range.EndIndex); } WriteLineOne(string.Join(" ", qs)); }
public static void Solve() { var n = I; var m = I; var p = new long[m]; var a = new double[m]; var b = new double[m]; Repeat(m, i => { p[i] = L - 1; a[i] = D; b[i] = D; }); if (m == 0) { Console.WriteLine(1.0); Console.WriteLine(1.0); return; } var comp = new CoordinateCompressor <long>(p); var st = SegmentTree.Create(m, Monoid.Create( Tuple.Create(1.0, 0.0), (x, y) => Tuple.Create(x.Item1 * y.Item1, x.Item2 * y.Item1 + y.Item2))); var min = 1.0; var max = 1.0; Repeat(m, i => { var cp = comp.Compress[p[i]]; st[cp] = Tuple.Create(a[i], b[i]); var crt = st[0, m - 1]; var d = crt.Item1 + crt.Item2; min = Min(min, d); max = Max(max, d); }); Console.WriteLine(min); Console.WriteLine(max); }
public void Test_large() { var array = new long[10000]; for (var i = 0; i < array.Length; i++) { array[i] = i * i; } var tree = SegmentTree.Create(array, 0L, (x, y) => x + y); var l = 2345; var r = 8765; var expected = 220123632470L; tree.Query(l, r - l).Is(expected); tree[5555] = 1; expected += 1 - 5555 * 5555; tree[r] = 0; tree.Query(l, r - l).Is(expected); }
public void Run() { #if MEASURE using (mr.Measure()) #endif { n = ReadLineInt32()[0]; ps = ReadLineInt32(); } allCells = new Cell[ps.Length]; for (var i = 0; i < ps.Length; i++) { allCells[i] = new Cell(ps[i], i); } #if MEASURE using (mt1.Measure()) #endif { trees = new SegmentTree <Cell, CellMonoid> [2]; for (var parity = 0; parity < 2; parity++) { var cells = new Cell[n]; for (var i = 0; i < cells.Length; i++) { cells[i] = i % 2 == parity ? allCells[i] : Cell.Max; } trees[parity] = SegmentTree.Create(cells, new CellMonoid()); } } #if MEASURE using (mh.Measure()) #endif { heap = new BinaryHeap <Range, RangeComparer>(new List <Range>(n), new RangeComparer()); } AddRange(0, allCells.Length); var outputCount = 0; while (heap.Count > 0) { var range = default(Range); #if MEASURE using (mh.Measure()) #endif { range = heap.Dequeue(); } var ec = range.EvenCell; var oc = default(Cell); #if MEASURE using (mt2.Measure()) #endif { oc = trees[1 - range.Parity].Query(ec.Index, range.EndIndex - ec.Index); } #if MEASURE using (mr.Measure()) #endif { if (outputCount != 0) { output.Write(' '); } output.Write(ec.Value); output.Write(' '); output.Write(oc.Value); outputCount += 2; if (outputCount >= n) { output.WriteLine(); break; } } #if MEASURE using (mt3.Measure()) #endif { trees[ec.Index % 2][ec.Index] = Cell.Max; trees[oc.Index % 2][oc.Index] = Cell.Max; } AddRange(range.Index, ec.Index); AddRange(ec.Index + 1, oc.Index); AddRange(oc.Index + 1, range.EndIndex); } #if MEASUSRE var measures = new Dictionary <string, Measurement>() { { "I/O", mr }, { "Heap", mh }, { "Tree Init", mt1 }, { "Tree Query", mt2 }, { "Tree Set", mt3 }, }; var total = measures.Sum(m => m.Value.Total); var log = Console.Error; log.WriteLine($"Total = {total}"); foreach (var m in measures.OrderBy(m => m.Value.Total)) { log.WriteLine($"{m.Key}: {(double)m.Value.Total / total * 100}%"); } #endif }
public void Run() { #if MEASURE using (mr.Measure()) #endif { n = int.Parse(input.ReadLine()); ps = ReadLineInt32(n); } #if MEASURE using (mo.Measure()) #endif { indexFromValue = new int[ps.Length + 1]; for (var i = 0; i < ps.Length; i++) { indexFromValue[ps[i]] = i; } rangeFromValue = new Range[ps.Length + 1]; } #if MEASURE using (mt1.Measure()) #endif { trees = new SegmentTree <int, Int32MinMonoid> [2]; for (var parity = 0; parity < 2; parity++) { trees[parity] = SegmentTree.Create(ps, parity, new Int32MinMonoid()); } } #if MEASURE using (mh.Measure()) #endif { heap = new BinaryHeap <int, Int32Comparer>(new List <int>(n / 2), new Int32Comparer()); } AddRange(0, ps.Length); var outputCount = 0; while (heap.Count > 0) { var ec = default(int); #if MEASURE using (mh.Measure()) #endif { ec = heap.Dequeue(); } var ei = indexFromValue[ec]; var range = rangeFromValue[ec]; var oc = default(int); #if MEASURE using (mt2.Measure()) #endif { oc = trees[1 - range.Parity].Query(ei, range.EndIndex - ei); } var oi = indexFromValue[oc]; #if MEASURE using (mr.Measure()) #endif { if (outputCount != 0) { output.Write(' '); } output.Write(ec); output.Write(' '); output.Write(oc); outputCount += 2; if (outputCount >= n) { output.WriteLine(); break; } } #if MEASURE using (mt3.Measure()) #endif { trees[ei & 1][ei] = int.MaxValue; trees[oi & 1][oi] = int.MaxValue; } AddRange(range.Index, ei); AddRange(ei + 1, oi); AddRange(oi + 1, range.EndIndex); } #if MEASURE var measures = new Dictionary <string, Measurement>() { { "Misc", mo }, { "I/O", mr }, { "Heap", mh }, { "Tree Init", mt1 }, { "Tree Query", mt2 }, { "Tree Set", mt3 }, }; var total = measures.Sum(m => m.Value.Total); var log = Console.Error; log.WriteLine($"Total = {total}"); foreach (var m in measures.OrderBy(m => m.Value.Total)) { log.WriteLine($"{m.Key}: {(double)m.Value.Total / total * 100}%"); } #endif }