結果

問題 No.2650 [Cherry 6th Tune *] セイジャク
ユーザー tobisatistobisatis
提出日時 2024-02-23 21:29:44
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 1,103 ms / 2,500 ms
コード長 7,889 bytes
コンパイル時間 7,779 ms
コンパイル使用メモリ 166,940 KB
実行使用メモリ 226,176 KB
最終ジャッジ日時 2024-09-29 05:36:43
合計ジャッジ時間 35,275 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
30,464 KB
testcase_01 AC 52 ms
30,336 KB
testcase_02 AC 291 ms
51,712 KB
testcase_03 AC 173 ms
42,240 KB
testcase_04 AC 723 ms
71,532 KB
testcase_05 AC 562 ms
65,272 KB
testcase_06 AC 292 ms
48,640 KB
testcase_07 AC 589 ms
67,456 KB
testcase_08 AC 230 ms
43,372 KB
testcase_09 AC 1,024 ms
84,264 KB
testcase_10 AC 1,028 ms
84,096 KB
testcase_11 AC 1,010 ms
84,664 KB
testcase_12 AC 1,012 ms
84,652 KB
testcase_13 AC 1,049 ms
84,960 KB
testcase_14 AC 1,053 ms
84,864 KB
testcase_15 AC 1,103 ms
84,548 KB
testcase_16 AC 979 ms
84,868 KB
testcase_17 AC 925 ms
83,644 KB
testcase_18 AC 934 ms
84,708 KB
testcase_19 AC 946 ms
84,752 KB
testcase_20 AC 914 ms
84,628 KB
testcase_21 AC 920 ms
84,908 KB
testcase_22 AC 919 ms
84,736 KB
testcase_23 AC 896 ms
84,884 KB
testcase_24 AC 887 ms
84,016 KB
testcase_25 AC 911 ms
83,968 KB
testcase_26 AC 900 ms
84,096 KB
testcase_27 AC 892 ms
84,096 KB
testcase_28 AC 890 ms
84,736 KB
testcase_29 AC 896 ms
84,536 KB
testcase_30 AC 885 ms
84,224 KB
testcase_31 AC 972 ms
84,480 KB
testcase_32 AC 458 ms
226,176 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (82 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 #

namespace AtCoder;

#nullable enable

using System.Numerics;

static class Extensions
{
    public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray();
}

abstract class SegmentTree<TMonoid>
{
    public interface IOperator
    {
        TMonoid IdentityElement { get; }
        TMonoid Operation(TMonoid x, TMonoid y);
    }

    public interface IBinarySearchOperator { bool F(TMonoid value); }

    public class Body<TOperator> where TOperator : struct, IOperator
    {
        public int BaseSize { get; init; }
        protected int Height { get; init; }
        protected TOperator Op { get; init; }
        protected TMonoid[] Values { get; init; }

        public Body(TMonoid[] initial, TOperator op = default)
        {
            Op = op;
            (BaseSize, Height) = (1, 0);
            while (BaseSize < initial.Length) (BaseSize, Height) = (BaseSize << 1, Height + 1);
            var size = BaseSize << 1;
            Values = new TMonoid[size];
            for (var i = 0; i < size; i++) Values[i] = op.IdentityElement;
            for (var i = 0; i < initial.Length; i++) Values[i + BaseSize] = initial[i];
            for (var i = BaseSize - 1; i > 0; i--) Update(i);
        }

        public TMonoid Product(int l, int r)
        {
            (l, r) = (l + BaseSize, r + BaseSize);
            var (resL, resR) = (Op.IdentityElement, Op.IdentityElement);
            while (l < r)
            {
                if ((l & 1) == 1) resL = Op.Operation(resL, Values[l++]);
                if ((r & 1) == 1) resR = Op.Operation(Values[--r], resR);
                (l, r) = (l >> 1, r >> 1);
            }
            return Op.Operation(resL, resR);
        }

        public void Set(int index, TMonoid value)
        {
            var j = index + BaseSize;
            Values[j] = value;
            for (var i = 1; i <= Height; i++) Update(j >> i);
        }

        // the result might be greater than n
        public int MinIndex<TMinIndexOperator>(TMinIndexOperator op = default) where TMinIndexOperator : struct, IBinarySearchOperator
        {
            if (!op.F(Values[1])) return BaseSize + 1;
            var product = Op.IdentityElement;
            var (res, w, i) = (0, BaseSize, 1);
            while (w > 0)
            {
                var p = Op.Operation(product, Values[i]);
                if (!op.F(p))
                {
                    res += w;
                    i++;
                    product = p;
                }
                w >>= 1;
                i <<= 1;
            }
            return res + 1;
        }

        protected void Update(int k) { Values[k] = Op.Operation(Values[k << 1], Values[(k << 1) + 1]); }
    }
}

abstract class LazySegmentTree<TMonoid, TFunction>
{
    public interface IOperator : SegmentTree<TMonoid>.IOperator
    {
        TFunction IdentityFunction { get; }
        TMonoid Mapping(TFunction f, TMonoid x);
        TFunction Composition(TFunction f, TFunction g);
    }

    public class Body<TOperator> : SegmentTree<TMonoid>.Body<TOperator> where TOperator : struct, IOperator
    {
        TFunction[] Lazy { get; init; }

        public Body(TMonoid[] initial, TOperator op = default) : base(initial, op)
        {
            var size = BaseSize << 1;
            Lazy = new TFunction[size];
            for (var i = 0; i < size; i++) Lazy[i] = op.IdentityFunction;
        }

        public new TMonoid Product(int l, int r)
        {
            if (l == r) return Op.IdentityElement;
            Ascend(l, r);
            return base.Product(l, r);
        }

        public new void Set(int index, TMonoid value)
        {
            var j = index + BaseSize;
            for (var i = Height; i >= 1; i--) Propagate(j >> i);
            Values[j] = value;
            for (var i = 1; i <= Height; i++) Update(j >> i);
        }

        public void Apply(int l, int r, TFunction f)
        {
            if (l == r) return;
            Ascend(l, r);
            (l, r) = (l + BaseSize, r + BaseSize);
            var (l2, r2) = (l, r);
            while (l2 < r2)
            {
                if ((l2 & 1) == 1) Apply(l2++, f);
                if ((r2 & 1) == 1) Apply(--r2, f);
                (l2, r2) = (l2 >> 1, r2 >> 1);
            }
            for (int i = 1; i <= Height; i++)
            {
                if (((l >> i) << i) != l) Update(l >> i);
                if (((r >> i) << i) != r) Update((r - 1) >> i);
            }
        }

        void Ascend(int l, int r)
        {
            (l, r) = (l + BaseSize, r + BaseSize);
            for (int i = Height; i >= 1; i--)
            {
                if (((l >> i) << i) != l) Propagate(l >> i);
                if (((r >> i) << i) != r) Propagate((r - 1) >> i);
            }
        }
        void Propagate(int k)
        {
            Apply(k << 1, Lazy[k]);
            Apply((k << 1) + 1, Lazy[k]);
            Lazy[k] = Op.IdentityFunction;
        }
        void Apply(int k, TFunction f)
        {
            Values[k] = Op.Mapping(f, Values[k]);
            if (k < BaseSize) Lazy[k] = Op.Composition(Lazy[k], f);
        }
    }
}

readonly struct Op : LazySegmentTree<int, int>.IOperator
{
    public int IdentityFunction => -1;
    public int IdentityElement => -1;
    public int Composition(int f, int g) => Math.Max(f, g);
    public int Mapping(int f, int x) => Math.Max(f, x);
    public int Operation(int x, int y) => Math.Max(x, y);
}

class AtCoder
{
    object? Solve()
    {
        var n = Int();
        var _ = Int();
        var ns = new HashSet<int>();
        var xz = new int[n];
        for (var i = 0; i < n; i++)
        {
            var x = Int();
            xz[i] = x;
            ns.Add(x);
        }
        var t = Int();
        var sounds = new (int, int)[t];
        for (var i = 0; i < t; i++)
        {
            var l = Int();
            var r = Int();
            sounds[i] = (l, r);
            ns.Add(l);
            ns.Add(r);
        }
        var d = new Dictionary<int, int>();
        {
            var l = new List<int>(ns);
            l.Sort();
            for (var i = 0; i < l.Count; i++)
            {
                d[l[i]] = i;
            }
        }
        var tree = new LazySegmentTree<int, int>.Body<Op>(d.Count.Repeat(() => -1));
        for (var i = 0; i < t; i++)
        {
            var (lo, ro) = sounds[i];
            var l = d[lo];
            var r = d[ro];
            tree.Apply(l, r + 1, i + 1);
        }
        for (var i = 0; i < n; i++)
        {
            var x = d[xz[i]];
            Out(tree.Product(x, x + 1));
        }
        return null;
    }

    public static void Main() => new AtCoder().Run();
    public void Run()
    {
        var res = Solve();
        if (res != null)
        {
            if (res is bool yes) res = yes ? "Yes" : "No";
            sw.WriteLine(res);
        }
        sw.Flush();
    }

    string[] input = Array.Empty<string>();
    int iter = 0;
    readonly StreamWriter sw = new(Console.OpenStandardOutput()) { AutoFlush = false };

    #pragma warning disable IDE0051
    string String()
    {
        while (iter >= input.Length) (input, iter) = (Console.ReadLine()!.Split(' '), 0);
        return input[iter++];
    }
    T Input<T>() where T : IParsable<T> => T.Parse(String(), null);
    int Int() => Input<int>();
    void Out(object? x, string? separator = null)
    {
        separator ??= Environment.NewLine;
        if (x is System.Collections.IEnumerable obj and not string)
        {
            var firstLine = true;
            foreach (var item in obj)
            {
                if (!firstLine) sw.Write(separator);
                firstLine = false;
                sw.Write(item);
            }
        }
        else sw.Write(x);
        sw.WriteLine();
    }
    #pragma warning restore IDE0051
}
0