Пример #1
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            int[] array  = ReadInts();
            int[] kArray = ReadInts();

            long[] deltas    = new long[n];
            long[] deltaSums = new long[n];
            for (int i = 1; i < n; i++)
            {
                deltas[i]    = kArray[i - 1];
                deltaSums[i] = deltaSums[i - 1] + deltas[i];
            }

            // val, left
            var tree = new LazySegmentTree2 <long, long[]>(n, (a, b) => b,
                                                           (a, b, l, r) =>
            {
                long baseVal = b[0] + deltaSums[l] - deltaSums[b[1]];
                long res     = baseVal * (r - l + 1) + deltaSums[r];
                if (l > b[1])
                {
                    res -= deltaSums[l - 1];
                }
                else
                {
                    res -= deltaSums[l];
                }
                return(res);
            }, (a, b) => a + b, new long[2] {
                0, 0
            }, 0);

            for (int i = 0; i < n; i++)
            {
                tree.Update(i, i, new long[2] {
                    array[i], i
                });
            }

            int q = ReadInt();

            for (int i = 0; i < q; i++)
            {
                string[] line = Read().Split();
                if (line[0][0] == 's')
                {
                    int l = int.Parse(line[1]) - 1;
                    int r = int.Parse(line[2]) - 1;
                    WriteLine(tree.Scan(l, r));
                }
                else
                {
                    int  ii     = int.Parse(line[1]) - 1;
                    int  x      = int.Parse(line[2]);
                    long now    = tree.Scan(ii, ii) + x;
                    int  bottom = ii;
                    int  top    = n;
                    while (bottom + 1 < top)
                    {
                        int  mid    = (bottom + top) / 2;
                        long val    = tree.Scan(mid, mid);
                        long target = now + deltaSums[mid] - deltaSums[ii];
                        if (val <= target)
                        {
                            bottom = mid;
                        }
                        else
                        {
                            top = mid;
                        }
                    }

                    tree.Update(ii, bottom, new long[2] {
                        now, ii
                    });
                }
            }
        }
Пример #2
0
        static void Method(string[] args)
        {
            int[] hw = ReadInts();
            int   h  = hw[0];
            int   w  = hw[1];

            int[][] abs = new int[h][];
            for (int i = 0; i < h; i++)
            {
                abs[i] = ReadInts();
                abs[i][0]--;
                abs[i][1]--;
            }

            // 始点を管理していく
            bool[] isHoles = new bool[w + 1];
            var    tree    = new LazySegmentTree2 <int, int>(
                w + 1, (a, b) => b, (a, b) => b, (a, b) => Max(a, b), 0, 0);

            for (int i = 0; i <= w; i++)
            {
                tree.Update(i, i, i);
            }
            var que = new PriorityQueue <int>();

            for (int i = 0; i < w; i++)
            {
                que.Enqueue(0, i);
            }

            for (int i = 0; i < h; i++)
            {
                int max = tree.Scan(abs[i][0], abs[i][1] + 1);
                tree.Update(abs[i][1] + 1, abs[i][1] + 1, max);
                tree.Update(abs[i][0], abs[i][1], -1);
                if (max < w && max >= 0)
                {
                    que.Enqueue(abs[i][1] + 1 - max, max);
                }

                while (que.Count > 0)
                {
                    var pair = que.Top();
                    int now  = (int)pair.Key + pair.Value;
                    if (tree.Scan(now, now) == pair.Value)
                    {
                        break;
                    }
                    que.Dequeue();
                }

                if (que.Count == 0)
                {
                    WriteLine(-1);
                }
                else
                {
                    WriteLine(que.Top().Key + i + 1);
                }
            }
        }