Пример #1
0
    static void Main()
    {
        var n = int.Parse(Console.ReadLine());
        var s = Console.ReadLine();

        var l = new AvlList <int>();

        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        for (int i = 0; i < n; i++)
        {
            var c = s[i];

            if (c == 'L')
            {
                l.Prepend(i + 1);
            }
            else if (c == 'R')
            {
                l.Add(i + 1);
            }
            else if (c < 'D')
            {
                var k = c - 'A';
                if (k < l.Count)
                {
                    Console.WriteLine(l.RemoveAt(k));
                }
                else
                {
                    Console.WriteLine("ERROR");
                }
            }
            else
            {
                var k = c - 'D';
                if (k < l.Count)
                {
                    Console.WriteLine(l.RemoveAt(l.Count - 1 - k));
                }
                else
                {
                    Console.WriteLine("ERROR");
                }
            }
        }
        Console.Out.Flush();
    }
Пример #2
0
    static void Main()
    {
        var(n, qc) = Read2();
        var a  = Read();
        var qs = Array.ConvertAll(new bool[qc], _ => Read3());

        var l = new AvlList <int>();

        foreach (var x in a)
        {
            l.Add(x);
        }

        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        foreach (var(t, x, y) in qs)
        {
            if (t == 1)
            {
                // Node を操作することで高速化できます。
                (l[x - 1], l[y - 1]) = (l[y - 1], l[x - 1]);
            }
            else if (t == 2)
            {
                l.Prepend(l.RemoveAt(n - 1));
            }
            else
            {
                Console.WriteLine(l[x - 1]);
            }
        }
        Console.Out.Flush();
    }