結果

問題 No.755 Zero-Sum Rectangle
ユーザー nasadigitalnasadigital
提出日時 2018-12-03 10:28:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 2,220 bytes
コンパイル時間 1,011 ms
コンパイル使用メモリ 110,704 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 21:06:46
合計ジャッジ時間 2,972 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 188 ms
4,376 KB
testcase_02 AC 116 ms
4,380 KB
testcase_03 AC 106 ms
4,376 KB
testcase_04 AC 101 ms
4,380 KB
testcase_05 AC 97 ms
4,380 KB
testcase_06 AC 95 ms
4,376 KB
testcase_07 AC 94 ms
4,376 KB
testcase_08 AC 94 ms
4,380 KB
testcase_09 AC 94 ms
4,376 KB
testcase_10 AC 95 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
evil_1 AC 114 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <istream>
#include <map>
#include <numeric>
#include <ostream>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>


using namespace std;

typedef long long ll;


class Solution {
 public:
  void solve(std::istream& in, std::ostream& out) {
    int n, m;
    in >> m >> n;
    ll mat[n][n];
    for (int ctr1 = 0; ctr1 < n; ++ctr1)
      for (int ctr2 = 0; ctr2 < n; ++ctr2)
        in >> mat[ctr1][ctr2];
    ll acc[n + 1][n + 1];
    memset(acc, 0, sizeof(acc));
    for (int ctr1 = 0; ctr1 < n; ++ctr1)
      for (int ctr2 = 0; ctr2 < n; ++ctr2)
        acc[ctr1 + 1][ctr2 + 1] = mat[ctr1][ctr2] + acc[ctr1 + 1][ctr2] +
            acc[ctr1][ctr2 + 1] - acc[ctr1][ctr2];
    ll dp[n + 1][n + 1];
    memset(dp, 0, sizeof(dp));
    for (int ctr1 = 0; ctr1 < n; ++ctr1)
      for (int ctr2 = 0; ctr2 < n; ++ctr2)
        for (int ctr3 = ctr1; ctr3 < n; ++ctr3)
          for (int ctr4 = ctr2; ctr4 < n; ++ctr4)
            if (acc[ctr3 + 1][ctr4 + 1] - acc[ctr3 + 1][ctr2] -
                acc[ctr1][ctr4 + 1] + acc[ctr1][ctr2] == 0) {
              dp[ctr3 + 1][ctr4 + 1] += 1;
              dp[ctr3 + 1][ctr2] -= 1;
              dp[ctr1][ctr4 + 1] -= 1;
              dp[ctr1][ctr2] += 1;
            }
    for (int ctr1 = 0; ctr1 <= n; ++ctr1)
      for (int ctr2 = 0; ctr2 <= n; ++ctr2) {
        if (ctr1 > 0)
          dp[ctr1][ctr2] += dp[ctr1 - 1][ctr2];
        if (ctr2 > 0)
          dp[ctr1][ctr2] += dp[ctr1][ctr2 - 1];
        if (ctr1 > 0 && ctr2 > 0)
          dp[ctr1][ctr2] -= dp[ctr1 - 1][ctr2 - 1];
      }
    int x, y;
    while (m--) {
      in >> x >> y;
      out << dp[x - 1][y - 1] << "\n";
    }
  }
};

void solve(std::istream& in, std::ostream& out)
{
  out << std::setprecision(12);
  Solution solution;
  solution.solve(in, out);
}
// Powered by caide (code generator, tester, and library code inliner)


#include <fstream>
#include <iostream>


int main() {
  
  ios_base::sync_with_stdio(false);
  cin.tie(0);


  istream& in = cin;


  ostream& out = cout;

  solve(in, out);
  return 0;
}

0