AtCoderのARC    次のARCの問題へ    前のARCの問題へ

ARC206-A Range Replace


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("4");
            WillReturn.Add("1 1 2 3");
            //4
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10");
            WillReturn.Add("2 5 6 5 2 1 7 9 7 2");
            //41
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static long[] GetSplitArr(string pStr)
    {
        return (pStr == "" ? new string[0] : pStr.Split(' ')).Select(pX => long.Parse(pX)).ToArray();
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] AArr = GetSplitArr(InputList[1]);
        long UB = AArr.GetUpperBound(0);

        long Answer = 0;
        var IndListDict = new Dictionary<long, List<long>>();

        for (long I = 0; I <= UB; I++) {
            if (IndListDict.ContainsKey(AArr[I]) == false) {
                IndListDict[AArr[I]] = new List<long>();
            }
            IndListDict[AArr[I]].Add(I);
        }

        for (long I = 0; I <= UB - 1; I++) {
            if (AArr[I] != AArr[I + 1]) {
                long RangeSta = I + 1;
                long RangeEnd = UB;
                long AppearCnt = GetListRangeValueCnt.GetRangeCnt(IndListDict[AArr[I]], RangeSta, RangeEnd);
                Answer += RangeEnd - RangeSta + 1 - AppearCnt;
            }
        }
        Console.WriteLine(Answer + 1);
    }
}

#region GetListRangeValueCnt
// {昇順にソートされたList,Min,Max}を引数とし、
// Min以上Max以下な値の個数を返す
internal class GetListRangeValueCnt
{
    // 機能01 Min以上な値の個数を返す
    static internal long GetMoreOrEqualCnt(List<long> pSortedList, long pMinVal)
    {
        int ResultInd = ExecNibunhou_LowerBound(pMinVal, pSortedList);

        if (ResultInd == -1) return 0;
        return (pSortedList.Count - 1) - ResultInd + 1;
    }

    // 機能02 Min超えな値の個数を返す
    static internal long GetMoreStrictCnt(List<long> pSortedList, long pMinVal)
    {
        int ResultInd = ExecNibunhou_UpperBound(pMinVal, pSortedList);

        if (ResultInd == -1) return 0;
        return (pSortedList.Count - 1) - ResultInd + 1;
    }

    // 機能03 Max以下な値の個数を返す
    static internal long GetLessOrEqualCnt(List<long> pSortedList, long pMaxVal)
    {
        int ResultInd = ExecNibunhou_LowerOrEqual_Max(pMaxVal, pSortedList);

        if (ResultInd == -1) return 0;
        return ResultInd + 1;
    }

    // 機能04 Max未満な値の個数を返す
    static internal long GetLessStrictCnt(List<long> pSortedList, long pMaxVal)
    {
        int ResultInd = ExecNibunhou_LowerMax(pMaxVal, pSortedList);

        if (ResultInd == -1) return 0;
        return ResultInd + 1;
    }

    // 機能05 Min以上Max以下な値の個数を返す
    static internal long GetRangeCnt(List<long> pSortedList, long pMinVal, long pMaxVal)
    {
        if (pMinVal > pMaxVal) {
            throw new Exception("pMinVal > pMaxVal");
        }

        int ResultInd1 = ExecNibunhou_LowerBound(pMinVal, pSortedList);
        if (ResultInd1 == -1) return 0;

        int ResultInd2 = ExecNibunhou_LowerOrEqual_Max(pMaxVal, pSortedList);
        if (ResultInd2 == -1) return 0;

        return ResultInd2 - ResultInd1 + 1;
    }

    // 二分法で、Val以上で最小の値を持つ、添字を返す
    static private int ExecNibunhou_LowerBound(long pVal, List<long> pList)
    {
        if (pList.Count == 0) return -1;

        // 最後の要素がVal未満の特殊ケース
        if (pVal > pList[pList.Count - 1]) {
            return -1;
        }
        // 最初の要素がVal以上の特殊ケース
        if (pVal <= pList[0]) {
            return 0;
        }

        int L = 0;
        int R = pList.Count - 1;

        while (L + 1 < R) {
            int Mid = (L + R) / 2;

            if (pList[Mid] >= pVal) {
                R = Mid;
            }
            else {
                L = Mid;
            }
        }
        return R;
    }

    // 二分法で、Val超えで最小の値を持つ、添字を返す
    static private int ExecNibunhou_UpperBound(long pVal, List<long> pList)
    {
        // 要素が0件のケース
        if (pList.Count == 0) return -1;

        // 最後の要素がVal以下の特殊ケース
        if (pVal >= pList[pList.Count - 1]) {
            return -1;
        }
        // 最初の要素がVal超えの特殊ケース
        if (pVal < pList[0]) {
            return 0;
        }

        int L = 0;
        int R = pList.Count - 1;

        while (L + 1 < R) {
            int Mid = (L + R) / 2;

            if (pList[Mid] > pVal) {
                R = Mid;
            }
            else {
                L = Mid;
            }
        }
        return R;
    }

    // 二分法で、Val以下で最大の値を持つ、添字を返す
    static private int ExecNibunhou_LowerOrEqual_Max(long pVal, List<long> pList)
    {
        if (pList.Count == 0) return -1;

        // 最後の要素がVal以下の特殊ケース
        if (pVal >= pList[pList.Count - 1]) {
            return pList.Count - 1;
        }
        // 最初の要素がVal超えの特殊ケース
        if (pVal < pList[0]) {
            return -1;
        }

        int L = 0;
        int R = pList.Count - 1;

        while (L + 1 < R) {
            int Mid = (L + R) / 2;

            if (pList[Mid] <= pVal) {
                L = Mid;
            }
            else {
                R = Mid;
            }
        }
        return L;
    }

    // 二分法で、Val未満で最大の値を持つ、添字を返す
    static private int ExecNibunhou_LowerMax(long pVal, List<long> pList)
    {
        if (pList.Count == 0) return -1;

        // 最後の要素がVal未満の特殊ケース
        if (pVal > pList[pList.Count - 1]) {
            return pList.Count - 1;
        }
        // 最初の要素がVal以上の特殊ケース
        if (pVal <= pList[0]) {
            return -1;
        }

        int L = 0;
        int R = pList.Count - 1;

        while (L + 1 < R) {
            int Mid = (L + R) / 2;

            if (pList[Mid] < pVal) {
                L = Mid;
            }
            else {
                R = Mid;
            }
        }
        return L;
    }
}
#endregion


解説

以下の例で考えます。

1 1 2 3 4 5 1 1 1 2 3 4 5 1 1 2 3 4 5

次が1でなければ、
1以外の値を、何個1で上書きするかが解になります。

ウェーブレット木で、末尾までの
1の個数を求め、末尾までの区間長から引けば、
末尾までの1の個数が分かりますが、
ウェーブレット木だとTLEしました。

なので
IndのList[値]なDictを最初に作り、
IndのList内を二分探索してます。