結果

問題 No.440 2次元チワワ問題
ユーザー 古寺いろは古寺いろは
提出日時 2016-10-28 23:32:13
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,314 ms / 5,000 ms
コード長 4,216 bytes
コンパイル時間 1,053 ms
コンパイル使用メモリ 113,644 KB
実行使用メモリ 49,084 KB
最終ジャッジ日時 2024-05-03 08:12:07
合計ジャッジ時間 14,191 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
17,792 KB
testcase_01 AC 25 ms
18,048 KB
testcase_02 AC 26 ms
18,176 KB
testcase_03 AC 25 ms
17,920 KB
testcase_04 AC 27 ms
18,164 KB
testcase_05 AC 26 ms
17,792 KB
testcase_06 AC 26 ms
18,048 KB
testcase_07 AC 74 ms
22,272 KB
testcase_08 AC 190 ms
24,320 KB
testcase_09 AC 418 ms
34,304 KB
testcase_10 AC 67 ms
22,016 KB
testcase_11 AC 93 ms
20,096 KB
testcase_12 AC 100 ms
26,368 KB
testcase_13 AC 464 ms
33,236 KB
testcase_14 AC 39 ms
18,304 KB
testcase_15 AC 426 ms
35,304 KB
testcase_16 AC 86 ms
20,388 KB
testcase_17 AC 812 ms
49,084 KB
testcase_18 AC 821 ms
48,700 KB
testcase_19 AC 805 ms
48,584 KB
testcase_20 AC 813 ms
48,960 KB
testcase_21 AC 1,276 ms
48,756 KB
testcase_22 AC 1,261 ms
48,612 KB
testcase_23 AC 1,301 ms
48,748 KB
testcase_24 AC 1,314 ms
48,748 KB
testcase_25 AC 1,281 ms
48,620 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;

class Iroha
{
    public Iroha() { }
    public static int Main()
    {
        new Iroha().calc();
        return 0;
    }

    Scanner cin;

    int H, W;
    string[] S;
    int[,] board;

    void calc()
    {
        cin = new Scanner();
        H = cin.nextInt();
        W = cin.nextInt();
        S = new string[H];
        board = new int[H, W];
        for (int i = 0; i < H; i++)
        {
            S[i] = cin.next();
            for (int j = 0; j < W; j++)
            {
                if (S[i][j] == 'w') board[i, j] = 1;
            }
        }

        int Q = cin.nextInt();
        int[] a = new int[Q];
        int[] b = new int[Q];
        int[] c = new int[Q];
        int[] d = new int[Q];
        for (int i = 0; i < Q; i++)
        {
            a[i] = cin.nextInt() - 1;
            b[i] = cin.nextInt() - 1;
            c[i] = cin.nextInt();
            d[i] = cin.nextInt();
        }

        precalc();
        long[] ans = new long[Q];
        for (int i = 0; i < Q; i++)
        {
            ans[i] += calc(a[i], b[i], c[i], d[i]);
        }

        int[,] board2 = new int[W, H];
        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                board2[j, i] = board[i, j];
            }
        }
        board = board2;
        swap(ref H, ref W);

        precalc();
        for (int i = 0; i < Q; i++)
        {
            ans[i] += calc(b[i], a[i], d[i], c[i]);
            Console.WriteLine(ans[i]);
        }
    }
    
    long[,] dpc;
    long[,] dpcw;
    long[,] dpcww;
    long[,] dpw;
    long[,] dpww;
    long[,] dpwwc;
    long[,] dpwc;

    void precalc()
    {
        dpc = new long[H, W + 1];
        dpcw = new long[H, W + 1];
        dpcww = new long[H, W + 1];
        dpw = new long[H, W + 1];
        dpww = new long[H, W + 1];
        dpwwc = new long[H, W + 1];
        dpwc = new long[H, W + 1];

        for (int i = 0; i < H; i++)
        {
            for (int j = 0; j < W; j++)
            {
                dpc[i, j + 1] = dpc[i, j];
                dpcw[i, j + 1] = dpcw[i, j];
                dpcww[i, j + 1] = dpcww[i, j];
                dpw[i, j + 1] = dpw[i, j];
                dpww[i, j + 1] = dpww[i, j];
                dpwwc[i, j + 1] = dpwwc[i, j];
                dpwc[i, j + 1] = dpwc[i, j];

                //c
                if (board[i,j] == 0)
                {
                    dpc[i, j + 1]++;
                    dpwwc[i, j + 1] += dpww[i, j];
                    dpwc[i, j + 1] += dpw[i, j];
                }
                //w
                else
                {
                    dpcw[i, j + 1] += dpc[i, j];
                    dpcww[i, j + 1] += dpcw[i, j];
                    dpw[i, j + 1]++;
                    dpww[i, j + 1] += dpw[i, j];
                }
            }
        }
    }

    long calc(int a, int b, int c, int d)
    {
        long ans = 0;
        for (int h = a; h < c; h++)
        {
            ans += dpwwc[h, d] - dpwwc[h, b] - dpww[h, b] * (dpc[h, d] - dpc[h, b]) - dpw[h, b] * (dpwc[h, d] - dpwc[h, b] - (dpw[h, b] * (dpc[h, d] - dpc[h, b])));
            ans += dpcww[h, d] - dpcww[h, b] - dpcw[h, b] * (dpw[h, d] - dpw[h, b]) - dpc[h, b] * (dpww[h, d] - dpww[h, b] - (dpw[h, b] * (dpw[h, d] - dpw[h, b])));
        }
        return ans;
    }

    //swap
    void swap<T>(ref T a, ref T b)
    {
        T c = a;
        a = b;
        b = c;
    } 

}


class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        i = 0;
        return next();
    }

    public int nextInt()
    {
        return int.Parse(next());
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }

}
0