結果
問題 | No.440 2次元チワワ問題 |
ユーザー |
|
提出日時 | 2017-02-08 03:19:18 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 4,402 ms / 5,000 ms |
コード長 | 2,538 bytes |
コンパイル時間 | 1,346 ms |
コンパイル使用メモリ | 93,420 KB |
実行使用メモリ | 28,160 KB |
最終ジャッジ日時 | 2024-12-25 15:39:13 |
合計ジャッジ時間 | 34,235 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
#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 monoidint n;vector<T> a;function<T (T,T)> append; // associativeT unit; // unitsegment_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;}