結果

問題 No.440 2次元チワワ問題
ユーザー mamekinmamekin
提出日時 2016-10-29 16:35:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 368 ms / 5,000 ms
コード長 2,572 bytes
コンパイル時間 1,529 ms
コンパイル使用メモリ 113,584 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-16 06:45:51
合計ジャッジ時間 6,878 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 44 ms
4,380 KB
testcase_08 AC 110 ms
4,376 KB
testcase_09 AC 198 ms
4,380 KB
testcase_10 AC 38 ms
4,376 KB
testcase_11 AC 46 ms
4,380 KB
testcase_12 AC 64 ms
4,380 KB
testcase_13 AC 217 ms
4,380 KB
testcase_14 AC 7 ms
4,380 KB
testcase_15 AC 211 ms
4,380 KB
testcase_16 AC 24 ms
4,380 KB
testcase_17 AC 325 ms
4,384 KB
testcase_18 AC 312 ms
4,380 KB
testcase_19 AC 313 ms
4,380 KB
testcase_20 AC 314 ms
4,384 KB
testcase_21 AC 357 ms
4,380 KB
testcase_22 AC 358 ms
4,504 KB
testcase_23 AC 358 ms
4,376 KB
testcase_24 AC 368 ms
4,444 KB
testcase_25 AC 360 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

class CWW
{
private:
    int n;
    vector<vector<int> > front, back;
public:
    CWW(const string& s)
    {
        static const string cww = "cww";
        n = s.size();

        front.assign(n+1, vector<int>(4, 0));
        back.assign(n+1, vector<int>(4, 0));
        front[0][0] = 1;
        back[0][0] = 1;

        for(int i=0; i<n; ++i){
            front[i+1] = front[i];
            back[i+1] = back[i];
            for(int j=0; j<3; ++j){
                if(s[i] == cww[j])
                    front[i+1][j+1] += front[i][j];
                if(s[n-1-i] == cww[2-j])
                    back[i+1][j+1] += back[i][j];
            }
        }
    }
    int get(int a, int b)
    {
        int ans = front[n][3];
        for(int i=1; i<=3; ++i){
            ans -= front[a-1][i] * back[n+1-a][3-i];
            ans -= back[n-b][i] * front[b][3-i];
        }
        for(int i=1; i<=2; ++i){
            ans += front[a-1][i] * back[n-b][3-i];
        }
        ans += front[a-1][1] * (back[n+1-a][1] - back[n-b][1]) * back[n-b][1];
        return ans;
    }
};

int main()
{
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    for(int i=0; i<h; ++i)
        cin >> s[i];

    int q;
    cin >> q;
    vector<int> y1(q), x1(q), y2(q), x2(q);
    for(int i=0; i<q; ++i)
        cin >> y1[i] >> x1[i] >> y2[i] >> x2[i];

    vector<long long> ans(q, 0);
    for(int r=0; r<4; ++r){
        for(int y=1; y<=h; ++y){
            CWW cww(s[y-1]);
            for(int i=0; i<q; ++i){
                if(y1[i] <= y && y <= y2[i])
                    ans[i] += cww.get(x1[i], x2[i]);
            }
        }

        vector<string> t(w, string(h, ' '));
        for(int y=0; y<h; ++y){
            for(int x=0; x<w; ++x){
                t[x][h-1-y] = s[y][x];
            }
        }
        s.swap(t);
        for(int i=0; i<q; ++i){
            swap(y1[i], x1[i]);
            swap(y2[i], x2[i]);
            x1[i] = h + 1 - x1[i];
            x2[i] = h + 1 - x2[i];
            swap(x1[i], x2[i]);
        }
        swap(h, w);
    }

    for(int i=0; i<q; ++i)
        cout << ans[i] << endl;

    return 0;
}
0