AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC457-E Crossing Table Cloth


問題へのリンク


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 3");
            WillReturn.Add("1 3");
            WillReturn.Add("1 1");
            WillReturn.Add("2 4");
            WillReturn.Add("4");
            WillReturn.Add("1 4");
            WillReturn.Add("2 4");
            WillReturn.Add("1 3");
            WillReturn.Add("1 1");
            //Yes
            //No
            //Yes
            //No
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("7 10");
            WillReturn.Add("2 6");
            WillReturn.Add("2 5");
            WillReturn.Add("3 6");
            WillReturn.Add("1 6");
            WillReturn.Add("1 2");
            WillReturn.Add("5 6");
            WillReturn.Add("2 3");
            WillReturn.Add("3 7");
            WillReturn.Add("2 3");
            WillReturn.Add("1 2");
            WillReturn.Add("10");
            WillReturn.Add("1 2");
            WillReturn.Add("3 5");
            WillReturn.Add("1 4");
            WillReturn.Add("1 5");
            WillReturn.Add("1 5");
            WillReturn.Add("5 7");
            WillReturn.Add("1 6");
            WillReturn.Add("2 3");
            WillReturn.Add("5 7");
            WillReturn.Add("2 4");
            //Yes
            //No
            //No
            //Yes
            //Yes
            //No
            //Yes
            //Yes
            //No
            //No
        }
        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();
    }

    struct ClothDef
    {
        internal long L;
        internal long R;
    }
    static List<ClothDef> mClothList = new List<ClothDef>();

    static void Main()
    {
        List<string> InputList = GetInputList();

        long[] wkArr = { };
        Action<string> SplitAct = (pStr) => wkArr = GetSplitArr(pStr);

        SplitAct(InputList[0]);
        long N = wkArr[0];
        long M = wkArr[1];

        foreach (string EachStr in InputList.Skip(1).Take((int)M)) {
            SplitAct(EachStr);
            ClothDef WillAdd;
            WillAdd.L = wkArr[0];
            WillAdd.R = wkArr[1];
            mClothList.Add(WillAdd);
        }

        var MinDict = new Dictionary<long, long>();
        foreach (ClothDef EachCloth in mClothList) {
            if (MinDict.ContainsKey(EachCloth.L) == false) {
                MinDict[EachCloth.L] = EachCloth.R;
            }
            MinDict[EachCloth.L] = Math.Min(MinDict[EachCloth.L], EachCloth.R);
        }

        // RのMin[L]な1点更新区間最小値のセグ木
        var InsSegmentTree = new SegmentTree(N, long.MaxValue);
        for (long I = 0; I <= N; I++) {
            if (MinDict.ContainsKey(I)) {
                InsSegmentTree[I] = MinDict[I];
            }
        }

        // 度数[区間のハッシュ値]
        var CntDict = new Dictionary<long, long>();
        foreach (ClothDef EachCloth in mClothList) {
            long CurrHash = GetHash(EachCloth.L, EachCloth.R);
            if (CntDict.ContainsKey(CurrHash) == false) {
                CntDict[CurrHash] = 0;
            }
            CntDict[CurrHash]++;
        }

        // RのList[L]なDict
        var RListDict = new Dictionary<long, List<long>>();

        // LのList[R]なDict
        var LListDict = new Dictionary<long, List<long>>();

        foreach (ClothDef EachCloth in mClothList) {
            long L = EachCloth.L;
            long R = EachCloth.R;

            if (RListDict.ContainsKey(L) == false) {
                RListDict[L] = new List<long>();
            }
            RListDict[L].Add(R);

            if (LListDict.ContainsKey(R) == false) {
                LListDict[R] = new List<long>();
            }
            LListDict[R].Add(L);
        }

        foreach (var EachPair in RListDict) {
            EachPair.Value.Sort();
        }
        foreach (var EachPair in LListDict) {
            EachPair.Value.Sort();
        }

        foreach (string EachStr in InputList.Skip(1 + (int)M + 1)) {
            SplitAct(EachStr);
            long QueryL = wkArr[0];
            long QueryR = wkArr[1];
            long QueryHash = GetHash(QueryL, QueryR);

            // 場合1 区間と完全一致な布が2枚以上ある
            if (CntDict.ContainsKey(QueryHash)) {
                if (CntDict[QueryHash] >= 2) {
                    Console.WriteLine("Yes");
                    continue;
                }
            }

            // 場合2 区間と完全一致な布が1枚だけある
            if (CntDict.ContainsKey(QueryHash)) {
                if (CntDict[QueryHash] == 1) {
                    if (MinDict[QueryL] <= QueryR - 1) {
                        Console.WriteLine("Yes");
                        continue;
                    }

                    if (QueryL + 1 <= QueryR) {
                        long RangeMin = InsSegmentTree.Internal_Query(QueryL + 1, QueryR);
                        if (RangeMin <= QueryR) {
                            Console.WriteLine("Yes");
                            continue;
                        }
                    }
                    Console.WriteLine("No");
                    continue;
                }
            }

            // 場合3 区間と完全一致な布が0枚
            if (CntDict.ContainsKey(QueryHash) == false) {
                if (RListDict.ContainsKey(QueryL) == false) {
                    Console.WriteLine("No");
                    continue;
                }
                if (LListDict.ContainsKey(QueryR) == false) {
                    Console.WriteLine("No");
                    continue;
                }

                int Result1 = ExecNibunhou_LowerOrEqual_Max(QueryR, RListDict[QueryL]);
                if (Result1 == -1) {
                    Console.WriteLine("No");
                    continue;
                }
                int Result2 = ExecNibunhou_LowerBound(QueryL, LListDict[QueryR]);
                if (Result2 == -1) {
                    Console.WriteLine("No");
                    continue;
                }

                long ResultR = RListDict[QueryL][Result1];
                long ResultL = LListDict[QueryR][Result2];

                // カバーしたマス目の数で判定
                long CoverCnt = 0;
                CoverCnt += Math.Abs(ResultR - QueryL) + 1;
                CoverCnt += Math.Abs(ResultL - QueryR) + 1;

                long NeedCover = QueryR - QueryL + 1;
                if (NeedCover <= CoverCnt) {
                    Console.WriteLine("Yes");
                    continue;
                }
                Console.WriteLine("No");
                continue;
            }
        }
    }

    static long GetHash(long pL, long pR)
    {
        return pL * 100000000 + pR;
    }

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

        // 最後の要素がVal未満の特殊ケース
        if (pVal > pList.Last()) {
            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 int ExecNibunhou_LowerOrEqual_Max(long pVal, List<long> pList)
    {
        if (pList.Count == 0) return -1;

        // 最後の要素がVal以下の特殊ケース
        if (pVal >= pList.Last()) {
            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;
    }
}

#region SegmentTree
// SegmentTreeクラス (RMQ and 1点更新)
internal class SegmentTree
{
    private long[] mTreeNodeArr;
    private long UB; // 木のノードの配列のUB
    private long mLeafCnt; // 葉ノードの数
    private long mExternalArrUB;

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

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

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

    // コンストラクタ
    internal SegmentTree(long pExternalArrUB, long pInitVal)
    {
        mExternalArrUB = pExternalArrUB;

        // 簡単のため、葉ノード数を2のべき乗に
        long ArrLength = 0;
        for (long I = 1; I < long.MaxValue; I *= 2) {
            ArrLength += I;
            mLeafCnt = I;

            if (pExternalArrUB + 1 < mLeafCnt) break;
        }

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

        // ノードの添字を引数とし、範囲の開始添字と終了添字を持つ配列の作成
        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;
        }
    }

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

    // 親ノードの添字を取得
    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 Internal_Query(long pSearchStaInd, long pSearchEndInd)
    {
        return Private_Query(pSearchStaInd, pSearchEndInd, 0);
    }
    private long Private_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 = Private_Query(pSearchStaInd, pSearchEndInd, ChildNode1);
        long ChildVal2 = Private_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


解説

場合に分けて考えます。

●場合1 クエリ区間と完全一致な布が2枚以上ある
区間のハッシュ値ごとの度数分布表で判定できます。

●場合2 クエリ区間と完全一致な布が1枚だけある
例えば、クエリ区間が[100,200]なら

RのMin[L]な、1点更新区間最小値のセグ木を用意しておき、
[100]の最小値が199以下ならOK
[101,200]の最小値が200以下ならOK
で2つのOKケースを調べれば判定できます。

●場合3 クエリ区間と完全一致な布が0枚
RのList[L]
LのList[R]
を用意しておき、二分探索で判定できます。