結果

問題 No.1141 田グリッド
ユーザー betrue12betrue12
提出日時 2020-09-01 02:06:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 451 ms / 2,000 ms
コード長 3,174 bytes
コンパイル時間 2,495 ms
コンパイル使用メモリ 212,832 KB
実行使用メモリ 16,512 KB
最終ジャッジ日時 2024-04-28 11:47:31
合計ジャッジ時間 14,090 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 6 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 7 ms
5,376 KB
testcase_11 AC 10 ms
5,376 KB
testcase_12 AC 6 ms
5,376 KB
testcase_13 AC 208 ms
6,272 KB
testcase_14 AC 297 ms
16,512 KB
testcase_15 AC 424 ms
11,776 KB
testcase_16 AC 436 ms
11,776 KB
testcase_17 AC 426 ms
7,680 KB
testcase_18 AC 403 ms
7,680 KB
testcase_19 AC 441 ms
11,648 KB
testcase_20 AC 430 ms
11,776 KB
testcase_21 AC 435 ms
11,648 KB
testcase_22 AC 431 ms
11,648 KB
testcase_23 AC 422 ms
11,776 KB
testcase_24 AC 409 ms
7,552 KB
testcase_25 AC 380 ms
11,648 KB
testcase_26 AC 409 ms
7,680 KB
testcase_27 AC 418 ms
7,552 KB
testcase_28 AC 378 ms
11,648 KB
testcase_29 AC 403 ms
7,424 KB
testcase_30 AC 392 ms
7,680 KB
testcase_31 AC 451 ms
11,520 KB
testcase_32 AC 413 ms
11,776 KB
testcase_33 AC 423 ms
11,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int64_t MOD = 1e9+7;
void add(int64_t& a, int64_t b){
    a = (a+b) % MOD;
}
void mul(int64_t& a, int64_t b){
    a = a*b % MOD;
}


template<typename T>
struct Segtree {
    int n, n_org;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(){}
    Segtree(int n_input, Func f_input, T e_input){
        initialize(n_input, f_input, e_input);
    }
    void initialize(int n_input, Func f_input, T e_input){
        n_org = n_input;
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.assign(2*n-1, e);
    }

    void build(vector<T>& A){
        for(int k=0; k<int(A.size()); k++) dat[k+n-1] = A[k];
        for(int k=n-2; k>=0; k--) dat[k] = f(dat[2*k+1], dat[2*k+2]);
    }

    void update(int k, T a){
        k += n - 1;
        mul(dat[k], a);
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }
};

template<typename T>
struct Segtree2D {
    int n, n_org;
    T e;
    vector<Segtree<T>> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree2D(){}
    Segtree2D(int n_input, int m_input, Func f_input, T e_input){
        initialize(n_input, m_input, f_input, e_input);
    }
    void initialize(int n_input, int m_input, Func f_input, T e_input){
        n_org = n_input;
        f = f_input;
        e = e_input;
        n = 1;
        while(n < n_input) n <<= 1;
        dat.resize(2*n-1);
        for(int i=0; i<2*n-1; i++) dat[i] = Segtree<T>(m_input, f_input, e_input);
    }

    void update(int k, int x, T a){
        k += n - 1;
        dat[k].update(x, a);
        while(k > 0){
            k = (k - 1)/2;
            dat[k].update(x, a);
        }
    }

    T between(int a, int b, int s, int t){
        return query(a, b+1, 0, 0, n, s, t);
    }

    T query(int a, int b, int k, int l, int r, int s, int t){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k].between(s, t);
        T vl = query(a, b, 2*k+1, l, (l+r)/2, s, t);
        T vr = query(a, b, 2*k+2, (l+r)/2, r, s, t);
        return f(vl, vr);
    }
};

int main(){
    int H, W;
    cin >> H >> W;

    Segtree2D<int64_t> st(H, W, [](auto a, auto b){ return a*b%MOD; }, 1);
    for(int i=0; i<H; i++) for(int j=0; j<W; j++){
        int a;
        cin >> a;
        st.update(i, j, a);
    }

    int Q;
    cin >> Q;
    while(Q--){
        int r, c;
        cin >> r >> c;
        r--; c--;
        int64_t ans = 1;
        mul(ans, st.between(0, r-1, 0, c-1));
        mul(ans, st.between(r+1, H-1, 0, c-1));
        mul(ans, st.between(0, r-1, c+1, W-1));
        mul(ans, st.between(r+1, H-1, c+1, W-1));
        cout << ans << endl;
    }
    return 0;
}
0