AtCoderのPAST    次のPASTの問題へ    前のPASTの問題へ

第7回PAST L たくさんの最小値


問題へのリンク


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

    struct TXYInfoDef
    {
        internal long T;
        internal long X;
        internal long Y;
    }

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

        long[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => long.Parse(pX)).ToArray();

        var TXYInfoList = new List<TXYInfoDef>();
        foreach (string EachStr in InputList.Skip(2)) {
            SplitAct(EachStr);
            TXYInfoDef WillAdd;
            WillAdd.T = wkArr[0];
            WillAdd.X = wkArr[1];
            WillAdd.Y = wkArr[2];
            TXYInfoList.Add(WillAdd);
        }

        var InsSegmentTree = new SegmentTree(AArr.Length);
        for (long I = 0; I <= AArr.GetUpperBound(0); I++) {
            long Packed = ExecPack(AArr[I], I);
            InsSegmentTree.Update(I, Packed);
        }

        foreach (TXYInfoDef EachTXYInfo in TXYInfoList) {
            long T = EachTXYInfo.T;
            long X = EachTXYInfo.X;
            long Y = EachTXYInfo.Y;

            if (T == 1) {
                long Packed = ExecPack(Y, X - 1);
                InsSegmentTree.Update(X - 1, Packed);
            }

            if (T == 2) {
                var AnswerIndList = new List<long>();
                long RangeSta = X - 1;
                long RangeEnd = Y - 1;
                long Key1 = InsSegmentTree.Query(RangeSta, RangeEnd, 0);
                long Val1, CurrInd;
                ExecUnPack(Key1, out Val1, out CurrInd);
                AnswerIndList.Add(CurrInd);
                while (true) {
                    CurrInd++;
                    if (CurrInd > RangeEnd) break;
                    long Key2 = InsSegmentTree.Query(CurrInd, RangeEnd, 0);
                    long Val2;
                    ExecUnPack(Key2, out Val2, out CurrInd);

                    if (Val1 != Val2) break;
                    AnswerIndList.Add(CurrInd);
                }
                Console.Write(AnswerIndList.Count);
                AnswerIndList.ForEach(pX => Console.Write(" {0}", pX + 1));
                Console.WriteLine();
            }
        }
    }

    // 値と添字をlong型にパックする
    static long ExecPack(long pVal, long pInd)
    {
        return pVal * 1000000000 + pInd;
    }

    // long型を、値と添字にアンパックする
    static void ExecUnPack(long pPacked, out long pVal, out long pInd)
    {
        pInd = pPacked % 1000000000;
        pVal = pPacked / 1000000000;
    }
}

#region SegmentTree
// SegmentTreeクラス
internal class SegmentTree
{
    private long[] mTreeNodeArr;
    private long UB; // 木のノードの配列のUB
    private long mLeafCnt; // 葉ノードの数

    // ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列
    private struct RangeInfoDef
    {
        internal long StaInd;
        internal long EndInd;
    }
    private RangeInfoDef[] mRangeInfo;

    // コンストラクタ
    internal SegmentTree(long pLeafCnt)
    {
        // 簡単のため、葉ノード数を2のべき乗に
        long ArrLength = 0;
        for (long I = 1; I < long.MaxValue; I *= 2) {
            ArrLength += I;
            mLeafCnt = I;

            if (pLeafCnt < mLeafCnt) break;
        }

        // すべての値をint.MaxValueに
        UB = ArrLength - 1;
        mTreeNodeArr = new long[UB + 1];
        for (long I = 0; I <= UB; I++) {
            mTreeNodeArr[I] = long.MaxValue;
        }

        // ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列の作成
        mRangeInfo = new RangeInfoDef[UB + 1];
        for (long I = 0; I <= UB; I++) {
            if (I == 0) {
                RangeInfoDef WillSet1;
                WillSet1.StaInd = 0;
                WillSet1.EndInd = mLeafCnt - 1;
                mRangeInfo[I] = WillSet1;
                continue;
            }
            long ParentNode = DeriveParentNode(I);
            RangeInfoDef ParentRangeInfo = mRangeInfo[ParentNode];

            RangeInfoDef WillSet2;
            long Mid = (ParentRangeInfo.StaInd + ParentRangeInfo.EndInd) / 2;

            if (I % 2 == 1) { // 奇数ノードの場合
                WillSet2.StaInd = ParentRangeInfo.StaInd;
                WillSet2.EndInd = Mid;
            }
            else { // 偶数ノードの場合
                WillSet2.StaInd = Mid + 1;
                WillSet2.EndInd = ParentRangeInfo.EndInd;
            }
            mRangeInfo[I] = WillSet2;
        }
        for (long I = 0; I <= UB; I++) {
            //Console.WriteLine("mRangeInfo[{0}] StaInd={1} EndInd={2}",
            //    I, mRangeInfo[I].StaInd, mRangeInfo[I].EndInd);
        }
    }

    // 親ノードの添字を取得
    private long DeriveParentNode(long pTarget)
    {
        return (pTarget - 1) / 2;
    }

    // 子ノードの添字(小さいほう)を取得
    private long DeriveChildNode(long pTarget)
    {
        return pTarget * 2 + 1;
    }

    // 葉ノードの配列の添字を木の添字に変換して返す
    private long DeriveTreeNode(long pLeafArrInd)
    {
        long BaseInd = UB - mLeafCnt + 1;
        return BaseInd + pLeafArrInd;
    }

    // 葉ノードの配列のK番目の値をNewValに変更
    internal void Update(long pK, long pNewVal)
    {
        long CurrNode = DeriveTreeNode(pK);
        mTreeNodeArr[CurrNode] = pNewVal;

        // 登りながら更新
        while (CurrNode > 0) {
            CurrNode = DeriveParentNode(CurrNode);
            long ChildNode1 = DeriveChildNode(CurrNode);
            long ChildNode2 = ChildNode1 + 1;
            mTreeNodeArr[CurrNode] =
                Math.Min(mTreeNodeArr[ChildNode1], mTreeNodeArr[ChildNode2]);
        }
    }

    // 開始添字と終了添字とカレントノードを引数として、最小値を返す
    internal long Query(long pSearchStaInd, long pSearchEndInd, long pCurrNode)
    {
        long CurrNodeStaInd = mRangeInfo[pCurrNode].StaInd;
        long CurrNodeEndInd = mRangeInfo[pCurrNode].EndInd;

        // OverLapしてなければ、long.MaxValue
        if (CurrNodeEndInd < pSearchStaInd || pSearchEndInd < CurrNodeStaInd)
            return long.MaxValue;

        // 完全に含んでいれば、このノードの値
        if (pSearchStaInd <= CurrNodeStaInd && CurrNodeEndInd <= pSearchEndInd)
            return mTreeNodeArr[pCurrNode];

        // そうでなければ、2つの子の最小値
        long ChildNode1 = DeriveChildNode(pCurrNode);
        long ChildNode2 = ChildNode1 + 1;

        long ChildVal1 = Query(pSearchStaInd, pSearchEndInd, ChildNode1);
        long ChildVal2 = Query(pSearchStaInd, pSearchEndInd, ChildNode2);
        return Math.Min(ChildVal1, ChildVal2);
    }

    internal void DebugPrint()
    {
        for (long I = 0; I <= UB; I++) {
            Console.WriteLine("mTreeNodeArr[{0}] = {1}", I, mTreeNodeArr[I]);
        }
    }
}
#endregion


解説

RMQのセグメント木で
値*100000000 + 添字
をlong型でパックした値を管理してます。