結果

問題 No.440 2次元チワワ問題
ユーザー 古寺いろは古寺いろは
提出日時 2016-10-28 23:32:13
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,296 ms / 5,000 ms
コード長 4,216 bytes
コンパイル時間 2,899 ms
コンパイル使用メモリ 105,776 KB
実行使用メモリ 54,784 KB
最終ジャッジ日時 2023-08-15 21:41:39
合計ジャッジ時間 15,510 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
20,928 KB
testcase_01 AC 57 ms
20,756 KB
testcase_02 AC 58 ms
20,764 KB
testcase_03 AC 58 ms
20,788 KB
testcase_04 AC 60 ms
22,864 KB
testcase_05 AC 59 ms
20,748 KB
testcase_06 AC 59 ms
22,836 KB
testcase_07 AC 108 ms
24,584 KB
testcase_08 AC 221 ms
26,568 KB
testcase_09 AC 399 ms
36,204 KB
testcase_10 AC 99 ms
22,180 KB
testcase_11 AC 126 ms
21,436 KB
testcase_12 AC 131 ms
28,736 KB
testcase_13 AC 418 ms
35,780 KB
testcase_14 AC 71 ms
20,876 KB
testcase_15 AC 372 ms
40,356 KB
testcase_16 AC 111 ms
22,692 KB
testcase_17 AC 662 ms
52,648 KB
testcase_18 AC 618 ms
52,828 KB
testcase_19 AC 596 ms
52,744 KB
testcase_20 AC 617 ms
54,784 KB
testcase_21 AC 1,284 ms
54,652 KB
testcase_22 AC 1,296 ms
54,628 KB
testcase_23 AC 1,279 ms
54,664 KB
testcase_24 AC 1,286 ms
52,552 KB
testcase_25 AC 1,264 ms
54,624 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