結果

問題 No.440 2次元チワワ問題
ユーザー tossytossy
提出日時 2016-10-29 17:08:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 913 ms / 5,000 ms
コード長 2,056 bytes
コンパイル時間 940 ms
コンパイル使用メモリ 99,184 KB
実行使用メモリ 492,792 KB
最終ジャッジ日時 2024-05-03 15:53:05
合計ジャッジ時間 10,434 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,764 KB
testcase_01 AC 2 ms
11,984 KB
testcase_02 AC 2 ms
11,996 KB
testcase_03 AC 2 ms
9,944 KB
testcase_04 AC 18 ms
83,988 KB
testcase_05 AC 4 ms
15,212 KB
testcase_06 AC 7 ms
36,560 KB
testcase_07 AC 98 ms
172,880 KB
testcase_08 AC 144 ms
186,408 KB
testcase_09 AC 474 ms
349,964 KB
testcase_10 AC 139 ms
189,056 KB
testcase_11 AC 34 ms
55,656 KB
testcase_12 AC 176 ms
211,680 KB
testcase_13 AC 510 ms
372,540 KB
testcase_14 AC 7 ms
17,768 KB
testcase_15 AC 566 ms
396,316 KB
testcase_16 AC 8 ms
20,536 KB
testcase_17 AC 913 ms
492,616 KB
testcase_18 AC 642 ms
492,752 KB
testcase_19 AC 510 ms
492,700 KB
testcase_20 AC 877 ms
492,684 KB
testcase_21 AC 834 ms
492,792 KB
testcase_22 AC 475 ms
492,752 KB
testcase_23 AC 607 ms
492,740 KB
testcase_24 AC 876 ms
492,716 KB
testcase_25 AC 837 ms
492,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<(x)<<endl
#define fi first
#define se second

#define INF 2147483600

int dp[500][500][500];

int main(){
  int h,w;
  cin>>h>>w;
  vector<string> vec(h);
  rep(i,h) cin>>vec[i];

  int q;
  cin>>q;
  vector<int> a(q), b(q), c(q), d(q);
  vector<long> res(q,0);
  rep(i,q){
    scanf("%d %d %d %d", &a[i], &b[i], &c[i], &d[i]);
    a[i]--;b[i]--;c[i]--;d[i]--;
  }

  rep(i,h){
    rep(l,w){
      int cnt= (vec[i][l]=='w');
      dp[i][l][l]=0;
      repl(r,l+1,w){
        dp[i][l][r] = dp[i][l][r-1];
        if(vec[i][r]=='c') dp[i][l][r] += cnt*(cnt-1)/2;
        else cnt++;
      }
    }
    for(int r=w-1; r>=0; r--){
      int cnt= (vec[i][r]=='w');
      int prev=0;
      for(int l=r-1; l>=0; l--){
        if(vec[i][l]=='c') prev += cnt*(cnt-1)/2;
        else cnt++;
        dp[i][l][r] += prev;
//        cout<<i<<","<<l<<","<<r<<":"<<dp[i][l][r]<<endl;
      }
    }
  }

  rep(k,q){
    for(int i=a[k]; i<=c[k]; i++){
      res[k] += (long)dp[i][b[k]][d[k]];
    }
  }

  rep(i,w){
    rep(l,h){
      int cnt= (vec[l][i]=='w');
      repl(r,l+1,h){
        dp[i][l][r] = dp[i][l][r-1];
        if(vec[r][i]=='c') dp[i][l][r] += cnt*(cnt-1)/2;
        else cnt++;
      }
    }
    for(int r=h-1; r>=0; r--){
      int cnt= (vec[r][i]=='w');
      int prev=0;
      for(int l=r-1; l>=0; l--){
        if(vec[l][i]=='c') prev += cnt*(cnt-1)/2;
        else cnt++;
        dp[i][l][r] += prev;
      }
    }
  }

  rep(k,q){
    for(int i=b[k]; i<=d[k]; i++){
      res[k] += (long)dp[i][a[k]][c[k]];
    }
    printf("%ld\n", res[k]);
  }

  return 0;
}
0