競技プログラミングの鉄則    次の問題へ    前の問題へ

A08 Two Dimensional Sum


問題へのリンク


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

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

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

        SplitAct(InputList[0]);
        long H = wkArr[0];

        long[,] RunSumArr = CreateBanArr(InputList.Skip(1).Take((int)H));
        long UB_X = RunSumArr.GetUpperBound(0);
        long UB_Y = RunSumArr.GetUpperBound(1);

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

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

        foreach (string EachStr in InputList.Skip(1 + (int)H + 1)) {
            SplitAct(EachStr);
            long A = wkArr[0] - 1;
            long B = wkArr[1] - 1;
            long C = wkArr[2] - 1;
            long D = wkArr[3] - 1;

            long StaX = B;
            long StaY = A;
            long EndX = D;
            long EndY = C;

            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);
        }
    }

    // (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>をintの2次元配列に設定する
    ////////////////////////////////////////////////////////////////
    static long[,] CreateBanArr(IEnumerable<string> pStrEnum)
    {
        var StrList = pStrEnum.ToList();
        if (StrList.Count == 0) {
            return new long[0, 0];
        }

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

        SplitAct(StrList[0]);

        long UB_X = IntArr.GetUpperBound(0);
        long UB_Y = StrList.Count - 1;

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

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


解説

二次元累積和を使ってます。