static object Solve() { var n = int.Parse(Console.ReadLine()); var a = Read(); var map = new CompressionHashMap(a); var m = map.Count; var dpu = new BIT(m); var dpd = new BIT(m); foreach (var v in a) { var i = map[v] + 1; dpu.Add(i, dpd.Sum(i - 1)); dpd.Add(i, dpu.Sum(i + 1, m + 1)); dpu.Add(i, 1); dpd.Add(i, 1); dpu[i] %= M; dpd[i] %= M; } return((dpu.Sum(m) + dpd.Sum(m) - 2 * n + M) % M); }
// i 以下のインデックスのうち、交換回数が k 以下となる最小値 static int[] GetLeft(int n, long k, int[] a) { var left = new int[n]; var bit = new BIT(n); var inv = 0L; var ui = new bool[n]; var uj = new bool[n]; var q = TwoPointers(n, n, (i, j) => { if (!uj[j] && j > 0) { inv -= bit.Sum(1, a[j - 1]); bit.Add(a[j - 1], -1); } if (!ui[i]) { inv += bit.Sum(a[i] + 1, n + 1); bit.Add(a[i], 1); } ui[i] = uj[j] = true; return(inv <= k); }); foreach (var(i, j) in q) { left[i] = j; } return(left); }
static void Main(string [] args) { int [] a = { 3, 6, 1, 7, 2, 8, 4, 9, 10 }; var bit = new BIT(a.Length); bit.Build(a); bit.Print(); Console.WriteLine(bit.Sum(a.Length - 1)); bit.Update(3, 2); Console.WriteLine(bit.Sum(1, 3)); }
public void Solve() { for (int i = 0; i < N; i++) { A[i] -= K; } long[] sum = new long[N + 1]; for (int i = 0; i < N; i++) { sum[i + 1] = sum[i] + A[i]; } long Inf = (long)1e18; HashSet <long> H = new HashSet <long>(); H.Add(-Inf); H.Add(Inf); H.Add(0); foreach (var v in sum) { H.Add(v); } var L = H.ToList(); L.Sort(); var dic = new Dictionary <long, int>(); for (int i = 0; i < L.Count; i++) { dic.Add(L[i], i); } int NN = N * 2; var BT = new BIT(NN); for (int i = 1; i <= N; i++) { BT.Add(dic[sum[i]] + 1, 1); } long ans = 0; ans += N - BT.Sum(dic[L[dic[0]]]); BT.Add(dic[sum[1]] + 1, -1); BT.Add(dic[-Inf] + 1, 1); for (int i = 1; i < N; i++) { ans += N - BT.Sum(dic[L[dic[sum[i]]]]); BT.Add(dic[sum[i + 1]] + 1, -1); BT.Add(dic[-Inf] + 1, 1); } Console.WriteLine(ans); }
static void Main() { Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); var h = Read(); var n = h[0]; var a = Read(); var st = new BIT(n); for (int i = 0; i < n; i++) { st.Add(i + 1, a[i]); } for (int k = 0; k < h[1]; k++) { var q = Read(); if (q[0] == 0) { st.Add(q[1] + 1, q[2]); } else { Console.WriteLine(st.Sum(q[1] + 1, q[2] + 1)); } } Console.Out.Flush(); }
static void Main() { var(n, qc) = Read2(); var qs = Array.ConvertAll(new bool[qc], _ => Read()); var bit = new BIT(n); Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); foreach (var q in qs) { var k = q[1]; if (q[0] == 1) { var v = bit.Sum(k <= n ? n - k + 1 : k - n, n + 1); Console.WriteLine(v % 2 == 0 ? k : 2 * n - k + 1); } else { bit.Add(k, 1); } } Console.Out.Flush(); }
static object Solve() { var n = int.Parse(Console.ReadLine()); var a = Read(); var p2 = MPows(2, n); var p2_ = MPows(MHalf, n); var map = new CompressionHashMap(a); var bit = new BIT(map.Count); var r = 0L; for (int i = 0; i < n; i++) { var v = map[a[i]] + 1; r += bit.Sum(v) % M * p2_[n - i]; r %= M; bit.Add(v, p2[n - 1 - i]); } return(r); }
static void Main() { Console.WriteLine("I love you"); int [] arr = { 4, 6, 1, 8, 3, 2, 9, 0, 5, 7 }; var bit = new BIT(arr); bit.Display(); Console.WriteLine(); for (int i = 0; i < arr.Length; i++) { Console.Write(bit.Sum(i) + " "); } bit.Update(10, 7); Console.WriteLine(); for (int i = 0; i < arr.Length; i++) { Console.Write(bit.Sum(i) + " "); } Console.Write(bit.Sum(1) - bit.Sum(0)); }
public void Solve() { var h = new HashSet <long>(); h.Add(0); for (int i = 0; i < N; i++) { h.Add(A[i]); } var l = h.ToList(); l.Sort(); var dic = new Dictionary <long, int>(); for (int i = 0; i < l.Count; i++) { dic.Add(l[i], i); } long max = 0; List <int> pos = new List <int>(); for (int i = 0; i < N; i++) { if (max < A[i]) { pos.Add(i); max = A[i]; } } var cntBT = new BIT(N); var totBT = new BIT(N); for (int i = 0; i < N; i++) { cntBT.Add(dic[A[i]] + 1, 1); totBT.Add(dic[A[i]] + 1, A[i]); } long total = A.Sum(); long done = 0; long[] ans = new long[N]; for (int i = pos.Count - 1; i > 0; i--) { long del = (total - totBT.Sum(dic[A[pos[i - 1]]])) - A[pos[i - 1]] * (N - cntBT.Sum(dic[A[pos[i - 1]]])); ans[pos[i]] = del - done; done = del; } ans[0] = total - done; Console.WriteLine(String.Join("\n", ans)); }
public static long InversionNumber(int n, int[] a) { var r = 0L; var bit = new BIT(n); for (int i = 0; i < n; ++i) { r += i - bit.Sum(a[i]); bit.Add(a[i], 1); } return(r); }
public static long Inversion(int[] s) { var ans = 0L; var bit = new BIT(s.Length); for (var j = 0; j < s.Length; j++) { ans += j - bit.Sum(s[j]); bit.Add(s[j], 1); } return(ans); }
static void Main() { var n = int.Parse(Console.ReadLine()); var a = Console.ReadLine().Split().Select(int.Parse).Select((x, i) => new { x, i }).OrderBy(_ => _.x).ToArray(); var st = new BIT(n); var r = 0L; for (int i = 0; i < n; i++) { r += st.Sum(a[i].i + 1, n + 1); st.Add(a[i].i + 1, 1); } Console.WriteLine(r); }
static object Solve() { var(n, k) = ((int, long))Read2L(); var a = Read(); var compMap = new CompressionHashMap(a); a = compMap.Compressed; a = Array.ConvertAll(a, x => x + 1); var left = GetLeft(n, k, a); var bit = new BIT(n + 1); bit[1] = 1; for (int i = 0; i < n; i++) { bit[i + 2] = bit.Sum(left[i] + 1, i + 2) % M; } return(bit[n + 1]); }
static void Main() { var k = int.Parse(Console.ReadLine()); var a = new int[k].Select(_ => { Console.ReadLine(); return(Read()); }).ToArray(); Console.ReadLine(); var b = Read(); var t = a.Select(ai => Tally(ai, 20)).ToArray(); var inv = a.Select(ai => { var sum = 0L; var bit = new BIT(20); foreach (var x in ai) { sum += bit.Sum(x + 1, 21); bit.Add(x, 1); } return(sum); }).ToArray(); var st = new BIT(20); var r = 0L; foreach (var bi in b) { for (int x = 1; x <= 20; x++) { r = (r + st.Sum(x + 1, 21) * t[bi - 1][x]) % M; } r = (r + inv[bi - 1]) % M; for (int x = 1; x <= 20; x++) { st.Add(x, t[bi - 1][x]); } } Console.WriteLine(r); }
public void Solve() { int N = S.Length; int M = T.Length; var BTSA = new BIT(N); var BTSB = new BIT(N); var BTTA = new BIT(N); var BTTB = new BIT(N); for (int i = 0; i < N; i++) { if (S[i] == 'A') { BTSA.Add(i + 1, 1); } if (S[i] == 'B') { BTSB.Add(i + 1, 1); } } for (int i = 0; i < M; i++) { if (T[i] == 'A') { BTTA.Add(i + 1, 1); } if (T[i] == 'B') { BTTB.Add(i + 1, 1); } } int[][] jud = new int[3][]; jud[0] = new int[] { 0, -1, 1 }; jud[1] = new int[] { 1, 0, -1 }; jud[2] = new int[] { -1, 1, 0 }; var sbd = new StringBuilder(); for (int q = 0; q < Q; q++) { int a = Query[q][0]; int b = Query[q][1]; int c = Query[q][2]; int d = Query[q][3]; int sa = BTSA.Sum(b) - BTSA.Sum(a - 1); int sb = BTSB.Sum(b) - BTSB.Sum(a - 1); int ta = BTTA.Sum(d) - BTTA.Sum(c - 1); int tb = BTTB.Sum(d) - BTTB.Sum(c - 1); sa %= 3; sb %= 3; ta %= 3; tb %= 3; if (jud[sa][sb] == jud[ta][tb]) { sbd.AppendLine("YES"); } else { sbd.AppendLine("NO"); } } Console.Write(sbd.ToString()); }