結果

問題 No.1705 Mode of long array
ユーザー 👑 kakel-sankakel-san
提出日時 2023-12-30 21:40:24
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 836 ms / 3,000 ms
コード長 9,303 bytes
コンパイル時間 7,613 ms
コンパイル使用メモリ 157,016 KB
実行使用メモリ 224,504 KB
最終ジャッジ日時 2023-12-30 21:40:58
合計ジャッジ時間 33,804 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
33,060 KB
testcase_01 AC 83 ms
33,060 KB
testcase_02 AC 83 ms
33,060 KB
testcase_03 AC 88 ms
33,828 KB
testcase_04 AC 88 ms
33,700 KB
testcase_05 AC 89 ms
33,956 KB
testcase_06 AC 97 ms
35,748 KB
testcase_07 AC 103 ms
36,772 KB
testcase_08 AC 100 ms
36,260 KB
testcase_09 AC 104 ms
35,876 KB
testcase_10 AC 99 ms
35,748 KB
testcase_11 AC 100 ms
35,876 KB
testcase_12 AC 99 ms
35,876 KB
testcase_13 AC 479 ms
71,288 KB
testcase_14 AC 356 ms
65,676 KB
testcase_15 AC 309 ms
63,500 KB
testcase_16 AC 350 ms
64,404 KB
testcase_17 AC 283 ms
63,248 KB
testcase_18 AC 232 ms
55,716 KB
testcase_19 AC 453 ms
64,548 KB
testcase_20 AC 301 ms
63,648 KB
testcase_21 AC 419 ms
69,632 KB
testcase_22 AC 560 ms
68,200 KB
testcase_23 AC 276 ms
62,072 KB
testcase_24 AC 277 ms
61,944 KB
testcase_25 AC 276 ms
61,944 KB
testcase_26 AC 275 ms
62,072 KB
testcase_27 AC 275 ms
61,944 KB
testcase_28 AC 275 ms
62,072 KB
testcase_29 AC 277 ms
61,944 KB
testcase_30 AC 276 ms
61,944 KB
testcase_31 AC 274 ms
61,944 KB
testcase_32 AC 274 ms
61,944 KB
testcase_33 AC 827 ms
76,036 KB
testcase_34 AC 805 ms
76,036 KB
testcase_35 AC 805 ms
76,036 KB
testcase_36 AC 832 ms
76,036 KB
testcase_37 AC 798 ms
76,032 KB
testcase_38 AC 771 ms
76,036 KB
testcase_39 AC 820 ms
76,036 KB
testcase_40 AC 805 ms
76,036 KB
testcase_41 AC 810 ms
76,036 KB
testcase_42 AC 836 ms
76,036 KB
testcase_43 AC 354 ms
77,168 KB
testcase_44 AC 359 ms
76,144 KB
testcase_45 AC 352 ms
76,144 KB
testcase_46 AC 351 ms
76,016 KB
testcase_47 AC 353 ms
76,016 KB
testcase_48 AC 350 ms
76,016 KB
testcase_49 AC 587 ms
77,064 KB
testcase_50 AC 568 ms
77,064 KB
testcase_51 AC 590 ms
77,064 KB
testcase_52 AC 570 ms
77,064 KB
testcase_53 AC 576 ms
224,504 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (103 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 long[] NList => ReadLine().Split().Select(long.Parse).ToArray();
    static long[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var a = NList;
        var q = NN;
        var query = NArr(q);
        var tree = new AvlTree<Pair>();
        var counts = new Pair[m + 1];
        for (var i = 1; i < counts.Length; ++i)
        {
            counts[i] = new Pair(i, a[i - 1]);
            tree.Insert(counts[i]);
        }
        var ans = new List<int>();
        foreach (var que in query)
        {
            if (que[0] == 1)
            {
                tree.Delete(counts[que[1]]);
                counts[que[1]].Count += que[2];
                tree.Insert(counts[que[1]]);
            }
            else if (que[0] == 2)
            {
                tree.Delete(counts[que[1]]);
                counts[que[1]].Count -= que[2];
                tree.Insert(counts[que[1]]);
            }
            else
            {
                ans.Add(tree.ElementAt(tree.GetCount() - 1).A);
            }
        }
        WriteLine(string.Join("\n", ans));
    }
    class Pair : IComparable<Pair>
    {
        public int A;
        public long Count;
        public Pair(int a, long count)
        {
            A = a; Count = count;
        }
        public int CompareTo(Pair b)
        {
            var d = Count.CompareTo(b.Count);
            if (d != 0) return d;
            return A.CompareTo(b.A);
        }
    }
    public class AvlTree<T> where T: IComparable<T>
    {
        private class Node<U> where U: IComparable<U>
        {
            public U Value;
            public Node<U> Lc = null;
            public Node<U> Rc = null;
            public int Height;
            public int Count;
            public int CCount;
            public Node(int height, U value)
            {
                Height = height;
                Count = 1;
                CCount = 1;
                Value = value;
            }
        }
        private Node<T> root = null;
        static int GetHeight(Node<T> t)
        {
            return t == null ? 0 : t.Height;
        }
        static int GetCount(Node<T> t)
        {
            return t == null ? 0 : t.Count;
        }
        static int GetBalance(Node<T> t)
        {
            return GetHeight(t.Lc) - GetHeight(t.Rc);
        }
        static void Recalc(Node<T> t)
        {
            if (t == null) return;
            t.Height = 1 + Math.Max(GetHeight(t.Lc), GetHeight(t.Rc));
            t.Count = t.CCount + GetCount(t.Lc) + GetCount(t.Rc);
        }
        static Node<T> RotateLeft(Node<T> t)
        {
            Node<T> u = t.Rc;
            Node<T> t2 = u.Lc;
            u.Lc = t;
            t.Rc = t2;
            Recalc(t);
            Recalc(u);
            return u;
        }
        static Node<T> RotateRight(Node<T> t)
        {
            Node<T> u = t.Lc;
            Node<T> t2 = u.Rc;
            u.Rc = t;
            t.Lc = t2;
            Recalc(t);
            Recalc(u);
            return u;
        }
        static Node<T> RotateLR(Node<T> t)
        {
            t.Lc = RotateLeft(t.Lc);
            return RotateRight(t);
        }
        static Node<T> RotateRL(Node<T> t)
        {
            t.Rc = RotateRight(t.Rc);
            return RotateLeft(t);
        }
        static Node<T> BalanceLeft(Node<T> t)
        {
            if (GetBalance(t) > 1)
            {
                if (GetBalance(t.Lc) < 0) t = RotateLR(t);
                else t = RotateRight(t);
            }
            Recalc(t);
            return t;
        }
        static Node<T> BalanceRight(Node<T> t)
        {
            if (GetBalance(t) < -1)
            {
                if (GetBalance(t.Rc) > 0) t = RotateRL(t);
                else t = RotateLeft(t);
            }
            Recalc(t);
            return t;
        }
        /// <summary>valueを挿入する</summary>
        public void Insert(T value)
        {
            root = Insert(root, value);
        }
        Node<T> Insert(Node<T> cur, T value)
        {
            if (cur == null)
            {
                return new Node<T>(1, value);
            }
            var d = value.CompareTo(cur.Value);
            if (d < 0)
            {
                cur.Lc = Insert(cur.Lc, value);
                return BalanceLeft(cur);
            }
            else if (d > 0)
            {
                cur.Rc = Insert(cur.Rc, value);
                return BalanceRight(cur);
            }
            else
            {
                ++cur.CCount;
                Recalc(cur);
                return cur;
            }
        }
        /// <summary>valueを削除する 存在しない場合はなにもしない</summary>
        public void Delete(T value)
        {
            root = Delete(root, value);
        }
        Node<T> Delete(Node<T> cur, T value)
        {
            if (cur == null) return null;
            var d = value.CompareTo(cur.Value);
            if (d < 0)
            {
                cur.Lc = Delete(cur.Lc, value);
                return BalanceRight(cur);
            }
            else if (d > 0)
            {
                cur.Rc = Delete(cur.Rc, value);
                return BalanceLeft(cur);
            }
            else
            {
                --cur.CCount;
                if (cur.CCount == 0)
                {
                    if (cur.Lc != null)
                    {
                        Node<T> del = null;
                        var max = DeleteMax(cur.Lc, ref del);
                        cur.Lc = max;
                        cur.Value = del.Value;
                        cur.CCount = del.CCount;
                        return BalanceRight(cur);
                    }
                    else
                    {
                        return cur.Rc;
                    }
                }
                else
                {
                    Recalc(cur);
                    return cur;
                }
            }
        }
        Node<T> DeleteMax(Node<T> t, ref Node<T> del)
        {
            if (t.Rc == null)
            {
                del = t;
                return t.Lc;
            }
            else
            {
                var max = DeleteMax(t.Rc, ref del);
                t.Rc = max;
                return BalanceLeft(t);
            }
        }
        /// <summary>小さいほうからpos番目の要素を取得する</summary>
        public T ElementAt(int pos)
        {
            if (pos < 0 || pos >= GetCount(root)) return default;
            var t = ElementAt(root, pos);
            return t.Value;
        }
        /// <summary>小さいほうからpos番目の要素を取得する</summary>
        public T ElementAt(int pos, T defaultValue)
        {
            if (pos < 0 || pos >= GetCount(root)) return defaultValue;
            return ElementAt(pos);
        }
        Node<T> ElementAt(Node<T> cur, int pos)
        {
            if (pos < GetCount(cur.Lc)) return ElementAt(cur.Lc, pos);
            if (pos < GetCount(cur.Lc) + cur.CCount) return cur;
            if (pos < cur.Count) return ElementAt(cur.Rc, pos - GetCount(cur.Lc) - cur.CCount);
            return null;
        }
        public void Debug()
        {
            if (root == null) return;
            Console.WriteLine($"root val:{root.Value}, CCount:{root.CCount}, Count:{root.Count}");
            Debug(root.Value, root.Lc);
            Debug(root.Value, root.Rc);
        }
        void Debug(T pval, Node<T> cur)
        {
            if (cur == null) return;
            Console.WriteLine($"{pval} -> val:{cur.Value}, CCount:{cur.CCount}, Count:{cur.Count}");
            Debug(cur.Value, cur.Lc);
            Debug(cur.Value, cur.Rc);
        }
        /// <summary>value以上でもっとも小さい値とその場所を返却する</summary>
        public T LowerBound(T value, ref int index)
        {
            var cur = root;
            T ans = default;
            var add = 0;
            index = GetCount(root);
            while (cur != null)
            {
                if (value.CompareTo(cur.Value) <= 0)
                {
                    ans = cur.Value;
                    index = add + GetCount(cur.Lc);
                    cur = cur.Lc;
                }
                else
                {
                    add += GetCount(cur.Lc) + cur.CCount;
                    cur = cur.Rc;
                }
            }
            return ans;
        }
        /// <summary>要素の個数を求める</summary>
        public int GetCount()
        {
            return GetCount(root);
        }
    }
}
0