yukicoder    次のyukicoderの問題へ    前のyukicoderの問題へ

yukicoder 2930 Larger Mex


問題へのリンク


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("5 1");
            WillReturn.Add("0 3 1 0 2");
            //2
            //3
            //3
            //2
            //1
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4 0");
            WillReturn.Add("1 2 3 4");
            //4
            //3
            //2
            //1
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("20 3");
            WillReturn.Add("0 2 1 1 1 5 7 0 2 1 4 1 4 8 0 3 0 0 4 2");
            //0
            //0
            //2
            //3
            //5
            //6
            //9
            //9
            //10
            //10
            //10
            //9
            //8
            //7
            //6
            //5
            //4
            //3
            //2
            //1
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        long[] wkArr = InputList[0].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long M = wkArr[1];

        long[] AArr = InputList[1].Split(' ').Select(pX => long.Parse(pX)).ToArray();
        long UB = AArr.GetUpperBound(0);

        // Mexが0の場合
        if (M == 0) {
            for (long I = AArr.Length; 1 <= I; I--) {
                Console.WriteLine(I);
            }
            return;
        }

        // 件数[値]なDict
        var CntDict = new Dictionary<long, long>();

        if (AArr[0] <= M) {
            DictIncrement(CntDict, AArr[0]);
        }

        var InsDualSegmentTree = new DualSegmentTree(AArr.Length);

        long R = 0;
        for (long L = 0; L <= UB; L++) {
            while (R + 1 <= UB) {
                if (CntDict.Count == M) {
                    break;
                }

                if (AArr[R + 1] <= M - 1) {
                    long CurrVal = AArr[R + 1];
                    DictIncrement(CntDict, CurrVal);

                    if (CntDict.Count == M) {
                        R++;
                        break;
                    }
                }
                R++;
            }

            if (CntDict.Count == M) {
                //Console.WriteLine("左端{0}だと右端は{1}", L, R);

                long LenMin = R - L + 1;
                long LenMax = UB - L + 1;
                InsDualSegmentTree.RangeAdd(LenMin, LenMax, 1);
            }

            if (AArr[L] <= M - 1) {
                DictDecrement(CntDict, AArr[L]);
            }
        }

        for (long I = 1; I <= AArr.Length; I++) {
            Console.WriteLine(InsDualSegmentTree[I]);
        }
    }

    // Dictの指定したキーの値をインクリメント
    static void DictIncrement(Dictionary<long, long> pDict, long pKey)
    {
        if (pDict.ContainsKey(pKey) == false) {
            pDict[pKey] = 0;
        }
        pDict[pKey]++;
    }

    // Dictの指定したキーの値をデクリメント
    static void DictDecrement(Dictionary<long, long> pDict, long pKey)
    {
        if (--pDict[pKey] == 0) {
            pDict.Remove(pKey);
        }
    }
}

// 区間加算、1点取得な双対セグ木(フェニック木使用)
#region DualSegmentTree
internal class DualSegmentTree
{
    private long[] mBitArr; // 内部配列(1オリジンなため、添字0は未使用)
    private long mExternalArrUB;

    // ノードのIndexの列挙を返す
    internal IEnumerable<long> GetNodeIndEnum()
    {
        for (long I = 0; I <= GetUB(); I++) {
            yield return I;
        }
    }

    // ノードのUBを返す
    internal long GetUB()
    {
        return mExternalArrUB;
    }

    // コンストラクタ
    // フェニック木の外部配列(0オリジン)のUBを指定
    internal DualSegmentTree(long pExternalArrUB)
    {
        mExternalArrUB = pExternalArrUB;

        // フェニック木の外部配列は0オリジンで、
        // フェニック木の内部配列は1オリジンなため、2を足す
        mBitArr = new long[pExternalArrUB + 2];
    }

    // インデクサ
    internal long this[long pInd]
    {
        get { return GetVal(pInd); }
        set { RangeAdd(pInd, pInd, value - GetVal(pInd)); }
    }

    // 双対セグメント木の機能
    // 区間加算
    internal void RangeAdd(long pSta, long pEnd, long AddVal)
    {
        pSta++; // 1オリジンに変更
        pEnd++; // 1オリジンに変更

        long ImosSta = pSta;
        long ImosEnd = pEnd + 1;

        // いもす法
        FenwickTree_Add(ImosSta, AddVal);
        if (ImosEnd <= mBitArr.GetUpperBound(0)) {
            FenwickTree_Add(ImosEnd, -AddVal);
        }
    }

    // 双対セグメント木の機能
    // 1点取得
    internal long GetVal(long pInd)
    {
        pInd++; // 1オリジンに変更
        return FenwickTree_GetSum(1, pInd);
    }

    // フェニック木の機能
    // [pSta,pEnd] のSumを返す
    private long FenwickTree_GetSum(long pSta, long pEnd)
    {
        return FenwickTree_GetSum(pEnd) - FenwickTree_GetSum(pSta - 1);
    }

    // フェニック木の機能
    // [0,pEnd] のSumを返す
    private long FenwickTree_GetSum(long pEnd)
    {
        long Sum = 0;
        while (pEnd >= 1) {
            Sum += mBitArr[pEnd];
            pEnd -= pEnd & -pEnd;
        }
        return Sum;
    }

    // フェニック木の機能
    // [I] に Xを加算
    private void FenwickTree_Add(long pI, long pX)
    {
        while (pI <= mBitArr.GetUpperBound(0)) {
            mBitArr[pI] += pX;
            pI += pI & -pI;
        }
    }
}
#endregion


解説

Mexは左端を固定すれば、右端に対して単調増加なので
尺取法で
左端ごとに、右端がどこまで伸びればMexがM以上になるかを調べ、
双対セグ木で集計してます。