結果

問題 No.755 Zero-Sum Rectangle
ユーザー outlineoutline
提出日時 2020-12-21 23:08:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 133 ms / 2,000 ms
コード長 2,340 bytes
コンパイル時間 1,507 ms
コンパイル使用メモリ 135,860 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 11:56:36
合計ジャッジ時間 6,402 ms
ジャッジサーバーID
(参考情報)
judge9 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 91 ms
4,348 KB
testcase_02 AC 75 ms
4,348 KB
testcase_03 AC 97 ms
4,348 KB
testcase_04 AC 114 ms
4,348 KB
testcase_05 AC 127 ms
4,348 KB
testcase_06 AC 83 ms
4,348 KB
testcase_07 AC 74 ms
4,348 KB
testcase_08 AC 106 ms
4,348 KB
testcase_09 AC 115 ms
4,348 KB
testcase_10 AC 133 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
evil_1 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
using namespace std;

using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template<typename T>
struct CumulativeSum2D{
    vector<vector<T>> data;

    CumulativeSum2D(int W, int H) : data(W + 1, vector<T>(H + 1, 0)) {}

    void add(int x, int y, T z){
        ++x, ++y;
        if(x >= data.size() || y >= data[0].size()) return;
        data[x][y] += z;
    }

    void build(){
        for(int i = 1; i < (int)data.size(); ++i){
            for(int j = 1; j < (int)data[i].size(); ++j){
                data[i][j] += data[i][j - 1] + data[i - 1][j] - data[i - 1][j - 1];
            }
        }
    }

    T query(int sx, int sy, int gx, int gy){
        return (data[gx][gy] - data[sx][gy] - data[gx][sy] + data[sx][sy]);
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, M;
    cin >> N >> M;
    CumulativeSum2D<ll> S(M, M);
    for(int i = 0; i < M; ++i){
        for(int j = 0; j < M; ++j){
            ll A;
            cin >> A;
            S.add(i, j, A);
        }
    }
    S.build();
    
    for(int i = 0; i < N; ++i){
        int X, Y, ans = 0;
        cin >> X >> Y;
        --X, --Y;
        for(int sx = 0; sx <= X; ++sx){
            for(int sy = 0; sy <= Y; ++sy){
                for(int gx = X + 1; gx <= M; ++gx){
                    for(int gy = Y + 1; gy <= M; ++gy){
                        ans += S.query(sx, sy, gx, gy) == 0LL;
                    }
                }
            }
        }
        cout << ans << '\n';
    }

    return 0;
}
0