public void Solve() { int N = Reader.Int(); int[] X = new int[N], V = new int[N]; for (int i = 0; i < N; i++) { X[i] = Reader.Int(); V[i] = Reader.Int(); } V = Compress(V); Array.Sort(X, V); int[] L = new int[N], R = new int[N]; for (int i = 0, max = -1; i < N; i++) { R[i] = max = Math.Max(max, V[i] + 1); } for (int i = N - 1, min = int.MaxValue; i >= 0; i--) { L[i] = min = Math.Min(min, V[i]); } var BIT = new BinaryIndexedTree(N + 1, Mod); BIT.Add(0, 1); for (int i = 0; i < N; i++) { BIT.Add(R[i], BIT.Sum(L[i], R[i] + 1)); } Console.WriteLine(BIT.Sum(N, N + 1)); }
public void BITInitializationZeroTest() { const int length = 5; var bit = new BinaryIndexedTree(length); Assert.Equal(length, bit.Length); Assert.Equal(0, bit.Sum(length)); }
string Solve(int N, int[] who, int[] index) { Array.Sort(index, who); var ans = new int[N * N]; int L = 0; var bit = new BinaryIndexedTree(N * N); for (int i = 0; i < N * N; i++) { bit.Add(i, 1); } for (int i = 0; i < N; i++) { if (bit.Sum(index[i]) < who[i]) { return(No); } ans[index[i]] = who[i]; bit.Add(index[i], -1); int num = who[i] - 1; for (; L < ans.Length && num > 0; L++) { if (ans[L] == 0) { num--; bit.Add(L, -1); ans[L] = who[i]; } } if (num > 0) { return(No); } } for (int i = 0; i < N; i++) { for (int num = N - who[i]; L < ans.Length && num > 0; L++) { if (ans[L] == 0) { if (L < index[i]) { return(No); } ans[L] = who[i]; num--; } } } return(Yes + "\n" + string.Join(" ", ans)); }
void Solve() { var N = F(); var B = G(); var rev = new int[N]; var bit = new BinaryIndexedTree(N); for (var i = 0; i < N; i++) { rev[--B[i]] = i; bit.Add(i, 1); } var s = 0L; for (var i = 0; i < N; i++) { s += Min(bit.Sum(0, rev[i]), bit.Sum(rev[i] + 1, N)); bit.Add(rev[i], -1); } WriteLine(s); }
public override IEnumerable <object> Solve(TextReader inputStream) { var(sitesCount, queriesCount) = inputStream.ReadValue <int, int>(); var yCoordinates = new HashSet <int>(); var queries = new List <Query>(); var toAnswer = new Queue <SumQuery>(); var answers = new Dictionary <SumQuery, long>(); for (int i = 0; i < sitesCount; i++) { var(x, y, d, c) = inputStream.ReadValue <int, int, int, int>(); yCoordinates.Add(y); yCoordinates.Add(y + d + 1); queries.Add(new AddQuery(x, y, d, c)); queries.Add(new AddQuery(x + d + 1, y, d, -c)); } for (int i = 0; i < queriesCount; i++) { var(a, b) = inputStream.ReadValue <int, int>(); yCoordinates.Add(b); var query = new SumQuery(a, b); queries.Add(query); toAnswer.Enqueue(query); } queries.Sort(); var shrinker = new CoordinateShrinker <int>(yCoordinates); var costs = new BinaryIndexedTree(shrinker.Count); foreach (var query in queries) { var shrinkedY = shrinker.Shrink(query.Y); if (query is AddQuery addQuery) { var shrinkedYPlusD = shrinker.Shrink(addQuery.Y + addQuery.Distance + 1); costs.AddAt(shrinkedY, addQuery.Cost); costs.AddAt(shrinkedYPlusD, -addQuery.Cost); } else if (query is SumQuery sumQuery) { answers.Add(sumQuery, costs.Sum(shrinkedY + 1)); } } foreach (var query in toAnswer) { yield return(answers[query]); } }
public void BITSumFromZeroTest() { var a = new long[] { 5, 3, 7, 9, 8 }; long[] sum = GetPrefixSum(a); var bit = new BinaryIndexedTree(a); Assert.Equal(a.Length, bit.Length); for (int i = 0; i < sum.Length; i++) { Assert.Equal(sum[i], bit.Sum(i)); } }
public void BITAddTest() { var a = new long[] { 5, 3, 7, 9, 8 }; var sum = GetPrefixSum(a); var bit = new BinaryIndexedTree(a); Assert.Equal(a.Length, bit.Length); for (int i = 0; i < sum.Length; i++) { Assert.Equal(sum[i], bit.Sum(i)); } bit.AddAt(2, 3); a[2] += 3; sum = GetPrefixSum(a); Assert.Equal(a.Length, bit.Length); for (int i = 0; i < sum.Length; i++) { Assert.Equal(sum[i], bit.Sum(i)); Assert.Equal(sum[i], bit.Sum(^ (a.Length - i))); } }
public override void Solve(IOManager io) { Modular.Mod = 1000000007; var n = io.ReadInt(); var k = io.ReadInt(); var numbers = new int[n][]; var all = new int[n * k + 1]; // include 0 for (int i = 0; i < numbers.Length; i++) { numbers[i] = io.ReadIntArray(k); numbers[i].AsSpan().CopyTo(all.AsSpan(k * i)); } var shrinker = new CoordinateShrinker <int>(all); var last = new BinaryIndexedTree(shrinker.Count); last[0] = 1; for (int i = 0; i < numbers.Length; i++) { var current = new BinaryIndexedTree(shrinker.Count); foreach (var value in numbers[i]) { var index = shrinker.Shrink(value); current[index] = last.Sum(index + 1); } last = current; } io.WriteLine(last.Sum(last.Length)); }
public void BITSumFromAnyTest() { var a = new long[] { 5, 3, 7, 9, 8 }; var sum = GetPrefixSum(a); var bit = new BinaryIndexedTree(a); Assert.Equal(a.Length, bit.Length); for (int begin = 0; begin < bit.Length; begin++) { for (int end = begin; end <= bit.Length; end++) { Assert.Equal(sum[end] - sum[begin], bit.Sum(begin, end)); } } }
public long findNumberOfSwaps(string P, int A0, int X, int Y, int N, int K) { string s = Initialize(P, A0, X, Y, N); long count = 0; for (int begin = 0; begin < K; begin++) { var bit = new BinaryIndexedTree(26); for (int i = begin; i < s.Length; i += K) { var c = s[i] - 'a'; bit[c]++; count += bit.Sum(c); } } return(count); }
void Solve() { var S = Console.ReadLine(); var N = S.Length; var s = S.Select(c => c - '0').ToArray(); var bit = new BinaryIndexedTree(s); int k = 0, l = 0, r = N; while (true) { var t = bit.Sum(l, r); if (t == 0 || t == r - l) { break; } bit.Add(l, -s[l]); bit.Add(r - 1, -s[r - 1]); l++; r--; k++; } Console.WriteLine(N - k); }
public long countColoring(int n, int xzero, int xmul, int xadd, int xmod, int yzero, int ymul, int yadd, int ymod) { int[] xs = new int[n], ys = new int[n]; Point[] points = new Point[n]; xs[0] = xzero; ys[0] = yzero; points[0] = new Point(xs[0], ys[0]); for (int i = 1; i < n; ++i) { xs[i] = (int)(((long)points[i - 1].X * xmul + xadd) % xmod); ys[i] = (int)(((long)points[i - 1].Y * ymul + yadd) % ymod); points[i] = new Point(xs[i], ys[i]); } Array.Sort(xs); Array.Sort(ys); for (int i = 0; i < n; ++i) { points[i].X = Array.BinarySearch(xs, points[i].X) + 1; points[i].Y = Array.BinarySearch(ys, points[i].Y) + 1; } Array.Sort(points); long count = 0; BinaryIndexedTree tree; tree = new BinaryIndexedTree(n); int[] countL = new int[n], countR = new int[n]; for (int i = 0; i < n; ++i) { countL[i] = tree.Sum(points[i].Y); tree.Add(points[i].Y, 1); } tree = new BinaryIndexedTree(n); for (int i = n - 1; i >= 0; --i) { countR[i] = tree.Sum(n) - tree.Sum(points[i].Y); count += (long)countR[i] * (countR[i] - 1) / 2; count -= (long)countL[i] * countR[i]; tree.Add(points[i].Y, 1); } return count; }
void Solve() { var N = F(); var A = G(); var B = G(); var rev = new int[N]; for (var i = 0; i < N; i++) { rev[--A[i]] = i; } for (var i = 0; i < N; i++) { B[i] = rev[B[i] - 1]; } var inv = new int[N]; var s = 0L; var bit = new BinaryIndexedTree(N); for (var i = 0; i < N; i++) { s += inv[B[i]] = bit.Sum(B[i], N); bit.Add(B[i], 1); } if (s % 2 == 1) { Console.WriteLine(-1); } else { if (s > 0) { s /= 2; var done = 0; while (s >= inv[done]) { s -= inv[done++]; } var k = N - 1; for (var j = N - 1; j >= done; j--) { while (B[k] < done) { k--; } B[j] = B[k--]; } for (var j = 0; j < done; j++) { B[j] = j; } for (var j = done; s > 0 && j < N; j++) { for (k = N - 1; s > 0 && k > j; k--) { if (B[k - 1] > B[k]) { var t = B[k]; B[k] = B[k - 1]; B[k - 1] = t; s--; } } } } for (var i = 0; i < N; i++) { B[i] = A[B[i]] + 1; } Console.WriteLine(string.Join(" ", B)); } }