結果

問題 No.1332 Range Nearest Query
ユーザー kakel-sankakel-san
提出日時 2024-08-29 23:40:51
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 1,497 ms / 2,500 ms
コード長 6,584 bytes
コンパイル時間 14,769 ms
コンパイル使用メモリ 169,408 KB
実行使用メモリ 256,656 KB
最終ジャッジ日時 2024-08-29 23:42:55
合計ジャッジ時間 71,121 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
31,872 KB
testcase_01 AC 68 ms
31,488 KB
testcase_02 AC 69 ms
31,616 KB
testcase_03 AC 1,169 ms
122,668 KB
testcase_04 AC 1,186 ms
122,916 KB
testcase_05 AC 1,177 ms
119,552 KB
testcase_06 AC 1,497 ms
141,468 KB
testcase_07 AC 1,359 ms
139,540 KB
testcase_08 AC 1,371 ms
140,948 KB
testcase_09 AC 1,356 ms
137,080 KB
testcase_10 AC 1,378 ms
136,576 KB
testcase_11 AC 1,357 ms
142,072 KB
testcase_12 AC 1,338 ms
142,192 KB
testcase_13 AC 1,359 ms
142,464 KB
testcase_14 AC 1,357 ms
142,324 KB
testcase_15 AC 1,364 ms
142,296 KB
testcase_16 AC 1,262 ms
140,672 KB
testcase_17 AC 1,258 ms
141,180 KB
testcase_18 AC 1,245 ms
141,312 KB
testcase_19 AC 1,250 ms
140,888 KB
testcase_20 AC 1,273 ms
140,924 KB
testcase_21 AC 1,266 ms
143,176 KB
testcase_22 AC 1,271 ms
141,436 KB
testcase_23 AC 1,234 ms
141,428 KB
testcase_24 AC 1,256 ms
141,136 KB
testcase_25 AC 1,251 ms
141,304 KB
testcase_26 AC 984 ms
141,232 KB
testcase_27 AC 996 ms
141,344 KB
testcase_28 AC 322 ms
71,036 KB
testcase_29 AC 332 ms
70,492 KB
testcase_30 AC 350 ms
70,276 KB
testcase_31 AC 312 ms
70,504 KB
testcase_32 AC 333 ms
70,452 KB
testcase_33 AC 339 ms
70,700 KB
testcase_34 AC 343 ms
70,752 KB
testcase_35 AC 324 ms
70,788 KB
testcase_36 AC 322 ms
70,624 KB
testcase_37 AC 328 ms
70,396 KB
testcase_38 AC 897 ms
91,832 KB
testcase_39 AC 572 ms
73,072 KB
testcase_40 AC 1,203 ms
140,612 KB
testcase_41 AC 681 ms
76,948 KB
testcase_42 AC 884 ms
92,100 KB
testcase_43 AC 785 ms
80,016 KB
testcase_44 AC 1,094 ms
120,576 KB
testcase_45 AC 992 ms
95,360 KB
testcase_46 AC 821 ms
86,356 KB
testcase_47 AC 955 ms
256,656 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (217 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = NN;
        var x = NList;
        var q = NN;
        var map = NArr(q);
        var a1 = Near(n, x, map);
        for (var i = 0; i < n; ++i) x[i] = 1_000_000_000 - x[i];
        for (var i = 0; i < q; ++i) map[i][2] = 1_000_000_000 - map[i][2];
        var a2 = Near(n, x, map);
        var ans = new int[q];
        for (var i = 0; i < q; ++i) ans[i] = Math.Min(a1[i], a2[i]);
        WriteLine(string.Join("\n", ans));
    }
    static int[] Near(int n, int[] x, int[][] query)
    {
        var list = x.Select((xi, id) => (xi, id)).ToList();
        list.Sort((l, r) =>
        {
            var d = l.xi.CompareTo(r.xi);
            if (d != 0) return d;
            return l.id.CompareTo(r.id);
        });
        var plist = new int[n];
        for (var i = 0; i < n; ++i) plist[list[i].id] = i;
        var seg = new SegmentTree<int>(n, new SegOp());
        var qlist = query.Select((qi, id) => (id, qi[0], qi[1], qi[2])).ToList();
        qlist.Sort((l, r) => r.Item2.CompareTo(l.Item2));
        var ans = Enumerable.Repeat(int.MaxValue, query.Length).ToArray();
        var count = n - 1;
        foreach (var que in qlist)
        {
            var li = que.Item2 - 1;
            var ri = que.Item3 - 1;
            var xi = que.Item4;
            while (count >= li)
            {
                seg[plist[count]] = count;
                --count;
            }
            var pos = LowerBound(0, xi, list);
            if (pos < n)
            {
                var p2 = seg.MaxRight(pos, v => v > ri);
                if (p2 < n)
                {
                    ans[que.id] = list[p2].xi - xi;
                }
            }
        }
        return ans;
    }
    static int LowerBound(int left, int min, IList<(int xi, int id)> list)
    {
        if (list[left].xi >= min) return left;
        var ng = left;
        var ok = list.Count;
        while (ok - ng > 1)
        {
            var center = (ng + ok) / 2;
            if (list[center].xi < min) ng = center;
            else ok = center;
        }
        return ok;
    }
    struct SegOp : ISegmentTreeOperator<int>
    {
        public int Identity => int.MaxValue;
        public int Operate(int x, int y)
        {
            return Math.Min(x, y);
        }
    }
    interface ISegmentTreeOperator<T>
    {
        T Identity { get; }
        T Operate(T x, T y);
    }
    class SegmentTree<T>
    {
        int _n;
        int size;
        int log;
        T[] d;
        ISegmentTreeOperator<T> op;
        void Update(int k)
        {
            d[k] = op.Operate(d[2 * k], d[2 * k + 1]);
        }
        public SegmentTree(int n, ISegmentTreeOperator<T> op)
        {
            this.op = op;
            _n = n;
            size = 1;
            while (size < n) size <<= 1;
            log = CountRZero(size);
            d = new T[2 * size];
            for (var i = 0; i < d.Length; ++i) d[i] = op.Identity;
        }
        public SegmentTree(T[] v, ISegmentTreeOperator<T> op)
        {
            this.op = op;
            _n = v.Length;
            size = 1;
            while (size < v.Length) size <<= 1;
            log = CountRZero(size);
            d = new T[2 * size];
            for (var i = 0; i < size; ++i) d[i] = op.Identity;
            for (var i = 0; i < v.Length; ++i) d[size + i] = v[i];
            for (var i = size - 1; i >= 1; --i) Update(i);
        }
        int CountRZero(int n)
        {
            var ans = 0;
            while (n % 2 == 0)
            {
                ++ans;
                n >>= 1;
            }
            return ans;
        }
        public T this[int p]
        {
            get { return d[p + size]; }
            set
            {
                p += size;
                d[p] = value;
                for (var i = 1; i <= log; ++i) Update(p >> i);
            }
        }
        public T Prod(int l, int r)
        {
            var sml = op.Identity;
            var smr = op.Identity;
            l += size;
            r += size;
            while (l < r)
            {
                if ((l & 1) != 0) sml = op.Operate(sml, d[l++]);
                if ((r & 1) != 0) smr = op.Operate(d[--r], smr);
                l >>= 1;
                r >>= 1;
            }
            return op.Operate(sml, smr);
        }
        T AllProd() => d[1];
        public int MinLeft(int r, Predicate<T> f)
        {
            if (r == 0) return 0;
            r += size;
            T sm = op.Identity;
            do
            {
                r--;
                while (r > 1 && (r % 2) != 0) r >>= 1;
                if (!f(op.Operate(d[r], sm)))
                {
                    while (r < size)
                    {
                        r = 2 * r + 1;
                        if (f(op.Operate(d[r], sm)))
                        {
                            sm = op.Operate(d[r], sm);
                            r--;
                        }
                    }
                    return r + 1 - size;
                }
                sm = op.Operate(d[r], sm);
            }
            while ((r & -r) != r);
            return 0;
        }
        public int MaxRight(int l, Predicate<T> f)
        {
            if (l == _n) return _n;
            l += size;
            T sm = op.Identity;
            do
            {
                while (l % 2 == 0) l >>= 1;
                if (!f(op.Operate(sm, d[l])))
                {
                    while (l < size)
                    {
                        l = 2 * l;
                        if (f(op.Operate(sm, d[l])))
                        {
                            sm = op.Operate(sm, d[l]);
                            ++l;
                        }
                    }
                    return l - size;
                }
                sm = op.Operate(sm, d[l]);
                ++l;
            } while ((l & -l) != l);
            return _n;
        }
    }
}
0