結果

問題 No.755 Zero-Sum Rectangle
ユーザー m_tsubasam_tsubasa
提出日時 2018-12-03 15:16:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 175 ms / 2,000 ms
コード長 1,294 bytes
コンパイル時間 1,495 ms
コンパイル使用メモリ 166,088 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 21:08:53
合計ジャッジ時間 3,791 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 175 ms
4,376 KB
testcase_02 AC 141 ms
4,376 KB
testcase_03 AC 134 ms
4,376 KB
testcase_04 AC 131 ms
4,376 KB
testcase_05 AC 129 ms
4,380 KB
testcase_06 AC 128 ms
4,376 KB
testcase_07 AC 132 ms
4,380 KB
testcase_08 AC 132 ms
4,376 KB
testcase_09 AC 132 ms
4,380 KB
testcase_10 AC 132 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
evil_1 AC 144 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

long long n, m;
long long a[150][150] = {0};
long long sum[150][150] = {0};

void solve();
bool ch(int u, int d, int l, int r);

int main() {
  cin >> n >> m;
  for(int i = 0; i < m; ++i)
    for(int j = 0; j < m; ++j) {
      cin >> a[i][j];
      if(j != 0) a[i][j] += a[i][j - 1];
      if(i != 0) a[i][j] += a[i - 1][j];
      if(i != 0 && j != 0) a[i][j] -= a[i - 1][j - 1];
    }
  solve();
  for(int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    cout << sum[--x][--y] << endl;
  }
  return 0;
}

void solve() {
  for(int u = 0; u < m; ++u)
    for(int d = u; d < m; ++d)
      for(int l = 0; l < m; ++l)
        for(int r = l; r < m; ++r) {
          if(ch(u, d, l, r)) {
            ++sum[u][l];
            ++sum[d + 1][r + 1];
            --sum[u][r + 1];
            --sum[d + 1][l];
          }
        }
  for(int i = 0; i < m; ++i)
    for(int j = 0; j < m; ++j) {
      if(j != 0) sum[i][j] += sum[i][j - 1];
      if(i != 0) sum[i][j] += sum[i - 1][j];
      if(i != 0 && j != 0) sum[i][j] -= sum[i - 1][j - 1];
    }
}

bool ch(int u, int d, int l, int r) {
  long long now = a[d][r];
  if(l != 0) now -= a[d][l - 1];
  if(u != 0) now -= a[u - 1][r];
  if(l != 0 && u != 0) now += a[u - 1][l - 1];
  return now == 0;
}
0