結果

問題 No.755 Zero-Sum Rectangle
ユーザー simansiman
提出日時 2022-06-28 13:19:13
言語 C++17(clang)
(14.0.0 + boost 1.83.0)
結果
AC  
実行時間 227 ms / 2,000 ms
コード長 1,640 bytes
コンパイル時間 1,329 ms
コンパイル使用メモリ 105,900 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-13 11:00:27
合計ジャッジ時間 6,916 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 160 ms
4,380 KB
testcase_02 AC 124 ms
4,380 KB
testcase_03 AC 172 ms
4,384 KB
testcase_04 AC 195 ms
4,376 KB
testcase_05 AC 213 ms
4,380 KB
testcase_06 AC 139 ms
4,376 KB
testcase_07 AC 130 ms
4,376 KB
testcase_08 AC 177 ms
4,380 KB
testcase_09 AC 198 ms
4,380 KB
testcase_10 AC 227 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
evil_1 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;
  ll A[M][M];

  for (int i = 0; i < M; ++i) {
    for (int j = 0; j < M; ++j) {
      cin >> A[i][j];
    }
  }
  ll S[M + 1][M + 1];
  memset(S, 0, sizeof(S));
  for (int y = 1; y <= M; ++y) {
    for (int x = 1; x <= M; ++x) {
      S[y][x] = S[y][x - 1] + A[y - 1][x - 1];
    }
  }
  for (int x = 0; x <= M; ++x) {
    for (int y = 1; y <= M; ++y) {
      S[y][x] += S[y - 1][x];
    }
  }

  /*
  for (int y = 0; y <= M; ++y) {
    for (int x = 0; x <= M; ++x) {
      cerr << S[y][x] << " ";
    }
    cerr << endl;
  }
  */

  int x, y;
  for (int i = 0; i < N; ++i) {
    cin >> y >> x;
    int ans = 0;

    for (int cy = 1; cy <= y; ++cy) {
      for (int cx = 1; cx <= x; ++cx) {

        for (int ny = y; ny <= M; ++ny) {
          for (int nx = x; nx <= M; ++nx) {
            if (ny < cy) continue;
            if (nx < cx) continue;

            int ly = min(cy, ny);
            int lx = min(cx, nx);
            int ry = max(cy, ny);
            int rx = max(cx, nx);

            if (ly <= y && y <= ry && lx <= x && x <= rx) {
              ll sum = S[ry][rx] - S[ry][lx - 1] - S[ly - 1][rx] + S[ly - 1][lx - 1];
              // fprintf(stderr, "(%d, %d) -> (%d, %d) sum: %lld\n", lx, ly, rx, ry, sum);
              if (sum == 0) ++ans;
            }
          }
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}
0