結果

問題 No.440 2次元チワワ問題
ユーザー tossytossy
提出日時 2016-10-29 17:08:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 830 ms / 5,000 ms
コード長 2,056 bytes
コンパイル時間 1,075 ms
コンパイル使用メモリ 95,744 KB
実行使用メモリ 492,708 KB
最終ジャッジ日時 2023-08-16 06:48:00
合計ジャッジ時間 11,133 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,732 KB
testcase_01 AC 3 ms
9,740 KB
testcase_02 AC 3 ms
9,720 KB
testcase_03 AC 2 ms
7,576 KB
testcase_04 AC 20 ms
83,772 KB
testcase_05 AC 6 ms
15,036 KB
testcase_06 AC 9 ms
36,308 KB
testcase_07 AC 106 ms
171,584 KB
testcase_08 AC 133 ms
300,692 KB
testcase_09 AC 356 ms
378,268 KB
testcase_10 AC 120 ms
305,292 KB
testcase_11 AC 38 ms
55,316 KB
testcase_12 AC 155 ms
263,668 KB
testcase_13 AC 502 ms
393,612 KB
testcase_14 AC 8 ms
17,668 KB
testcase_15 AC 515 ms
417,892 KB
testcase_16 AC 9 ms
20,352 KB
testcase_17 AC 811 ms
492,488 KB
testcase_18 AC 830 ms
492,488 KB
testcase_19 AC 655 ms
492,708 KB
testcase_20 AC 744 ms
492,576 KB
testcase_21 AC 701 ms
492,572 KB
testcase_22 AC 617 ms
492,512 KB
testcase_23 AC 786 ms
492,516 KB
testcase_24 AC 768 ms
492,608 KB
testcase_25 AC 691 ms
492,536 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