結果

問題 No.440 2次元チワワ問題
ユーザー kimiyukikimiyuki
提出日時 2017-02-08 03:19:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3,058 ms / 5,000 ms
コード長 2,538 bytes
コンパイル時間 975 ms
コンパイル使用メモリ 92,808 KB
実行使用メモリ 28,180 KB
最終ジャッジ日時 2023-08-26 16:42:15
合計ジャッジ時間 22,517 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 76 ms
8,164 KB
testcase_08 AC 241 ms
8,208 KB
testcase_09 AC 535 ms
15,584 KB
testcase_10 AC 61 ms
8,024 KB
testcase_11 AC 80 ms
4,860 KB
testcase_12 AC 111 ms
16,400 KB
testcase_13 AC 605 ms
23,236 KB
testcase_14 AC 9 ms
4,376 KB
testcase_15 AC 562 ms
24,020 KB
testcase_16 AC 28 ms
4,380 KB
testcase_17 AC 939 ms
27,984 KB
testcase_18 AC 939 ms
28,020 KB
testcase_19 AC 954 ms
28,084 KB
testcase_20 AC 945 ms
27,996 KB
testcase_21 AC 3,011 ms
27,980 KB
testcase_22 AC 3,058 ms
28,180 KB
testcase_23 AC 3,039 ms
28,024 KB
testcase_24 AC 3,048 ms
28,000 KB
testcase_25 AC 1,533 ms
27,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>
#include <cmath>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < int(n); ++(i))
using ll = long long;
using namespace std;

template <typename T>
struct segment_tree { // on monoid
    int n;
    vector<T> a;
    function<T (T,T)> append; // associative
    T unit; // unit
    segment_tree() = default;
    segment_tree(int a_n, T a_unit, function<T (T,T)> a_append) {
        n = pow(2,ceil(log2(a_n)));
        a.resize(2*n-1, a_unit);
        unit = a_unit;
        append = a_append;
    }
    void point_update(int i, T z) {
        a[i+n-1] = z;
        for (i = (i+n)/2; i > 0; i /= 2) {
            a[i-1] = append(a[2*i-1], a[2*i]);
        }
    }
    T range_concat(int l, int r) {
        return range_concat(0, 0, n, l, r);
    }
    T range_concat(int i, int il, int ir, int l, int r) {
        if (l <= il and ir <= r) {
            return a[i];
        } else if (ir <= l or r <= il) {
            return unit;
        } else {
            return append(
                    range_concat(2*i+1, il, (il+ir)/2, l, r),
                    range_concat(2*i+2, (il+ir)/2, ir, l, r));
        }
    }
};
int nc2(int n) { return n*(n-1)/2; }

struct cww_t {
    int c, w, cw, wc, cww, wwc;
};
const cww_t unit = {};
cww_t append(cww_t const & x, cww_t const & y) {
    cww_t z;
    z.c = x.c + y.c;
    z.w = x.w + y.w;
    z.cw = x.cw + x.c * y.w + y.cw;
    z.wc = x.wc + x.w * y.c + y.wc;
    z.cww = x.cww + x.cw * y.w + x.c * nc2(y.w) + y.cww;
    z.wwc = x.wwc + nc2(x.w) * y.c + x.w * y.wc + y.wwc;
    return z;
}
int main() {
    int h, w; cin >> h >> w;
    vector<string> f(h); repeat (y,h) cin >> f[y];
    vector<segment_tree<cww_t> > segtree_hr(h, segment_tree<cww_t>(w, unit, append));
    vector<segment_tree<cww_t> > segtree_vt(w, segment_tree<cww_t>(h, unit, append));
    repeat (y,h) repeat (x,w) segtree_hr[y].point_update(x, (cww_t) { (f[y][x] == 'c'), (f[y][x] == 'w'), 0, 0 });
    repeat (x,w) repeat (y,h) segtree_vt[x].point_update(y, (cww_t) { (f[y][x] == 'c'), (f[y][x] == 'w'), 0, 0 });
    int q; cin >> q;
    while (q --) {
        int ly, lx, ry, rx; cin >> ly >> lx >> ry >> rx; -- ly; -- lx;
        ll ans = 0;
        repeat_from (y,ly,ry) { auto it = segtree_hr[y].range_concat(lx, rx); ans += it.cww + it.wwc; }
        repeat_from (x,lx,rx) { auto it = segtree_vt[x].range_concat(ly, ry); ans += it.cww + it.wwc; }
        cout << ans << endl;
    }
    return 0;
}
0