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]); } }
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)); } } }
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 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]); }
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 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)); }
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)); }
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))); } } } }
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 q = ReadInt(); SegTree <int> segTree = new SegTree <int>(200010, 0, (a, b) => a + b, 0); List <int> reses = new List <int>(); for (int i = 0; i < q; i++) { int[] tx = ReadInts(); int t = tx[0]; int x = tx[1]; if (t == 1) { segTree.Update(x, segTree[x] + 1); } else { int bottom = 0; int top = 200001; while (bottom + 1 < top) { int mid = (bottom + top) / 2; if (segTree.Scan(1, mid + 1) < x) { bottom = mid; } else { top = mid; } } reses.Add(top); segTree.Update(top, segTree[top] - 1); } } for (int i = 0; i < reses.Count; i++) { WriteLine(reses[i]); } }
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[] 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); }
static void Method(string[] args) { int[] nl = ReadInts(); int n = nl[0]; int length = nl[1]; int[][] lrcs = new int[n][]; for (int i = 0; i < n; i++) { lrcs[i] = ReadInts(); } Array.Sort(lrcs, (a, b) => a[0] - b[0]); SegTree <long> tree = new SegTree <long>(length, long.MaxValue / 2, Min, long.MaxValue / 2); for (int i = 0; i < n; i++) { if (lrcs[i][0] == 0) { if (lrcs[i][2] < tree.Look(lrcs[i][1] - 1)) { tree.Update(lrcs[i][1] - 1, lrcs[i][2]); } } else { long val = lrcs[i][2] + tree.Scan(lrcs[i][0] - 1, lrcs[i][1] - 1); if (val < tree.Look(lrcs[i][1] - 1)) { tree.Update(lrcs[i][1] - 1, val); } } } WriteLine(tree.Look(length - 1)); }
static void Main() { var N = long.Parse(Console.ReadLine()); var A = Console.ReadLine().Split(' ').Select(long.Parse).ToList(); var E = new List <long> [N]; for (int i = 0; i < N; ++i) { E[i] = new List <long>(); } for (int i = 0; i < N - 1; ++i) { var uv = Console.ReadLine().Split(' ').Select(int.Parse).ToList(); int u = uv[0], v = uv[1]; E[u - 1].Add(v - 1); E[v - 1].Add(u - 1); } //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)); }
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); } }
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); }
static void Method(string[] args) { int n = ReadInt(); int[] array = ReadInts(); SegTree <long> lowCntTree = new SegTree <long>(n, 1, (i, j) => i + j, 0); int[] poses = new int[n]; for (int i = 0; i < n; i++) { poses[array[i] - 1] = i; } long res = 0; for (int i = 0; i < n; i++) { long cnt = Min(lowCntTree.Run(0, poses[i]), lowCntTree.Run(poses[i] + 1, n)); res += cnt; lowCntTree.Update(poses[i], 0); } WriteLine(res); }
static void Method(string[] args) { long n = ReadLong(); int[] array = ReadInts(); int[] sorted = new int[n]; for (int i = 0; i < n; i++) { sorted[i] = array[i]; } Array.Sort(sorted); Dictionary <int, int> pos = new Dictionary <int, int>(); for (int i = 0; i < n; i++) { if (pos.ContainsKey(sorted[i])) { continue; } pos.Add(sorted[i], i); } int bottom = 0; int top = (int)n; while (bottom + 1 < top) { int mid = (bottom + top) / 2; int val = sorted[mid]; int[] sums = new int[n]; for (int i = 0; i < n; i++) { if (val <= array[i]) { sums[i] = 1; } else { sums[i] = -1; } if (i > 0) { sums[i] += sums[i - 1]; } } long cnt = 0; SegTree <int> tree = new SegTree <int>(2 * 100000 + 5, 0, (a, b) => a + b, 0); for (int i = (int)n - 1; i >= 0; i--) { tree.Update(sums[i] + 100000, tree.Look(sums[i] + 100000) + 1); if (val <= array[i]) { cnt += tree.Scan(sums[i] - 1 + 100000, 2 * 100000 + 10); } else { cnt += tree.Scan(sums[i] + 1 + 100000, 2 * 100000 + 10); } } long thres = n * (n + 1) / 4; if ((n * (n + 1) / 2) % 2 > 0) { thres++; } if (cnt >= thres) { bottom = mid; } else { top = mid; } } Console.WriteLine(sorted[bottom]); }
static void Method(string[] args) { int n = ReadInt(); int[] array = ReadInts(); var segTreeEven = new SegTree <int>(n, int.MaxValue / 10, Min, int.MaxValue / 10); var segTreeOdd = new SegTree <int>(n, int.MaxValue / 10, Min, int.MaxValue / 10); var poses = new int[n]; for (int i = 0; i < n; i++) { if (i % 2 == 0) { segTreeEven.Update(i, array[i]); } else { segTreeOdd.Update(i, array[i]); } poses[array[i] - 1] = i; } var que = new PriorityQueue <int[]>(); que.Enqueue(segTreeEven.Scan(0, n), new int[2] { 0, n }); while (que.Count > 0) { var pair = que.Dequeue(); int top = (int)pair.Key; int topPos = poses[top - 1]; int last; if (pair.Value[0] % 2 == 0) { last = segTreeOdd.Scan(topPos + 1, pair.Value[1]); } else { last = segTreeEven.Scan(topPos + 1, pair.Value[1]); } int lastPos = poses[last - 1]; Write(top + " " + last + " "); if (pair.Value[1] - pair.Value[0] <= 2) { continue; } if (pair.Value[0] % 2 == 0) { if (pair.Value[0] < topPos) { que.Enqueue(segTreeEven.Scan(pair.Value[0], topPos), new int[2] { pair.Value[0], topPos }); } if (topPos + 1 < lastPos) { que.Enqueue(segTreeOdd.Scan(topPos + 1, lastPos), new int[2] { topPos + 1, lastPos }); } if (lastPos + 1 < pair.Value[1]) { que.Enqueue(segTreeEven.Scan(lastPos + 1, pair.Value[1]), new int[2] { lastPos + 1, pair.Value[1] }); } } else { if (pair.Value[0] < topPos) { que.Enqueue(segTreeOdd.Scan(pair.Value[0], topPos), new int[2] { pair.Value[0], topPos }); } if (topPos + 1 < lastPos) { que.Enqueue(segTreeEven.Scan(topPos + 1, lastPos), new int[2] { topPos + 1, lastPos }); } if (lastPos + 1 < pair.Value[1]) { que.Enqueue(segTreeOdd.Scan(lastPos + 1, pair.Value[1]), new int[2] { lastPos + 1, pair.Value[1] }); } } } WriteLine(); }
static void Method(string[] args) { int n = ReadInt(); int[][] xys = new int[n][]; for (int i = 0; i < n; i++) { xys[i] = ReadInts(); } Array.Sort(xys, (a, b) => a[1] - b[1]); for (int i = 0; i < n; i++) { xys[i][1] = i; } Array.Sort(xys, (a, b) => a[0] - b[0]); long[] belows = new long[n]; SegTree <int> segTree = new SegTree <int>(n, 0, (a, b) => a + b, 0); for (int i = 0; i < n; i++) { belows[i] = segTree.Scan(0, xys[i][1]); segTree.Update(xys[i][1], 1); } long mask = 998244353; long[] pows = new long[n + 1]; pows[0] = 1; for (int i = 1; i <= n; i++) { pows[i] = pows[i - 1] * 2; pows[i] %= mask; } long res = (pows[n - 1] * n) % mask; for (int i = 0; i < n; i++) { long tmp = pows[n - 1] - 1; if (i >= 0) { tmp -= pows[i] - 1; } if (xys[i][1] >= 0) { tmp -= pows[xys[i][1]] - 1; } if (n - i - 1 >= 0) { tmp -= pows[n - i - 1] - 1; } if (n - xys[i][1] - 1 >= 0) { tmp -= pows[n - xys[i][1] - 1] - 1; } while (tmp < 0) { tmp += mask; } tmp += pows[belows[i]] - 1; tmp += pows[i - belows[i]] - 1; long afterBelows = segTree.Scan(0, xys[i][1]) - belows[i]; tmp += pows[afterBelows] - 1; tmp += pows[n - i - 1 - afterBelows] - 1; tmp %= mask; res += tmp; res %= mask; } WriteLine(res); }
static void Main() { var HWN = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int H = HWN[0], W = HWN[1], N = HWN[2]; var Ymin = new int[W]; for (int w = 0; w < W; ++w) { Ymin[w] = H; } var Xmin = new int[H]; for (int h = 0; h < H; ++h) { Xmin[h] = W; } var XYList = new List <XYPoint>(); for (int i = 0; i < N; ++i) { var YX = Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); int Y = YX[0] - 1, X = YX[1] - 1; XYList.Add(new XYPoint(X, Y)); Ymin[X] = Math.Min(Ymin[X], Y); Xmin[Y] = Math.Min(Xmin[Y], X); } XYList.Sort(delegate(XYPoint p1, XYPoint p2) { return(p1.Y - p2.Y); }); int xEnd = Xmin[0]; int yEnd = Ymin[0]; long ans = 0; for (int x = 0; x < xEnd; ++x) { ans += Ymin[x]; } var st = new SegTree <int>(W + 2, delegate(int x, int y) { return(x + y); }); for (int x = xEnd; x < W; ++x) { st.Update(x, 1); } int n = 0; for (int y = 0; y < yEnd; ++y) { while (n < N && XYList[n].Y <= y) { var p = XYList[n]; st.Update(p.X, 1); n += 1; } int add = st.Query(0, Xmin[y]); ans += add; } Console.WriteLine(ans); }
static void Method(string[] args) { string s = Read(); int cCnt = 26; int[] cnts = new int[26]; for (int i = 0; i < s.Length; i++) { cnts[s[i] - 'a']++; } int oddCnt = 0; int oddIndex = 0; for (int i = 0; i < cCnt; i++) { if (cnts[i] % 2 > 0) { oddCnt++; oddIndex = i; } } if ((s.Length % 2 == 0 && oddCnt > 0) || (s.Length % 2 > 0 && oddCnt != 1)) { WriteLine(-1); return; } var remain = new Dictionary <int, List <int> >(); var posNos = new List <int>(); int[] useCnt = new int[cCnt]; int unusedCnt = 0; long res = 0; for (int i = 0; i < s.Length; i++) { int index = s[i] - 'a'; if (useCnt[index] < cnts[index] / 2) { res += unusedCnt; useCnt[index]++; posNos.Add(index); } else { if (!remain.ContainsKey(index)) { remain.Add(index, new List <int>()); } remain[index].Add(unusedCnt); unusedCnt++; } } if (s.Length % 2 == 1) { posNos.Add(oddIndex); } SegTree <int> segTree = new SegTree <int>(unusedCnt, (a, b) => a + b, 0); for (int i = posNos.Count - 1; i >= 0; i--) { int pos = remain[posNos[i]][0]; res += pos - segTree.Scan(0, pos); segTree.Update(pos, 1); remain[posNos[i]].RemoveAt(0); } WriteLine(res); }
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)); var NQ = Console.ReadLine().Split().Select(int.Parse).ToList(); int N = NQ[0], Q = NQ[1]; int M = 200000; var st = new SegTree <int>(M, Math.Min); var Y = new Yochien[M]; for (int i = 0; i < M; ++i) { Y[i] = new Yochien(); } var LY = new int[N]; // i-th infant belongs to LY[i]-th Yochien. var LI = new int[N]; // i-th infant belongs to LI-th index (of LY[i]-th Yochien). for (int i = 0; i < N; ++i) { var AB = Console.ReadLine().Split().Select(int.Parse).ToList(); int A = AB[0], B = AB[1]; LY[i] = B - 1; LI[i] = Y[LY[i]].Add(A); //Console.WriteLine($"{LY[i]}, {LI[i]}"); } for (int i = 0; i < M; ++i) { st.Update(i, Y[i].Max()); } for (int iq = 0; iq < Q; ++iq) { var CD = Console.ReadLine().Split().Select(int.Parse).ToList(); int C = CD[0], D = CD[1]; var ly_old = LY[C - 1]; var y_old = Y[ly_old]; int A = y_old.Update(LI[C - 1], 0); var ly_new = LY[C - 1] = D - 1; var y_new = Y[ly_new]; LI[C - 1] = y_new.Add(A); st.Update(ly_old, y_old.Max()); st.Update(ly_new, y_new.Max()); Console.WriteLine(st.Query(0, st.Count)); } }
static void Method(string[] args) { int[] nm = ReadInts(); int n = nm[0]; int m = nm[1]; int max = 1000000; long res = 1; int[][] ylrs = new int[n][]; for (int i = 0; i < n; i++) { ylrs[i] = ReadInts(); if (ylrs[i][1] == 0 && ylrs[i][2] == max) { res++; } } Array.Sort(ylrs, (a, b) => a[0] - b[0]); int[][] xyAdds = new int[m][]; int[][] xyRems = new int[m][]; for (int i = 0; i < m; i++) { int[] xlr = ReadInts(); xyAdds[i] = new int[2] { xlr[0], xlr[1] }; xyRems[i] = new int[2] { xlr[0], xlr[2] }; if (xlr[1] == 0 && xlr[2] == max) { res++; } } Array.Sort(xyAdds, (a, b) => a[1] - b[1]); Array.Sort(xyRems, (a, b) => a[1] - b[1]); SegTree <long> segTree = new SegTree <long>(max + 10, 0, (a, b) => a + b, 0); int idxAdd = 0; int idxRem = 0; for (int i = 0; i < n; i++) { while (idxAdd < m && xyAdds[idxAdd][1] <= ylrs[i][0]) { segTree.Update(xyAdds[idxAdd][0], segTree.Look(xyAdds[idxAdd][0]) + 1); idxAdd++; } while (idxRem < m && xyRems[idxRem][1] < ylrs[i][0]) { segTree.Update(xyRems[idxRem][0], segTree.Look(xyRems[idxRem][0]) - 1); idxRem++; } res += segTree.Scan(ylrs[i][1], ylrs[i][2] + 1); } WriteLine(res); }
static void Main() { var str = Console.ReadLine().Split(" "); var N = int.Parse(str[0]); var Q = int.Parse(str[1]); var C = new int[N]; str = Console.ReadLine().Split(" "); for (int i = 0; i < N; ++i) { C[i] = int.Parse(str[i]); } var LR = new List <List <int> >(); for (int q = 0; q < Q; ++q) { str = Console.ReadLine().Split(" "); int l = int.Parse(str[0]); int r = int.Parse(str[1]); LR.Add(new List <int> { q, l, r }); } LR.Sort((x, y) => x[2] - y[2]); var I = new int[N + 1]; for (int i = 0; i < I.Length; ++i) { I[i] = -1; } var st = new SegTree <int>(N, (x, y) => x + y, 0); var ans = new int[Q]; int j = 0; for (int iq = 0; iq < Q; ++iq) { int q = LR[iq][0]; int l = LR[iq][1]; int r = LR[iq][2]; for (; j < r; ++j) { int c = C[j]; int iprev = I[c]; I[c] = j; if (iprev != -1) { st.Update(iprev, 0); } st.Update(j, 1); } ans[q] = st.Query(l - 1, r); } Console.WriteLine(String.Join("\n", ans)); }