void Calc() { string[] str = Console.ReadLine().Split(' '); int N = int.Parse(str[0]); int M = int.Parse(str[1]); Data[] D = new Data[N]; for (int i = 0; i < N; i++) { str = Console.ReadLine().Split(' '); D[i] = new Data(int.Parse(str[0]), int.Parse(str[1])); } Array.Sort(D, (x, y) => (x.space - y.space)); SegTree Seg = new SegTree(M + 1, this); int p = 0; for (int i = 1; i <= M; i++) { int count; while (p < N && D[p].space < i) { Seg.Add(D[p].l, D[p].r); p++; } count = N - p; for (int j = i; j <= M; j += i) { count += Seg.Get(j); } sb.Append(count + "\n"); } }
void Calc() { string[] str = Console.ReadLine().Split(' '); N = int.Parse(str[0]); M = int.Parse(str[1]); str = Console.ReadLine().Split(' '); A = new int[N]; for (int i = 0; i < N; i++) { A[i] = int.Parse(str[i]); } SegTree Seg = new SegTree(M + 2, this); for (int i = 0; i < N - 1; i++) { if (A[i] < A[i + 1]) { Seg.Add(A[i] + 1, A[i + 1], A[i + 1] + 1, -1); Seg.Add(A[i + 1] + 1, M + 1, A[i + 1] - A[i], 0); Seg.Add(0, A[i], A[i + 1] - A[i], 0); } else { Seg.Add(A[i + 1] + 1, A[i], A[i + 1] - A[i] + M, 0); Seg.Add(A[i] + 1, M + 1, A[i + 1] + 1 + M, -1); Seg.Add(0, A[i + 1], A[i + 1] + 1, -1); } } long count = 1000000000000; for (int i = 1; i <= M; i++) { count = Math.Min(Seg.Get(i), count); } sb.Append(count + "\n"); }
void Calc() { string[] str = Console.ReadLine().Split(' '); int N = int.Parse(str[0]); long K = int.Parse(str[1]); long[] L = new long[N]; long[] R = new long[N]; int eN = 0; long sum = 0; { for (int i = 0; i < N; i++) { str = Console.ReadLine().Split(' '); long D = int.Parse(str[0]); bool b = int.Parse(str[1]) == 1; if (b && 2 * D > K) { sb.Append("-1\n"); return; } if (b) { L[eN] = K - (((sum + 2 * D) % K) == 0 ? K : ((sum + 2 * D) % K)); R[eN] = K - ((sum % K) == 0 ? K : sum % K); eN++; } sum += 2 * D; } } long[] DPL = new long[eN]; long[] DPR = new long[eN]; long[] Points = new long[2 * eN]; int[] A = new int[2 * eN]; for (int i = 0; i < eN; i++) { Points[2 * i] = L[i]; Points[2 * i + 1] = R[i]; A[2 * i] = 2 * i; A[2 * i + 1] = 2 * i + 1; } Array.Sort(Points, A); int[] B = new int[2 * eN]; for (int i = 0; i < 2 * eN; i++) { B[A[i]] = i; } SegTree Seg = new SegTree(2 * eN, this); for (int i = 4 * eN - 2; i >= 0; i--) { Seg.X[i] = eN; } for (int i = eN - 1; i >= 0; i--) { int lp = B[2 * i]; int rp = B[2 * i + 1]; int gL = Seg.Get(lp); DPL[i] = gL == eN ? 0 : DPR[gL] + (K + Points[B[2 * gL + 1]] - Points[lp]) % K; int gR = Seg.Get(rp); DPR[i] = gR == eN ? 0 : DPR[gR] + (K + Points[B[2 * gR + 1]] - Points[rp]) % K; int bf = 0; int bl = 2 * eN; long C = Points[B[2 * i]]; while (bf != bl) { int bc = (bf + bl) / 2; if (bc == 2 * eN || C < Points[bc]) { bl = bc; } else { bf = bc + 1; } } int l = bf; bf = -1; bl = 2 * eN - 1; C = Points[B[2 * i + 1]]; while (bf != bl) { int bc = (bf + bl + 1) / 2; if (bc == -1 || C > Points[bc]) { bf = bc; } else { bl = bc - 1; } } int r = bf; if (Points[lp] < Points[rp]) { if (l <= r) { Seg.Add(i, l, r); } } else { if (l != 2 * eN) { Seg.Add(i, l, 2 * eN - 1); } if (r != -1) { Seg.Add(i, 0, r); } } } for (int i = 4 * eN - 2; i >= 0; i--) { Seg.X[i] = eN; } long min = DPR[0]; for (int i = 0; i < eN; i++) { int lp = B[2 * i]; int rp = B[2 * i + 1]; int gL = Seg.Get(lp); if (gL == eN) { min = Math.Min(min, DPL[i]); } int gR = Seg.Get(rp); if (gR == eN) { min = Math.Min(min, DPR[i]); } int bf = 0; int bl = 2 * eN; long C = Points[B[2 * i]]; while (bf != bl) { int bc = (bf + bl) / 2; if (bc == 2 * eN || C < Points[bc]) { bl = bc; } else { bf = bc + 1; } } int l = bf; bf = -1; bl = 2 * eN - 1; C = Points[B[2 * i + 1]]; while (bf != bl) { int bc = (bf + bl + 1) / 2; if (bc == -1 || C > Points[bc]) { bf = bc; } else { bl = bc - 1; } } int r = bf; if (Points[lp] < Points[rp]) { if (l <= r) { Seg.Add(0, l, r); } } else { if (l != 2 * eN) { Seg.Add(0, l, 2 * eN - 1); } if (r != -1) { Seg.Add(0, 0, r); } } } sb.Append((min + sum) + "\n"); }