結果

問題 No.2809 Sort Query
ユーザー tobisatistobisatis
提出日時 2024-07-12 23:26:27
言語 C#
(.NET 8.0.203)
結果
TLE  
実行時間 -
コード長 8,411 bytes
コンパイル時間 9,877 ms
コンパイル使用メモリ 168,124 KB
実行使用メモリ 195,716 KB
最終ジャッジ日時 2024-07-12 23:29:16
合計ジャッジ時間 22,656 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
30,072 KB
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 TLE -
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 TLE -
testcase_45 TLE -
testcase_46 TLE -
testcase_47 TLE -
testcase_48 TLE -
testcase_49 TLE -
testcase_50 TLE -
testcase_51 AC 1,034 ms
168,088 KB
testcase_52 AC 1,010 ms
167,824 KB
testcase_53 AC 1,073 ms
169,752 KB
testcase_54 AC 1,111 ms
167,964 KB
testcase_55 AC 994 ms
167,816 KB
testcase_56 AC 1,890 ms
121,328 KB
testcase_57 AC 1,503 ms
106,720 KB
testcase_58 AC 1,411 ms
110,464 KB
testcase_59 AC 1,408 ms
93,184 KB
testcase_60 AC 1,945 ms
153,472 KB
testcase_61 TLE -
testcase_62 AC 1,439 ms
111,380 KB
testcase_63 AC 1,929 ms
142,208 KB
testcase_64 TLE -
testcase_65 AC 1,671 ms
131,072 KB
testcase_66 AC 58 ms
30,336 KB
testcase_67 AC 61 ms
30,336 KB
testcase_68 AC 94 ms
30,080 KB
testcase_69 AC 81 ms
29,792 KB
testcase_70 AC 55 ms
195,716 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (90 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;

abstract class SelfBalancingTreeBase<T>
{
    protected class Node
    {
        public required T Data { get; set; }
        public Node? L { get; set; }
        public Node? R { get; set; }
        public int Height { get; set; } = 1;
        public int Size { get; set; } = 1;
        public Node? this[bool right]
        {
            get => right ? R : L;
            set { if (right) R = value; else L = value; }
        }
    }
    
    protected static Node At(Node? node, int index)
    {
        while (node != null)
        {
            var ls = Size(node.L);
            if (index == ls) return node;
            var right = index > ls;
            if (right) index -= ls + 1;
            node = node[right];
        }
        throw new IndexOutOfRangeException();
    }

    protected Node? Root { get; set; }
    protected static int Size(Node? node) => node?.Size ?? 0;
    protected static int Height(Node? node) => node?.Height ?? 0;
    protected static int HeightDiff(Node node) => Height(node.R) - Height(node.L);
    protected static void Update(Node node)
    {
        node.Height = Math.Max(Height(node.L), Height(node.R)) + 1;
        node.Size = Size(node.L) + Size(node.R) + 1;
    }

    protected static Node? DeleteMin(Node node, Node swap)
    {
        if (node.L == null) { swap.Data = node.Data; return node.R; }
        node.L = DeleteMin(node.L, swap);
        return Balance(node);
    }

    protected static Node Balance(Node node)
    {
        var diff = HeightDiff(node);
        if (diff < -1 || 1 < diff)
        {
            var right = diff > 0;
            var next = node[right]!;
            if (HeightDiff(next) * diff < 0) node[right] = Rotate(next, right);
            return Rotate(node, !right);
        }
        Update(node);
        return node;
    }

    protected static Node Rotate(Node node, bool right)
    {
        var n0 = node[!right]!;
        node[!right] = n0[right];
        Update(node);
        n0[right] = node;
        Update(n0);
        return n0;
    }

    public bool DeleteAt(int index) => Size(Root) != Size(Root = DeleteAt(Root, index));
    static Node? DeleteAt(Node? node, int index)
    {
        if (node == null) return null;
        var ls = Size(node.L);
        if (index == ls)
        {
            if (node.R == null) return node.L;
            node.R = DeleteMin(node.R, node);
        }
        else
        {
            var right = index > ls;
            node[right] = DeleteAt(node[right], right ? index - 1 - ls : index);
        }
        return Balance(node);
    }

    protected IEnumerator<T> GetEnumerator()
    {
        var stack = new Stack<Node>();
        var lastR = Root;
        while (lastR != null || stack.Count > 0)
        {
            while (lastR != null)
            {
                stack.Push(lastR);
                lastR = lastR.L;
            }
            var last = stack.Pop();
            yield return last.Data;
            lastR = last.R;
        }
    }

    public int Count { get => Size(Root); }
}

abstract class SelfBalancingTree<TKey, TValue> : SelfBalancingTreeBase<KeyValuePair<TKey, TValue>> where TKey : IComparable<TKey>
{
    public int MaxIndex(TKey key, bool include) => Bound(Root, key, true, include).Item1;
    public int MinIndex(TKey key, bool include) => Bound(Root, key, false, include).Item1;
    protected static (int, Node?) Bound(Node? node, TKey key, bool max, bool include)
    {
        var index = max ? -1 : 0;
        Node? res = null;
        while (node != null)
        {
            var comp = key.CompareTo(node.Data.Key);
            var left = comp < 0 || comp == 0 && (max ^ include);
            if (!left) index += Size(node.L) + 1;
            if (max ^ left) res = node;
            node = left ? node.L : node.R;
        }
        return (index, res);
    }
    public int CountKey(TKey key) => MaxIndex(key, true) - MaxIndex(key, false);

    public ValueTuple<TKey>? MaxKey(TKey key, bool include) => ToOptionalKey(Bound(Root, key, true, include).Item2);
    public ValueTuple<TKey>? MinKey(TKey key, bool include) => ToOptionalKey(Bound(Root, key, false, include).Item2);
    static ValueTuple<TKey>? ToOptionalKey(Node? node) => node == null ? null : new(node.Data.Key);

    protected static Node? Find(Node? node, TKey key)
    {
        var res = Bound(node, key, false, true).Item2;
        return res != null && key.CompareTo(res.Data.Key) == 0 ? res : null;
    }

    protected static KeyValuePair<TKey, TValue> CreateData(TKey key, TValue value) => new(key, value);

    protected void Insert(KeyValuePair<TKey, TValue> data) => Root = Insert(Root, data);
    static Node Insert(Node? node, KeyValuePair<TKey, TValue> data)
    {
        if (node == null) return new Node{ Data = data };
        var right = data.Key.CompareTo(node.Data.Key) >= 0;
        node[right] = Insert(node[right], data);
        return Balance(node);
    }

    public bool Delete(TKey key) => Size(Root) != Size(Root = Delete(Root, key));
    static Node? Delete(Node? node, TKey key)
    {
        if (node == null) return null;
        var comp = key.CompareTo(node.Data.Key);
        if (comp == 0)
        {
            if (node.R == null) return node.L;
            node.R = DeleteMin(node.R, node);
        }
        else
        {
            var right = comp > 0;
            node[right] = Delete(node[right], key);
        }
        return Balance(node);
    }
}

class OrderedMultiSet<T> : SelfBalancingTree<T, ValueTuple> where T : IComparable<T> 
{
    public bool Contains(T key) => Find(Root, key) != null;
    public T At(int index) => At(Root, index).Data.Key;
    public void Insert(T key) => Insert(CreateData(key, ValueTuple.Create()));
    public new IEnumerator<T> GetEnumerator()
    {
        using var e = base.GetEnumerator();
        while (e.MoveNext()) yield return e.Current.Key;
    }
}

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

class AtCoder
{
    object? Solve()
    {
        var n = Int();
        var q = Int();
        var sorted = new OrderedMultiSet<long>();
        var d = new Dictionary<int, long>();
        for (var i = 0; i < n; i++)
        {
            d[i] = Input<long>();
            sorted.Insert(-1);
        }
        var ans = new List<long>();
        for (var i = 0; i < q; i++)
        {
            var t = Int();
            if (t == 2)
            {
                var removed = new List<long>();
                foreach (var k in d.Keys) removed.Add(sorted.At(k));
                foreach (var v in removed) sorted.Delete(v);
                foreach (var v in d.Values) sorted.Insert(v);
                d = new();
            }
            else
            {
                var k = Int() - 1;
                if (t == 3)
                {
                    if (d.TryGetValue(k, out var v)) ans.Add(v);
                    else ans.Add(sorted.At(k));
                }
                else
                {
                    var x = Input<long>();
                    d[k] = x;
                }
            }
        }
        Out(ans);
        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 };

    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();
    }
}
0