結果

問題 No.755 Zero-Sum Rectangle
ユーザー t98slidert98slider
提出日時 2023-06-21 20:29:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 3,219 bytes
コンパイル時間 2,417 ms
コンパイル使用メモリ 179,344 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-11 15:39:38
合計ジャッジ時間 3,770 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 302 ms
4,376 KB
testcase_02 AC 70 ms
4,380 KB
testcase_03 AC 64 ms
4,380 KB
testcase_04 AC 58 ms
4,376 KB
testcase_05 AC 49 ms
4,376 KB
testcase_06 AC 45 ms
4,376 KB
testcase_07 AC 42 ms
4,380 KB
testcase_08 AC 41 ms
4,376 KB
testcase_09 AC 44 ms
4,380 KB
testcase_10 AC 41 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
evil_1 AC 70 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

template <class T> struct CumulativeSum2D{
    int h, w;
    std::vector<std::vector<T>> dat;
    CumulativeSum2D(int H, int W) : h(H), w(W), dat(H + 1, std::vector<T>(W + 1, 0)) {}
    CumulativeSum2D(std::vector<std::vector<T>> &A) 
        : h(A.size()), w(A[0].size()), dat(h + 1, std::vector<T>(w + 1, 0)) {
        for(int y = 1; y <= h; y++){
            for(int x = 1; x <= w; x++){
                dat[y][x] = A[y - 1][x - 1] + dat[y][x - 1] + dat[y - 1][x] - dat[y - 1][x - 1];
            }
        }
    }
    void add(int y, int x, T z){
        assert(0 <= y && y < h);
        assert(0 <= x && x < w);
        dat[y + 1][x + 1] += z;
    }
    void build(){
        for(int y = 1; y <= h; y++) {
            for(int x = 1; x <= w; x++) {
                dat[y][x] += dat[y][x - 1] + dat[y - 1][x] - dat[y - 1][x - 1];
            }
        }
    }
    T query(int ly, int lx, int ry, int rx){
        assert(0 <= ly && ly <= ry && ry <= h);
        assert(0 <= lx && lx <= rx && rx <= h);
        return dat[ry][rx] - dat[ly][rx] - dat[ry][lx] + dat[ly][lx];
    }
};

template <class T> struct imos2D{
    int h, w;
    std::vector<std::vector<T>> dat;
    imos2D(int H, int W) : h(H), w(W), dat(H + 1, std::vector<T>(W + 1, 0)) {}
    void add(int ly, int lx, int ry, int rx, T v){
        assert(0 <= ly && ly <= ry && ry <= h);
        assert(0 <= lx && lx <= rx && rx <= w);
        dat[ry][rx] += v;
        dat[ly][rx] -= v;
        dat[ry][lx] -= v;
        dat[ly][lx] += v;
    }
    void build(){
        for(int i = 0; i <= h; i++) {
            for(int j = 1; j <= w; j++) {
                dat[i][j] += dat[i][j - 1];
            }
        }
        for(int i = 0; i <= w; i++) {
            for(int j = 1; j <= h; j++) {
                dat[j][i] += dat[j - 1][i];
            }
        }
    }
    const std::vector<T>& operator[](int y) const {
        assert(0 <= y && y < h);
        return dat[y];
    }
    std::vector<T>& operator[](int y) { 
        assert(0 <= y && y < h);
        return dat[y];
    }
};

using namespace std;
using ll = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<vector<ll>> A(m, vector<ll>(m));
    for(int y = 0; y < m; y++){
        for(int x = 0; x < m; x++){
            cin >> A[y][x];
        }
    }
    
    CumulativeSum2D<ll> CS(A);
    imos2D<int> imos(m, m);

    for(int ly = 0; ly < m; ly++){
        for(int ry = ly + 1; ry <= m; ry++){
            vector<pair<ll, int>> tmp(m + 1);
            for(int rx = 0; rx <= m; rx++){
                tmp[rx] = {CS.query(ly, 0, ry, rx), rx};
            }
            sort(tmp.begin(), tmp.end());
            for(int x = 0; x < m; ){
                int pre = x;
                while(x <= m && tmp[x].first == tmp[pre].first) x++;
                for(int l = pre; l < x; l++){
                    for(int r = l + 1; r < x; r++){
                        imos.add(ly, tmp[l].second, ry, tmp[r].second, 1);
                    }
                }
            }
        }
    }
    imos.build();
    while(n--){
        int y, x;
        cin >> y >> x;
        cout << imos[y - 1][x - 1] << '\n';
    }
}
0