AtCoderのARC    前のARCの問題へ

ARC205-A 2x2 Erasing


問題へのリンク


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 4");
            WillReturn.Add("..#.#");
            WillReturn.Add(".....");
            WillReturn.Add("#....");
            WillReturn.Add("...#.");
            WillReturn.Add(".#...");
            WillReturn.Add("1 3 1 3");
            WillReturn.Add("3 5 3 5");
            WillReturn.Add("2 3 1 4");
            WillReturn.Add("1 5 1 5");
            //2
            //0
            //2
            //5
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("7 6");
            WillReturn.Add("#.#....");
            WillReturn.Add(".....#.");
            WillReturn.Add(".......");
            WillReturn.Add(".#..#.#");
            WillReturn.Add("#....#.");
            WillReturn.Add(".......");
            WillReturn.Add("....##.");
            WillReturn.Add("1 3 2 7");
            WillReturn.Add("4 6 1 6");
            WillReturn.Add("6 7 2 7");
            WillReturn.Add("3 5 4 6");
            WillReturn.Add("1 6 2 4");
            WillReturn.Add("1 7 1 7");
            //4
            //4
            //2
            //0
            //6
            //13
        }
        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 long mN;

    static char[,] mBanArr;
    static long UB;

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

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

        SplitAct(InputList[0]);
        mN = wkArr[0];

        mBanArr = CreateBanArr(InputList.Skip(1).Take((int)mN));
        UB = mBanArr.GetUpperBound(0);

        long[,] RunSumArr = new long[UB + 1, UB + 1];
        for (int LoopX = 0; LoopX <= UB; LoopX++) {
            for (int LoopY = 0; LoopY <= UB; LoopY++) {
                if (Is_2_2_LeftUp(LoopX, LoopY)) {
                    RunSumArr[LoopX, LoopY] = 1;
                }
            }
        }

        // 累積和を設定する (横方向)
        for (int LoopX = 1; LoopX <= UB; LoopX++) {
            for (int LoopY = 0; LoopY <= UB; LoopY++) {
                RunSumArr[LoopX, LoopY] += RunSumArr[LoopX - 1, LoopY];
            }
        }

        // 累積和を設定する (縦方向)
        for (int LoopX = 0; LoopX <= UB; LoopX++) {
            for (int LoopY = 1; LoopY <= UB; LoopY++) {
                RunSumArr[LoopX, LoopY] += RunSumArr[LoopX, LoopY - 1];
            }
        }

        foreach (string EachStr in InputList.Skip(1 + (int)mN)) {
            SplitAct(EachStr);
            long U = wkArr[0] - 1;
            long D = wkArr[1] - 1;
            long L = wkArr[2] - 1;
            long R = wkArr[3] - 1;

            long StaX = L;
            long StaY = U;
            long EndX = R - 1;
            long EndY = D - 1;

            long CurrSum = DeriveSumRect(RunSumArr, EndX, EndY);
            CurrSum -= DeriveSumRect(RunSumArr, StaX - 1, EndY);
            CurrSum -= DeriveSumRect(RunSumArr, EndX, StaY - 1);
            CurrSum += DeriveSumRect(RunSumArr, StaX - 1, StaY - 1);
            Console.WriteLine(CurrSum);
        }
    }

    // 2*2の左上かを返す
    static bool Is_2_2_LeftUp(long pBaseX, long pBaseY)
    {
        Func<long, long, bool> IsOKFunc = (pX, pY) =>
        {
            if (pX < 0 || UB < pX) return false;
            if (pY < 0 || UB < pY) return false;

            return mBanArr[pX, pY] == '.';
        };

        if (IsOKFunc(pBaseX + 0, pBaseY + 0) == false) return false;
        if (IsOKFunc(pBaseX + 1, pBaseY + 0) == false) return false;
        if (IsOKFunc(pBaseX + 0, pBaseY + 1) == false) return false;
        if (IsOKFunc(pBaseX + 1, pBaseY + 1) == false) return false;
        return true;
    }

    // (0,0)と(pX,pY)からなる長方形の数の和を返す
    static long DeriveSumRect(long[,] pRunSumArr, long pX, long pY)
    {
        if (pX < 0) return 0;
        if (pY < 0) return 0;
        return pRunSumArr[pX, pY];
    }

    ////////////////////////////////////////////////////////////////
    // IEnumerable<string>をcharの2次元配列に設定
    ////////////////////////////////////////////////////////////////
    static char[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new char[0, 0];
        }
        int UB_X = StrList[0].Length - 1;
        int UB_Y = StrList.Count - 1;

        char[,] WillReturn = new char[UB_X + 1, UB_Y + 1];

        for (int Y = 0; Y <= UB_Y; Y++) {
            for (int X = 0; X <= UB_X; X++) {
                WillReturn[X, Y] = StrList[Y][X];
            }
        }
        return WillReturn;
    }
}


解説

考察すると2*2の白マスの、左上を1として、
二次元累積和を事前に求めておけば良いと分かります。