static void Main() { Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); var n = int.Parse(Console.ReadLine()); var map = Array.ConvertAll(new int[n], _ => Read().Skip(1).ToList()); var qc = int.Parse(Console.ReadLine()); var qs = Array.ConvertAll(new int[qc], _ => Read()); var et = new EulerTour(n, 0, map); var st = new ST1 <long>(2 * n, (x, y) => x + y, 0); foreach (var q in qs) { if (q[0] == 0) { st.Set(et.ordersMap[q[1]][0] - 1, st.Get(et.ordersMap[q[1]][0] - 1) + q[2]); st.Set(et.ordersMap[q[1]].Last(), st.Get(et.ordersMap[q[1]].Last()) - q[2]); } else { Console.WriteLine(st.Get(0, et.ordersMap[q[1]][0])); } } Console.Out.Flush(); }
static object Solve() { var(w, n) = Read2(); var ps = Array.ConvertAll(new bool[n], _ => Read3()); var st = new ST1 <long>(w + 1, Math.Max, min); st.Set(0, 0); foreach (var(l, r, v) in ps) { for (int x = w; x > 0; x--) { var m = st.Get(Math.Max(0, x - r), Math.Max(0, x - l + 1)); if (m == min) { continue; } st.Set(x, Math.Max(st.Get(x), m + v)); } } var mv = st.Get(w); return(mv == min ? -1 : mv); }
static void Main() { Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); var h = Read(); var n = h[0]; var st = new ST1 <int>(n, Math.Min, int.MaxValue); for (int k = 0; k < h[1]; k++) { var q = Read(); if (q[0] == 0) { st.Set(q[1], q[2]); } else { Console.WriteLine(st.Get(q[1], q[2] + 1)); } } Console.Out.Flush(); }
static void Main() { Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); var n = int.Parse(Console.ReadLine()); var s = Console.ReadLine().Select(c => 1 << (c - 'a')).ToArray(); var qc = int.Parse(Console.ReadLine()); var st = new ST1 <int>(n, (x, y) => x | y, 0, s); for (int k = 0; k < qc; k++) { var q = Console.ReadLine().Split(); if (q[0] == "1") { st.Set(int.Parse(q[1]) - 1, 1 << (q[2][0] - 'a')); } else { var f = st.Get(int.Parse(q[1]) - 1, int.Parse(q[2])); Console.WriteLine(Enumerable.Range(0, 26).Count(i => (f & (1 << i)) != 0)); } } Console.Out.Flush(); }
static void Main() { Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); var h = Read(); var n = h[0]; var st = new ST1 <long>(n + 1, (x, y) => x + y, 0); for (int k = 0; k < h[1]; k++) { var q = Read(); if (q[0] == 0) { st.Set(q[1], st.Get(q[1]) + q[2]); } else { Console.WriteLine(st.Get(q[1], q[2] + 1)); } } Console.Out.Flush(); }
static void Main() { var n = int.Parse(Console.ReadLine()); var rs = new int[n].Select(_ => Array.ConvertAll(Console.ReadLine().Split(), int.Parse)) .Select(r => new[] { Math.Min(r[0], r[2]), Math.Min(r[1], r[3]), Math.Max(r[0], r[2]), Math.Max(r[1], r[3]) }) .ToArray(); var ys = rs.Select(r => r[1]).Concat(rs.Select(r => r[3])).Distinct().OrderBy(v => v).ToArray(); var yd = Enumerable.Range(0, ys.Length).ToDictionary(i => ys[i]); var xqs1 = rs.Where(r => r[1] == r[3]).Select(r => new[] { -1, r[0], yd[r[1]] }); var yqs = rs.Where(r => r[0] == r[2]).Select(r => new[] { 0, r[0], yd[r[1]], yd[r[3]] }); var xqs2 = rs.Where(r => r[1] == r[3]).Select(r => new[] { 1, r[2], yd[r[3]] }); var qs = xqs1.Concat(yqs).Concat(xqs2).OrderBy(q => q[1]); var sum = 0L; var st = new ST1 <int>(ys.Length, (x, y) => x + y, 0); foreach (var q in qs) { if (q[0] == 0) { sum += st.Get(q[2], q[3] + 1); } else { st.Set(q[2], st.Get(q[2]) - q[0]); } } Console.WriteLine(sum); }
static void Main() { Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); var h = Read(); var n = h[0]; var a = Array.ConvertAll(Console.ReadLine().Split(), long.Parse); var st = new ST1 <long>(n, (x, y) => x + y, 0, a); for (int k = 0; k < h[1]; k++) { var q = Read(); if (q[0] == 0) { st.Set(q[1], st.Get(q[1]) + q[2]); } else { Console.WriteLine(st.Get(q[1], q[2])); } } Console.Out.Flush(); }
static void Main() { var(n, qc) = Read2(); var a = Read(); var qs = Array.ConvertAll(new bool[qc], _ => Read3()); // a の値となりうるインデックスのセット var map = new Dictionary <int, SortedSet <int> >(); void AddPair(int i, int v) { if (!map.ContainsKey(v)) { map[v] = new SortedSet <int>(); } map[v].Add(i); } for (int i = 0; i < n; i++) { AddPair(i, a[i]); } foreach (var(t, x, y) in qs) { if (t == 1) { AddPair(x - 1, y); } } var st = new ST1 <int>(n, Math.Min, int.MaxValue, a); Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); foreach (var q in qs) { var(t, x, y) = q; x--; if (t == 1) { a[x] = y; st.Set(x, y); } else { var p = st.Get(x, y); var r = map[p].GetViewBetween(x, y - 1).Where(i => a[i] == p).Select(i => i + 1).ToArray(); Console.WriteLine($"{r.Length} " + string.Join(" ", r)); } } Console.Out.Flush(); }
static void Main() { var n = int.Parse(Console.ReadLine()); var xs = Read(); var qc = int.Parse(Console.ReadLine()); var qs = Enumerable.Range(0, qc).Select(Read3).ToArray(); var mins = Array.ConvertAll(new bool[qc], _ => int.MaxValue); var st1 = new ST1 <int>(n, Math.Max, int.MinValue); var st2 = new ST1 <int>(n, Math.Min, int.MaxValue); var xqs = xs.Select((x, i) => (x, l: int.MinValue, r: 0, i)).Concat(qs).ToArray(); // x - x_i >= 0 var q1 = xqs.OrderBy(v => v); // x_i - x >= 0 var q2 = xqs.OrderBy(v => - v.x).ThenBy(v => v.l); foreach (var(x, l, r, i) in q1) { if (r == 0) { st1.Set(i, x); } else { var max = st1.Get(l - 1, r); if (max == st1.v0) { continue; } mins[i] = Math.Min(mins[i], x - max); } } foreach (var(x, l, r, i) in q2) { if (r == 0) { st2.Set(i, x); } else { var min = st2.Get(l - 1, r); if (min == st2.v0) { continue; } mins[i] = Math.Min(mins[i], min - x); } } Console.WriteLine(string.Join("\n", mins)); }
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 ST1 <long>(n, (x, y) => x + y, 0); var r = 0L; for (int i = 0; i < n; i++) { r += st.Get(a[i].i, n); st.Set(a[i].i, 1); } Console.WriteLine(r); }
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 ST1 <int>(n, Math.Max, 0, a); for (int k = 0; k < h[1]; k++) { var q = Read(); if (q[0] == 1) { st.Set(q[1] - 1, q[2]); } else if (q[0] == 2) { Console.WriteLine(st.Get(q[1] - 1, q[2])); } else { Console.WriteLine(1 + st.Aggregate(q[1] - 1, n, n, (p, node, l) => { if (p < n || st[node] < q[2]) { return(p); } while (node.i < st.n2 >> 1) { node = q[2] <= st[node.Child0] ? node.Child0 : node.Child1; } return(st.Original(node)); })); } } Console.Out.Flush(); }
static void Main() { Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); var(n, m, qc) = Read3(); var qs = Array.ConvertAll(new bool[qc], _ => Read3()); var map = new CompressionHashMap(qs.Select(q => q.y).Append(0).ToArray()); var a = new int[n]; var b = new int[m]; var a_count = new ST1 <long>(map.Count, (x, y) => x + y, 0); var b_count = new ST1 <long>(map.Count, (x, y) => x + y, 0); var a_sum = new ST1 <long>(map.Count, (x, y) => x + y, 0); var b_sum = new ST1 <long>(map.Count, (x, y) => x + y, 0); a_count.Set(map[0], n); b_count.Set(map[0], m); var r = 0L; foreach (var(t, x, y) in qs) { var j = x - 1; if (t == 1) { var y0 = a[j]; var i = map[y]; var i0 = map[y0]; var sum0 = y0 * b_count.Get(map[0], i0) + b_sum.Get(i0, map.Count); var sum = y * b_count.Get(map[0], i) + b_sum.Get(i, map.Count); r += sum - sum0; a[j] = y; a_count.Set(i0, a_count.Get(i0) - 1); a_count.Set(i, a_count.Get(i) + 1); a_sum.Set(i0, a_sum.Get(i0) - y0); a_sum.Set(i, a_sum.Get(i) + y); } else { var y0 = b[j]; var i = map[y]; var i0 = map[y0]; var sum0 = y0 * a_count.Get(map[0], i0) + a_sum.Get(i0, map.Count); var sum = y * a_count.Get(map[0], i) + a_sum.Get(i, map.Count); r += sum - sum0; b[j] = y; b_count.Set(i0, b_count.Get(i0) - 1); b_count.Set(i, b_count.Get(i) + 1); b_sum.Set(i0, b_sum.Get(i0) - y0); b_sum.Set(i, b_sum.Get(i) + y); } Console.WriteLine(r); } Console.Out.Flush(); }
static void Main() { var(n, qc) = Read2(); var a = Read(); var qs = Array.ConvertAll(new bool[qc], _ => Read3()); var vs = a.ToList(); foreach (var(t, x, y) in qs) { if (t == 1) { vs.Add(y); } } var vMap = new CompressionHashMap(vs.ToArray()); // キーの値となりうるインデックスのリスト var map0 = Array.ConvertAll(new bool[vMap.Count], _ => new List <int>()); for (int i = 0; i < n; i++) { map0[vMap[a[i]]].Add(i); } foreach (var(t, x, y) in qs) { if (t == 1) { map0[vMap[y]].Add(x - 1); } } var map = Array.ConvertAll(map0, l => { var b = l.ToArray(); Array.Sort(b); return(b); }); var u = Array.ConvertAll(map, p => new bool[p.Length]); var mapInv = Array.ConvertAll(map, p => ToInverseMap(p)); for (int i = 0; i < n; i++) { u[vMap[a[i]]][mapInv[vMap[a[i]]][i]] = true; } var st = new ST1 <int>(n, Math.Min, int.MaxValue, a); var r = new List <int>(); Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false }); foreach (var q in qs) { var(t, x, y) = q; x--; if (t == 1) { u[vMap[a[x]]][mapInv[vMap[a[x]]][x]] = false; a[x] = y; st.Set(x, y); u[vMap[a[x]]][mapInv[vMap[a[x]]][x]] = true; } else { r.Clear(); var p = st.Get(x, y); var inds = map[vMap[p]]; var j0 = First(0, inds.Length, j => inds[j] >= x); for (int j = j0; j < inds.Length && inds[j] < y; j++) { if (u[vMap[p]][j]) { r.Add(inds[j] + 1); } } Console.WriteLine($"{r.Count} " + string.Join(" ", r)); } } Console.Out.Flush(); }