Exemplo n.º 1
0
    static object Solve()
    {
        var(n, qc) = Read2();
        var c  = Read();
        var qs = new bool[qc]
                 .Select((_, id) => { var a = Read(); return(id, l: a[0] - 1, r: a[1] - 1); })
                 .GroupBy(q => q.r)
                 .ToDictionary(g => g.Key, g => g.ToArray());

        // 各色に対して、最も右のインデックス
        var ris = Array.ConvertAll(new bool[n + 1], _ => - 1);
        var set = new IndexedSet <int>();

        var counts = new int[qc];

        for (int i = 0; i < n; i++)
        {
            if (ris[c[i]] != -1)
            {
                set.Remove(ris[c[i]]);
            }
            ris[c[i]] = i;
            set.Add(i);

            if (qs.ContainsKey(i))
            {
                foreach (var(id, l, _) in qs[i])
                {
                    var li = set.GetFirstIndex(x => x >= l);
                    counts[id] = set.Count - li;
                }
            }
        }

        return(string.Join("\n", counts));
    }