public void Solve() { Pair[] P = new Pair[N]; for (int i = 0; i < N; i++) { P[i] = new Pair(i, R[i] - L[i] + 1); } Array.Sort(P, (p, q) => p.Len.CompareTo(q.Len)); var ST = new SegTree(M + 1); int bigger = N; int ptr = 0; var ans = new List <long>(); for (int i = 1; i <= M; i++) { long cnt = 0; while (ptr < N && P[ptr].Len <= i) { ST.AddRange(L[P[ptr].Idx], R[P[ptr].Idx] + 1, 1); bigger--; ptr++; } cnt += bigger; for (int j = 0; j <= M; j += i) { cnt += ST.At(j); } ans.Add(cnt); } Console.WriteLine(String.Join("\n", ans)); }
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"); } }
static void Main() { var NQ = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int N = NQ[0], Q = NQ[1]; var st = new SegTree <long>(N, delegate(long x, long y) { return(x ^ y); }); var A = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); for (int i = 0; i < N; ++i) { st.Update(i, A[i]); } for (int i = 0; i < Q; ++i) { var TXY = Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); long T = TXY[0], X = TXY[1], Y = TXY[2]; if (T == 1) { A[X - 1] ^= Y; st.Update((int)X - 1, A[X - 1]); } else { Console.WriteLine(st.Query((int)X - 1, (int)Y)); } } }
void Calc() { string[] str = Console.ReadLine().Split(' '); int N = int.Parse(str[0]); long K = int.Parse(str[1]); long[] A = new long[N]; for (int i = 0; i < N; i++) { A[i] = int.Parse(Console.ReadLine()) - K; } N++; long[] sum = new long[N]; for (int i = 1; i < N; i++) { sum[i] = sum[i - 1] + A[i - 1]; } long[] B = new long[N]; int[] C = new int[N]; for (int i = 0; i < N; i++) { B[i] = sum[i]; C[i] = i; } Array.Sort(C, (x, y) => (B[x] > B[y] ? 1 : (B[x] == B[y] ? (x > y ? 1 : (x == y ? 0 : -1)) : -1))); SegTree Seg = new SegTree(N, this); long count = 0; for (int i = N - 1; i >= 0; i--) { count += Seg.Sum(C[i], N - 1); Seg.Add(C[i]); } sb.Append(count + "\n"); }
public static void Solve() { var N = Scanner.Scan <int>(); var S = Scanner.Scan <string>().Select(x => 1 << (x - 'a')).ToArray(); var Q = Scanner.Scan <int>(); var seg = new SegTree <int>(S, (x, y) => x | y, 0); for (var q = 0; q < Q; q++) { var query = Scanner.ScanEnumerable <string>().ToArray(); if (query[0] == "1") { var(i, c) = (int.Parse(query[1]) - 1, query[2][0] - 'a'); seg.Set(i, 1 << c); } else { var(l, r) = (int.Parse(query[1]) - 1, int.Parse(query[2])); var bit = seg.Prod(l, r); var answer = 0; for (var i = 0; i < 26; i++) { if ((bit >> i & 1) == 1) { answer++; } } Console.WriteLine(answer); } } }
static void Method(string[] args) { int[] nq = ReadInts(); int n = nq[0]; int q = nq[1]; int[] cs = ReadInts(); int[][] queries = new int[q][]; for (int i = 0; i < q; i++) { int[] lr = ReadInts(); queries[i] = new int[4] { lr[0] - 1, lr[1] - 1, 0, i }; } int[] prevs = new int[n]; int[] nexts = new int[n]; for (int i = n - 1; i >= 0; i--) { if (prevs[cs[i] - 1] > 0) { nexts[i] = prevs[cs[i] - 1]; } prevs[cs[i] - 1] = i; } SegTree <int> tree = new SegTree <int>(n, 0, (a, b) => a + b, 0); for (int i = 0; i < n; i++) { if (prevs[cs[i] - 1] == i) { tree.Update(i, 1); } } queries = queries.OrderBy(a => a[0]).ToArray(); int left = 0; for (int i = 0; i < q; i++) { while (left < queries[i][0]) { if (nexts[left] > 0) { tree.Update(nexts[left], 1); } left++; } queries[i][2] = tree.Scan(queries[i][0], queries[i][1] + 1); } queries = queries.OrderBy(a => a[3]).ToArray(); for (int i = 0; i < q; i++) { WriteLine(queries[i][2]); } }
private void Scan() { N = int.Parse(Console.ReadLine()); var line = Console.ReadLine().Split(' '); Pq = new PQ(); I[] kk = new I[N]; I[] gg = new I[N]; for (int i = 0; i < N; i++) { int a = int.Parse(line[i]); if (i % 2 == 1) { kk[i] = new I(a, i); gg[i] = new I(int.MaxValue, -1); } else { gg[i] = new I(a, i); kk[i] = new I(int.MaxValue, -1); } } G = new SegTree(gg); K = new SegTree(kk); Pq.Push(new S(0, N, G.Q(0, N))); }
static Action Solve() { int[] nk = ReadInts(); int n = nk[0]; int k = nk[1]; int[] cs = ReadInts(); int[] ds = ReadInts(); SegTree <int> segTreeC = new SegTree <int>(n, Max, 0); SegTree <int> segTreeD = new SegTree <int>(n, Max, 0); for (int i = 0; i < n; i++) { segTreeC.Update(i, cs[i]); segTreeD.Update(i, ds[i]); } long cnt = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j <= n; j++) { int delta = Abs(segTreeC.Scan(i, j) - segTreeD.Scan(i, j)); if (delta <= k) { cnt++; } } } return(() => WriteLine(cnt)); }
void Calc() { int N = int.Parse(Console.ReadLine()); string[] str = Console.ReadLine().Split(' '); SegTree[] S = new SegTree[N]; int p = 0; rs = new long[100001]; Half = Rev(2); for (int i = 0; i < N; i++) { int x = int.Parse(str[i]); if (x > 0) { S[p] = new SegTree(x); p++; } else if (x == 0) { S[p - 2] = Merge(S[p - 2], S[p - 1]); p--; } else { Multi(S[p - 1], -x); } } sb.Append(S[0].c1 + "\n"); }
void Multi(SegTree S, int M) { S.M = M * S.M % Define.mod; S.rM = Rev(M) * S.rM % Define.mod; S.c1 = (S.c1 * M + S.c2 * M % Define.mod * (M - 1) % Define.mod * Half) % Define.mod; S.c2 = S.c2 * M % Define.mod * M % Define.mod; }
static void Main() { var N = int.Parse(Console.ReadLine()); var L = new List <Point>(); for (int i = 0; i < N; ++i) { var buf = Console.ReadLine().Split(new char[] { ' ' }); var p = new Point(); p.w = int.Parse(buf[0]); p.h = int.Parse(buf[1]); L.Add(p); } L.Sort(delegate(Point p1, Point p2){ if (p1.h != p2.h) { return(p1.h - p2.h); } return(-(p1.w - p2.w)); }); var st = new SegTree <int>(100001, Math.Max); foreach (var p in L) { st.Update(p.w, st.Query(0, p.w) + 1); } Console.WriteLine(st.Query(0, st.Count)); }
void Calc() { int N = re.i(); int[] B = re.ia(); int Q = re.i(); Data[] D = new Data[Q]; for (int i = 0; i < Q; i++) { D[i] = new Data(re.i() - 1, re.i() - 1); } Array.Sort(D, (x, y) => (x.l - y.l)); SegTree Seg = new SegTree(N + 1, this); for (int i = 2 * N; i >= 0; i--) { if (i >= Seg.segf) { Seg.X[i] = Seg.ToPoint(i) == 0 ? 0 : 10000000; } else { int sl = i * 2 + 1; int sr = i * 2 + 2; Seg.X[i] = Math.Min(Seg.X[sl], Seg.X[sr]); } } int p = 0; { while (p < Q && D[p].l == 0) { Seg.Change(D[p].r + 1, 0); p++; } } for (int i = 0; i < N; i++) { if (i != 0) { Seg.Change(i, Seg.Min(i - 1, i)); } if (B[i] == 1) { Seg.Add(i, i, 1); } else { Seg.Add(i + 1, N, 1); } while (p < Q && D[p].l == i + 1) { Seg.Change(D[p].r + 1, Seg.Min(i, D[p].r + 1)); p++; } } sb.Append(Seg.Min(N - 1, N) + "\n"); }
static void Method(string[] args) { int n = ReadInt(); int[] array = new int[n]; int[][] sortedArray = new int[n][]; for (int i = 0; i < n; i++) { array[i] = ReadInt(); sortedArray[i] = new int[2] { array[i], i }; } sortedArray = sortedArray.OrderBy(a => a[0]).ToArray(); int[] indexes = new int[n]; for (int i = 0; i < n; i++) { indexes[sortedArray[i][1]] = i; } int[] prevs = new int[n]; int prev = -1; prevs[0] = -1; for (int i = 1; i < n; i++) { if (sortedArray[i - 1][0] != sortedArray[i][0]) { prev = i - 1; } prevs[i] = prev; } SegTree <int> segTree = new SegTree <int>(n, -1, Max, -1); int res = 0; for (int i = 0; i < n; i++) { int index = indexes[i]; int val = segTree.Scan(0, prevs[index] + 1); if (val == -1) { res++; } else { segTree.Update(val, -1); } segTree.Update(index, index); } WriteLine(res); }
static void Method(string[] args) { int n = ReadInt(); char[] s = Read().ToCharArray(); int q = ReadInt(); SegTree segTree = new SegTree(n, (a, b) => { bool[] res = new bool[26]; for (int i = 0; i < a.Length; i++) { res[i] = a[i] | b[i]; } return(res); }); for (int i = 0; i < n; i++) { bool[] tmp = segTree.Look(i); tmp[s[i] - 'a'] = true; segTree.Update(i); } for (int i = 0; i < q; i++) { string[] query = Read().Split(); int type = int.Parse(query[0]); if (type == 1) { int index = int.Parse(query[1]) - 1; bool[] tmp = segTree.Look(index); tmp[s[index] - 'a'] = false; tmp[query[2][0] - 'a'] = true; s[index] = query[2][0]; segTree.Update(index); } else { int l = int.Parse(query[1]) - 1; int r = int.Parse(query[2]); bool[] tmp = segTree.Scan(l, r); int kinds = 0; for (int j = 0; j < 26; j++) { if (tmp[j]) { kinds++; } } WriteLine(kinds); } } }
static void Main() { var N = int.Parse(Console.ReadLine()); var S = new SegTree <int>(N, Math.Max); for (int i = 0; i < N; ++i) { var c = int.Parse(Console.ReadLine()) - 1; S.Update(c, S.Query(0, c + 1) + 1); } Console.WriteLine(N - S.Query(0, N)); }
static void Method(string[] args) { int n = ReadInt(); long[][] xds = new long[n][]; for (int i = 0; i < n; i++) { xds[i] = ReadLongs(); } xds = xds.OrderBy(a => a[0]).ToArray(); SegTree <int> tree = new SegTree <int>(n, 0, Max, 0); for (int i = n - 1; i >= 0; i--) { tree.Update(i, i); int bottom = i; int top = n; while (bottom + 1 < top) { int mid = (bottom + top) / 2; if (xds[mid][0] < xds[i][0] + xds[i][1]) { bottom = mid; } else { top = mid; } } tree.Update(i, tree.Scan(i, top)); } int[] dp = new int[n]; int mask = 998244353; dp[0] = 1; dp[tree.Look(0)] += 1; for (int i = 1; i < n; i++) { dp[i] += dp[i - 1]; dp[i] %= mask; int to = tree.Look(i); dp[to] += dp[i - 1]; dp[to] %= mask; } WriteLine(dp[n - 1]); }
void Calc() { int N = re.i(); int[] white = new int[N]; int[] black = new int[N]; for (int i = 0; i < 2 * N; i++) { if (re.s() == "W") { white[re.i() - 1] = i; } else { black[re.i() - 1] = i; } } long[,] DP = new long[N + 1, N + 1]; for (int i = 0; i <= N; i++) { SegTree Seg = new SegTree(2 * N, this); for (int j = 0; j < i; j++) { Seg.Add(white[j]); } for (int j = 0; j <= N; j++) { if (i == 0 & j == 0) { DP[i, j] = 0; } else if (i == 0) { DP[i, j] = DP[i, j - 1] + Seg.Sum(black[j - 1] + 1, 2 * N - 1); Seg.Add(black[j - 1]); } else if (j == 0) { DP[i, j] = DP[i - 1, j] + Seg.Sum(white[i - 1] + 1, 2 * N - 1); } else { Seg.Add(black[j - 1]); DP[i, j] = Math.Min(DP[i - 1, j] + Seg.Sum(white[i - 1] + 1, 2 * N - 1), DP[i, j - 1] + Seg.Sum(black[j - 1] + 1, 2 * N - 1)); } } } sb.Append(DP[N, N] + "\n"); }
static void Method(string[] args) { int[] nk = ReadInts(); int n = nk[0]; long k = nk[1]; long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = ReadLong(); array[i] -= k; } long[] sums = new long[n]; var posDict = new Dictionary <long, Stack <int> >(); long res = 0; for (int i = 0; i < n; i++) { sums[i] = array[i]; if (i > 0) { sums[i] += sums[i - 1]; } if (!posDict.ContainsKey(sums[i])) { posDict.Add(sums[i], new Stack <int>()); } posDict[sums[i]].Push(i); if (sums[i] >= 0) { res++; } } Array.Sort(sums); Array.Reverse(sums); SegTree <long> segTree = new SegTree <long>(n, (a, b) => a + b, 0); for (int i = 0; i < n; i++) { int index = posDict[sums[i]].Pop(); res += segTree.Scan(index + 1, n); segTree.Update(index, 1); } WriteLine(res); }
public void Add(SegTree S, long M) { if (l == null && r == null) { S.Add(p, count * M % Define.mod); } if (l != null) { l.Add(S, M); } if (r != null) { r.Add(S, M); } }
static void Method(string[] args) { int n = ReadInt(); int[][] xyds = new int[n][]; for (int i = 0; i < n; i++) { xyds[i] = ReadInts(); } Array.Sort(xyds, (a, b) => a[0] - b[0]); int range = 30000; int mid = 15000; SegTree <int> plus = new SegTree <int>(range, 0, (a, b) => a + b, 0); SegTree <int> minus = new SegTree <int>(range, 0, (a, b) => a + b, 0); }
static void Main() { //var st = new SegTree<int>(14, Math.Max); var st = new SegTree <int>(14, delegate(int x, int y) { return(x + y); }); st.Print(); st.Update(5, 3); st.Print(); st.Update(8, 4); st.Print(); st.Update(10, 2); st.Print(); Console.WriteLine(st.Query(3, 7)); }
static void Main() { var str = Console.ReadLine().Split(' '); int N = int.Parse(str[0]), K = int.Parse(str[1]); int Amax = 300000; var st = new SegTree <int>(Amax + 1, Math.Max); for (long i = 0; i < N; ++i) { var A = int.Parse(Console.ReadLine()); st.Update(A, st.Query(Math.Max(0, A - K), Math.Min(Amax, A + K) + 1) + 1); } Console.WriteLine(st.Query(0, Amax + 1)); }
void Calc() { string[] str = Console.ReadLine().Split(' '); int N = int.Parse(str[0]); int M = int.Parse(str[1]); int[] L = new int[N]; int[] R = new int[N]; int[] C = new int[N]; for (int i = 0; i < N; i++) { str = Console.ReadLine().Split(' '); L[i] = int.Parse(str[0]); R[i] = int.Parse(str[1]); C[i] = i; } Array.Sort(C, (x, y) => (R[y] - R[x])); int count = Math.Max(N - M, 0); SegTree Seg = new SegTree(M + 2, this); for (int i = 2 * M + 2; i >= 0; i--) { if (i >= Seg.segf) { Seg.Z[i] = -Seg.ToPoint(i); } else { int sl = i * 2 + 1; int sr = i * 2 + 2; Seg.Z[i] = Math.Max(Seg.Z[sl], Seg.Z[sr]); } } int p = 0; for (int i = M; i >= 0; i--) { while (p < N && R[C[p]] >= i + 1) { Seg.Add(L[C[p]], M + 1); p++; } int A = Seg.Max(0, i) - (M - i); count = Math.Max(count, A); } sb.Append(count + "\n"); }
static void Method(string[] args) { int n = ReadInt(); long[] array = ReadLongs(); var sumTree = new SegTree <long>(n, 0, (a, b) => a + b, 0); var gcdTree = new SegTree <long>(n, 0, (a, b) => GCD(a, b), 0); for (int i = 0; i < n; i++) { long delta = array[i]; if (i > 0) { delta -= array[i - 1]; } sumTree.Update(i, delta); gcdTree.Update(i, Abs(delta)); } int m = ReadInt(); for (int i = 0; i < m; i++) { int[] tlr = ReadInts(); int t = tlr[0]; int l = tlr[1]; int r = tlr[2]; if (t == 0) { long top = sumTree.Scan(0, l); long gcd = gcdTree.Scan(l, r); WriteLine(GCD(top, gcd)); } else { sumTree.Update(l - 1, sumTree.Look(l - 1) + t); gcdTree.Update(l - 1, Abs(sumTree.Look(l - 1))); if (r < n) { sumTree.Update(r, sumTree.Look(r) - t); gcdTree.Update(r, Abs(sumTree.Look(r))); } } } }
public long AddLeft(SegTree S) { long ans = 0; if (l == null && r == null) { return(count * S.CalcLess(p) % Define.mod); } if (r != null) { ans += r.AddLeft(S); } if (l != null) { ans += l.AddLeft(S); } return(ans % Define.mod); }
static void Method(string[] args) { int[] hwm = ReadInts(); int h = hwm[0]; int w = hwm[1]; int m = hwm[2]; List <int>[] hws = new List <int> [h]; for (int i = 0; i < h; i++) { hws[i] = new List <int>(); } SegTree <int> tree = new SegTree <int>(w, 0, Max, 0); for (int i = 0; i < m; i++) { int[] hw = ReadInts(); hw[0]--; hw[1]--; hws[hw[0]].Add(hw[1]); tree.Update(hw[1], tree.Look(hw[1]) + 1); } int res = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < hws[i].Count; j++) { tree.Update(hws[i][j], tree.Look(hws[i][j]) - 1); } int tmp = hws[i].Count + tree.Scan(0, h + 1); res = Max(res, tmp); for (int j = 0; j < hws[i].Count; j++) { tree.Update(hws[i][j], tree.Look(hws[i][j]) + 1); } } WriteLine(res); }
static void Method(string[] args) { int n = ReadInt(); string s = Read(); int q = ReadInt(); int[] ks = ReadInts(); var dTree = new SegTree <long>(n, (a, b) => a + b, 0); var cTree = new SegTree <long>(n, (a, b) => a + b, 0); var mPosSet = new HashSet <int>(); for (int i = 0; i < n; i++) { switch (s[i]) { case 'D': dTree.Update(i, 1); break; case 'M': mPosSet.Add(i); break; case 'C': cTree.Update(i, 1); break; } } long[] res = new long[q]; for (int i = 0; i < q; i++) { foreach (int pos in mPosSet) { } } for (int i = 0; i < q; i++) { WriteLine(res); } }
bool check(int D) { SegTree Seg = new SegTree(2 * N); for (int i = 0; i < N; i++) { Seg.Add(Seg.ToLeaf(Q[i]), Seg.ToLeaf(Q[i + N]), true, true); } for (int i = Seg.segf - 1; i >= 0; i--) { int sl = i * 2 + 1; int sr = i * 2 + 2; Seg.Add(i, sl, true, false); Seg.Add(i, sr, true, false); } for (int i = 0; i < 2 * N - 1; i++) { int Pi = P[i] >= N ? Y[P[i] - N] : X[P[i]]; int bf = i; int bl = 2 * N - 1; while (bf != bl) { int bc = (bf + bl + 1) / 2; if (Pi + D > (P[bc] >= N ? Y[P[bc] - N] : X[P[bc]])) { bf = bc; } else { bl = bc - 1; } } if (bl != i) { Seg.Add(i + 1, bl, i); } } TwoSatSolver T = new TwoSatSolver(4 * N - 1, Seg.vs.ToArray(), Seg.us.ToArray(), Seg.vb.ToArray(), Seg.ub.ToArray()); return(T.Answer()); }
static void Method(string[] args) { int n = ReadInt(); long[] ps = ReadLongs(); SegTree <long> segTreeMin = new SegTree <long>(n, long.MaxValue, Min, long.MaxValue); SegTree <long> segTreeMax = new SegTree <long>(n, -1, Max, -1); long[] sorted = new long[n]; var poses = new Dictionary <long, int>(); for (int i = 0; i < n; i++) { sorted[i] = ps[i]; poses.Add(ps[i], i); } Array.Sort(sorted); long res = 0; for (int i = n - 1; i >= 0; i--) { long bottom = segTreeMax.Scan(0, poses[sorted[i]]); long top = segTreeMin.Scan(poses[sorted[i]], n); if (bottom != -1) { long bottom2 = segTreeMax.Scan(0, (int)bottom); long topTmp = top == long.MaxValue ? n : top; res += (topTmp - poses[sorted[i]]) * (bottom - bottom2) * sorted[i]; } if (top != long.MaxValue) { long top2 = segTreeMin.Scan((int)top + 1, n); long topTmp = top2 == long.MaxValue ? n : top2; res += (topTmp - top) * (poses[sorted[i]] - bottom) * sorted[i]; } segTreeMax.Update(poses[sorted[i]], poses[sorted[i]]); segTreeMin.Update(poses[sorted[i]], poses[sorted[i]]); } WriteLine(res); }
SegTree Merge(SegTree F, SegTree T) { if (F.size >= T.size) { long FT = T.AddRight(F); long TF = T.AddLeft(F); F.c1 = (F.c1 + T.c1 + FT) % Define.mod; F.c2 = (F.c2 + T.c2 + FT + TF) % Define.mod; T.Add(F); return(F); } else { long FT = F.AddLeft(T); long TF = F.AddRight(T); T.c1 = (F.c1 + T.c1 + FT) % Define.mod; T.c2 = (F.c2 + T.c2 + FT + TF) % Define.mod; F.Add(T); return(T); } }