結果

問題 No.2650 [Cherry 6th Tune *] セイジャク
ユーザー 👑 kakel-sankakel-san
提出日時 2024-02-23 22:02:38
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 1,418 ms / 2,500 ms
コード長 9,301 bytes
コンパイル時間 8,456 ms
コンパイル使用メモリ 158,856 KB
実行使用メモリ 209,608 KB
最終ジャッジ日時 2024-02-23 22:03:26
合計ジャッジ時間 47,744 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
33,444 KB
testcase_01 AC 93 ms
33,444 KB
testcase_02 AC 341 ms
46,856 KB
testcase_03 AC 268 ms
45,604 KB
testcase_04 AC 1,069 ms
90,504 KB
testcase_05 AC 848 ms
82,256 KB
testcase_06 AC 433 ms
57,252 KB
testcase_07 AC 894 ms
86,136 KB
testcase_08 AC 324 ms
47,396 KB
testcase_09 AC 1,380 ms
91,364 KB
testcase_10 AC 1,377 ms
91,192 KB
testcase_11 AC 1,387 ms
90,944 KB
testcase_12 AC 1,418 ms
90,936 KB
testcase_13 AC 1,371 ms
91,184 KB
testcase_14 AC 1,382 ms
90,936 KB
testcase_15 AC 1,376 ms
90,948 KB
testcase_16 AC 1,295 ms
89,300 KB
testcase_17 AC 1,312 ms
90,348 KB
testcase_18 AC 1,332 ms
89,292 KB
testcase_19 AC 1,340 ms
89,284 KB
testcase_20 AC 1,283 ms
89,288 KB
testcase_21 AC 1,282 ms
89,268 KB
testcase_22 AC 1,287 ms
89,280 KB
testcase_23 AC 1,229 ms
89,288 KB
testcase_24 AC 1,218 ms
89,272 KB
testcase_25 AC 1,226 ms
89,292 KB
testcase_26 AC 1,223 ms
89,408 KB
testcase_27 AC 1,216 ms
89,272 KB
testcase_28 AC 1,220 ms
89,300 KB
testcase_29 AC 1,216 ms
89,300 KB
testcase_30 AC 1,251 ms
90,816 KB
testcase_31 AC 1,285 ms
90,812 KB
testcase_32 AC 473 ms
209,608 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (97 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.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 c = NList;
        var (n, a) = (c[0], c[1]);
        var x = NList;
        var t = NN;
        var map = NArr(t);
        var set = new HashSet<int>();
        foreach (var xi in x) set.Add(xi);
        foreach (var range in map)
        {
            set.Add(range[0]);
            set.Add(range[1]);
        }
        var list = new List<int>(set);
        list.Sort();
        var dic = new Dictionary<int, int>();
        for (var i = 0; i < list.Count; ++i) dic[list[i]] = i;

        var seg = new LazySegTree<int, int>(Enumerable.Repeat(-1, list.Count).ToArray(), new SegOp());
        for (var i = 0; i < t; ++i)
        {
            seg.Apply(dic[map[i][0]], dic[map[i][1]] + 1, i + 1);
        }
        var ans = new int[n];
        for (var i = 0; i < n; ++i) ans[i] = seg.Get(dic[x[i]]);
        WriteLine(string.Join("\n", ans));
    }
    struct SegOp : ILazySegTreeOperator<int, int>
    {
        public int Composition(int f, int g)
        {
            return Math.Max(f, g);
        }

        public int E()
        {
            return -1;
        }

        public int Id()
        {
            return -1;
        }

        public int Mapping(int f, int x)
        {
            return Math.Max(f, x);
        }

        public int Op(int a, int b)
        {
            return Math.Max(a, b);
        }
    }
    interface ILazySegTreeOperator<S, F>
    {
        /// <summary>集合S上の二項演算 S×S → S</summary>
        S Op(S a, S b);
        /// <summary>Sの単位元</summary>
        S E();
        /// <summary>写像f(x)</summary>
        S Mapping(F f, S x);
        /// <summary>写像の合成 f ○ g</summary>
        F Composition(F f, F g);
        /// <summary>恒等写像 id</summary>
        F Id();
    }
    // モノイドの型 S
    // 写像の型 F
    // 以下の関数を格納する T
    //   ・: S × S → S を計算する関数 S op(S a, S b)
    //   e を返す関数 S e()
    //   f(x) を返す関数 S mapping(F f, S x)
    //   f○gを返す関数 F composition(F f, F g)
    //   idを返す関数 F id()
    // S,Fはreadonlyにしておくと速い
    // Tの関数オーバーフローに注意
    class LazySegTree<S, F>
    {
        int _n;
        int size;
        int log;
        List<S> d;
        List<F> lz;
        ILazySegTreeOperator<S, F> op;
        public LazySegTree(int n, ILazySegTreeOperator<S, F> op)
        {
            _n = n;
            var v = new S[n];
            for (var i = 0; i < v.Length; ++i) v[i] = op.E();
            Init(v, op);
        }
        public LazySegTree(S[] v, ILazySegTreeOperator<S, F> op)
        {
            _n = v.Length;
            Init(v, op);
        }
        private void Init(S[] v, ILazySegTreeOperator<S, F> op)
        {
            size = 1;
            log = 0;
            this.op = op;
            while (size < v.Length)
            {
                size <<= 1;
                ++log;
            }
            d = Enumerable.Repeat(op.E(), size * 2).ToList();
            lz = Enumerable.Repeat(op. Id(), size).ToList();
            for (var i = 0; i < v.Length; ++i) d[size + i] = v[i];
            for (var i = size - 1; i >= 1; --i) Update(i);
        }

        /// <summary>一点更新</summary>
        public void Set(int pos, S x)
        {
            pos += size;
            for (var i = log; i >= 1; --i) Push(pos >> i);
            d[pos] = x;
            for (var i = 1; i <= log; ++i) Update(pos >> i);
        }

        /// <summary>一点取得</summary>
        public S Get(int pos)
        {
            pos += size;
            for (var i = log; i >= 1; --i) Push(pos >> i);
            return d[pos];
        }

        /// <summary>区間取得 op(a[l..r-1])</summary>
        public S Prod(int l, int r)
        {
            if (l == r) return op.E();
            l += size;
            r += size;
            for (var i = log; i >= 1; --i)
            {
                if (((l >> i) << i) != l) Push(l >> i);
                if (((r >> i) << i) != r) Push(r >> i);
            }
            S sml = op.E();
            S smr = op.E();
            while (l < r)
            {
                if ((l & 1) != 0) sml = op.Op(sml, d[l++]);
                if ((r & 1) != 0) smr = op.Op(d[--r], smr);
                l >>= 1;
                r >>= 1;
            }

            return op.Op(sml, smr);
        }

        /// <summary>全体取得 op(a[0..n-1])</summary>
        public S AllProd() => d[1];

        /// <summary>なにこれ a[p] = op_st(a[p], x)</summary>
        public void Apply(int pos, F f)
        {
            pos += size;
            for (var i = log; i >= 1; --i) Push(pos >> i);
            d[pos] = op.Mapping(f, d[pos]);
            for (var i = 1; i <= log; ++i) Update(pos >> i);
        }

        /// <summary>区間更新 i = l..r-1 について a[i] = op_st(a[i], x)</summary>
        public void Apply(int l, int r, F f)
        {
            if (l == r) return;
            l += size;
            r += size;

            for (var i = log; i >= 1; --i)
            {
                if (((l >> i) << i) != l) Push(l >> i);
                if (((r >> i) << i) != r) Push((r - 1) >> i);
            }
            {
                var l2 = l;
                var r2 = r;
                while (l < r)
                {
                    if ((l & 1) != 0) AllApply(l++, f);
                    if ((r & 1) != 0) AllApply(--r, f);
                    l >>= 1;
                    r >>= 1;
                }
                l = l2;
                r = r2;
            }
            for (var i = 1; i <= log; ++i)
            {
                if (((l >> i) << i) != l) Update(l >> i);
                if (((r >> i) << i) != r) Update((r - 1) >> i);
            }
        }

        /// <summary>segtreeの上で二分探索をする
        /// Sを引数にとりboolを返す関数gが必要
        /// fが単調であれば、g(op(a[l], a[l + 1], ... a[r - 1])) = true となる最大のrが取得される
        /// 制約
        /// ・fに副作用がない
        /// ・f(op.E()) = true
        /// </summary>
        public int MaxRight(int l, Predicate<S> g)
        {
            if (l == _n) return _n;
            l += size;
            for (var i = log; i >= 1; --i) Push(l >> i);
            S sm = op.E();
            do
            {
                while (l % 2 == 0) l >>= 1;
                if (!g(op.Op(sm, d[l])))
                {
                    while (l < size)
                    {
                        Push(l);
                        if (g(op.Op(sm, d[l])))
                        {
                            sm = op.Op(sm, d[l]);
                            ++l;
                        }
                    }
                    return l - size;
                }
                sm = op.Op(sm, d[l]);
                ++l;
            } while ((l & -l) != l);
            return _n;
        }

        /// <summary>segtreeの上で二分探索をする
        /// Sを引数にとりboolを返す関数gが必要
        /// fが単調であれば、g(op(a[l], a[l + 1], ..., a[r - 1])) = true となる最小のlが取得される
        /// 制約
        /// ・fに副作用がない
        /// f(op.E()) = true
        public int MinLeft(int r, Predicate<S> g)
        {
            if (r == 0) return 0;
            r += size;
            for (var i = log; i >= 1; --i) Push((r - 1) >> i);
            S sm = op.E();
            do
            {
                --r;
                while (r > 1 && r % 2 == 1) r >>= 1;
                if (!g(op.Op(d[r], sm)))
                {
                    while (r < size)
                    {
                        Push(r);
                        r = (2 * r + 1);
                        if (g(op.Op(d[r], sm)))
                        {
                            sm = op.Op(d[r], sm);
                            --r;
                        }
                    }
                    return r + 1 - size;
                }
                sm = op.Op(d[r], sm);
            } while ((r & -r) != r);
            return 0;
        }

        void Update(int k)
        {
            d[k] = op.Op(d[2 * k], d[2 * k + 1]);
        }
        void AllApply(int k, F f)
        {
            d[k] = op.Mapping(f, d[k]);
            if (k < size) lz[k] = op.Composition(f, lz[k]);
        }
        void Push(int k)
        {
            AllApply(2 * k, lz[k]);
            AllApply(2 * k + 1, lz[k]);
            lz[k] = op.Id();
        }
    }
}
0