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

        var d   = a.GroupBy(x => x).ToDictionary(g => g.Key, g => g.LongCount());
        var sum = d.Values.Sum(c => c * (c - 1) / 2);

        void Add(int x, int count)
        {
            var c = d.GetValueOrDefault(x);

            sum -= c * (c - 1) / 2;
            c   += count;
            sum += c * (c - 1) / 2;
            d[x] = c;
        }

        var map = new AvlMap <int, int>();

        for (var(l, r) = (0, 1); r <= n; r++)
        {
            if (r == n || a[r] != a[r - 1])
            {
                map.Add(l, r);
                l = r;
            }
        }

        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        while (qc-- > 0)
        {
            var(L, R, X) = Read3();
            L--;

            var ranges = map.GetItems(p => L < p.Value, p => p.Key < R).ToArray();
            var(ll, _)  = ranges[0];
            var(rl, rr) = ranges[^ 1];
Пример #2
0
    static void Main()
    {
        var n  = int.Parse(Console.ReadLine());
        var qs = Array.ConvertAll(new bool[n], _ => Console.ReadLine().Split());

        var keys = qs.Select(q => q[1]).Concat(qs.Where(q => q[0] == "3").Select(q => q[2])).Distinct().ToArray();

        Array.Sort(keys, StringComparer.Ordinal);
        var keymap = Enumerable.Range(0, keys.Length).ToDictionary(i => keys[i], i => i);

        var map = new AvlMap <int, string>();

        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        foreach (var q in qs)
        {
            var key = keymap[q[1]];
            if (q[0] == "0")
            {
                map[key] = q[2];
            }
            else if (q[0] == "1")
            {
                Console.WriteLine(map.GetValueOrDefault(key, "0"));
            }
            else if (q[0] == "2")
            {
                map.Remove(key);
            }
            else
            {
                foreach (var p in map.GetItems(x => x >= key, x => x <= keymap[q[2]]))
                {
                    Console.WriteLine($"{keys[p.Key]} {p.Value}");
                }
            }
        }
        Console.Out.Flush();
    }
Пример #3
0
    static void Main()
    {
        var n = int.Parse(Console.ReadLine());

        var c   = StringComparer.Ordinal;
        var map = new AvlMap <string, string>(c.Compare);

        Console.SetOut(new System.IO.StreamWriter(Console.OpenStandardOutput())
        {
            AutoFlush = false
        });
        while (n-- > 0)
        {
            var q = Console.ReadLine().Split();
            if (q[0] == "0")
            {
                map[q[1]] = q[2];
            }
            else if (q[0] == "1")
            {
                Console.WriteLine(map.GetValueOrDefault(q[1], "0"));
            }
            else if (q[0] == "2")
            {
                map.Remove(q[1]);
            }
            else
            {
                foreach (var p in map.GetItems(x => c.Compare(x, q[1]) >= 0, x => c.Compare(x, q[2]) <= 0))
                {
                    Console.WriteLine($"{p.Key} {p.Value}");
                }
            }
        }
        Console.Out.Flush();
    }