結果

問題 No.3137 Non-Intersect Chord Triangle Game
ユーザー kakel-san
提出日時 2025-05-02 23:22:57
言語 C#
(.NET 8.0.404)
結果
TLE  
実行時間 -
コード長 11,635 bytes
コンパイル時間 10,992 ms
コンパイル使用メモリ 170,396 KB
実行使用メモリ 88,980 KB
最終ジャッジ日時 2025-05-02 23:24:21
合計ジャッジ時間 75,043 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48 TLE * 2 -- * 15
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (107 ミリ秒)。
  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 c = NList;
        var (n, m) = (c[0], c[1]);
        var map = NArr(m);
        WriteLine(Game(n, m, map) ? "Akane" : "Aoi");
    }
    static bool Game(int n, int m, int[][] map)
    {
        for (var i = 0; i < m; ++i) if (map[i][0] > map[i][1]) (map[i][0], map[i][1]) = (map[i][1], map[i][0]);
        Array.Sort(map, (l, r) => Math.Min(l[1] - l[0], l[0] + n - l[1]).CompareTo(Math.Min(r[1] - r[0], r[0] + n - r[1])));

        var tree = new AvlTree<int>();
        for (var i = 1; i <= n; ++i) tree.Insert(i);

        var max = n;
        for (var i = 0; i < m; ++i)
        {
            var pos1 = tree.GetPos(map[i][0]);
            var pos2 = tree.GetPos(map[i][1]);
            var all = tree.GetCount();
            var inner = pos2 - pos1 - 1;
            var outer = all - pos2 + pos1 - 1;
            // WriteLine($"delete {map[i][0]} - {map[i][1]} at {pos1} - {pos2}: inner {inner}, outer {outer}");
            if (inner < outer)
            {
                if (inner % 4 == 2) return true;
                while (true)
                {
                    var del = tree.ElementAt(pos1);
                    tree.Delete(del);
                    if (del == map[i][1]) break;
                }
            }
            else
            {
                if (outer % 4 == 2) return true;
                while (tree.GetCount() > pos2)
                {
                    var del = tree.ElementAt(pos2);
                    tree.Delete(del);
                }
                while (true)
                {
                    var del = tree.ElementAt(0);
                    tree.Delete(del);
                    if (del == map[i][0]) break;
                }
            }
        }
        return tree.GetCount() % 4 == 2;
    }
    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);
        }
        /// <summary>要素の一覧を返す</summary>
        public List<T> GetValues()
        {
            var list = new List<T>();
            if (root != null) GetValues(root, list);
            return list;
        }
        static void GetValues(Node<T> cur, List<T> list)
        {
            if (cur.Lc != null) GetValues(cur.Lc, list);
            list.Add(cur.Value);
            if (cur.Rc != null) GetValues(cur.Rc, list);
        }
        /// <summary>指定された要素が存在するか求める</summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Contains(T value)
        {
            if (root == null) return false;
            return Contains(root, value);
        }
        static bool Contains(Node<T> cur, T value)
        {
            var d = cur.Value.CompareTo(value);
            if (d == 0) return true;
            if (d > 0)
            {
                if (cur.Lc == null) return false;
                return Contains(cur.Lc, value);
            }
            else
            {
                if (cur.Rc == null) return false;
                return Contains(cur.Rc, value);
            }
        }
        public int GetPos(T value)
        {
            if (root == null) return -1;
            return GetPos(root, value);
        }
        static int GetPos(Node<T> cur, T value)
        {
            var d = cur.Value.CompareTo(value);
            if (d == 0) return GetCount(cur.Lc);
            if (d > 0)
            {
                if (cur.Lc == null) return -1;
                return GetPos(cur.Lc, value);
            }
            else
            {
                if (cur.Rc == null) return -1;
                var ans = GetPos(cur.Rc, value);
                if (ans < 0) return ans;
                return ans + 1 + GetCount(cur.Lc);
            }
        }
    }
}
0