Submission #76506670


Source Code Expand

// #define ATCODER
// #define BOOST

// #define MOD1000000007

// #pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#ifdef MOD1000000007
#define MOD 1000000007
#else
#define MOD 998244353
#endif

#ifdef ATCODER
#include <atcoder/all>
using namespace atcoder;
#ifdef MOD1000000007
using mint = modint1000000007;
#else
using mint = modint998244353;
#endif
#endif

#ifdef BOOST
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using Bint = cpp_int;
using Real32 = number<cpp_dec_float<32>>;
#define repb(i, r) for(Bint i = 0; (Bint)(i) < (Bint)(r); i++)
#define perb(i, r) for(Bint i = (Bint)(r); (Bint)(i) > 0; i++)
#endif

// c++テンプレ
// https://hackmd.io/J0ohc60AQaKfZJMJdT6sUg
// https://dekavit.net
// https://atcoder.github.io/ac-library/document_ja/index.html

#include <bits/stdc++.h>
#define repp(i, l, r) for(long long i = (long long)(l); i < (long long)(r); i++)
#define perp(i, r, l) for(long long i = (long long)(r); i > (long long)(l); i--)
#define rep(i, r) for(long long i = 0; (long long)(i) < (long long)(r); i++)
#define per(i, r) for(long long i = (long long)(r); (long long)(i) > 0; i--)
#define INF (1LL<<62)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;


// 2次元累積和
// [r1,r2)x[c1,c2)の範囲の総和をO(1)で求められる
// 前計算O(HW),クエリの処理O(1)
template<typename T>
class cumulative_sum_2d {
  private:
    vector<vector<T>> v;
    int H,W;
    T e = 0;

  public:
    cumulative_sum_2d(int h, int w) {
      v.resize(h+1);
      for(auto &x : v) x.assign(w+1,e);
      H = h, W = w;
    }
    cumulative_sum_2d(vector<vector<T>> &A) {
      H = A.size(), W = A[0].size();
      v.resize(H+1);
      for(auto &x : v) x.assign(W+1,e);
      for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
          v[i+1][j+1] = A[i][j];
        }
      }
    }
    // v[r][c]にxを代入
    // O(1)
    void set(int r, int c, T x) {
      if(0<=r&&r<H&&0<=x&&c<W) v[r+1][c+1] = x;
    }
    // 累積和を構築
    // O(HW)
    void build() {
      for(int i = 1; i <= H; i++) {
        for(int j = 1; j <= W; j++) {
          v[i][j] += v[i-1][j] + v[i][j-1] - v[i-1][j-1];
        }
      }
    }
    // [r1,r2)x[c1,c2)の総和を返す
    // O(1)
    T prod(int r1, int c1, int r2, int c2) {
      if(0<=r1&&r1<r2&&r2<=H&&0<=c1&&c1<c2&&c2<=W){
        return v[r2][c2] - v[r2][c1] - v[r1][c2] + v[r1][c1];
      }else{
        cerr<<"Out of range."<<endl;
        return e;
      }
    }

    vector<T> &operator[](int i) { return this->v[i]; }
};

int solver(){
  ll H,W,K;
  cin>>H>>W>>K;
  vector<string> S(H);
  vector<vector<ll>> A(H,vector<ll>(W));
  rep(i,H){
    cin>>S[i];
    rep(j,W){
      A[i][j] = S[i][j]-'0';
    }
  }
  cumulative_sum_2d<ll> C(A);
  C.build();
  unordered_map<ll,ll> cnt;
  ll ans = 0;
  repp(h2,1,H+1){
    repp(h1,1,h2+1){
      rep(w,W+1){
        cnt[C[h2][w]-C[h1-1][w]]++;
      }
      repp(w1,1,W+1){
        auto temp = C[h2][w1-1]-C[h1-1][w1-1];
        cnt[temp]--;
        temp = K + C[h2][w1-1] - C[h1-1][w1-1];
        ans += cnt[temp];
      }
      cnt[C[h2][W]-C[h1-1][W]] = 0;
    }
  }
  cout<<ans<<endl;
  return 0;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout<<fixed<<setprecision(10);
  solver();
  return 0;
}

Submission Info

Submission Time
Task D - Count Subgrid Sum = K
User dekavit
Language C++23 (GCC 15.2.0)
Score 425
Code Size 3589 Byte
Status AC
Exec Time 2059 ms
Memory 16896 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 425 / 425
Status
AC × 3
AC × 31
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1 ms 3416 KiB
sample_02.txt AC 1 ms 3480 KiB
sample_03.txt AC 1 ms 3700 KiB
test_01.txt AC 1 ms 3572 KiB
test_02.txt AC 1 ms 3388 KiB
test_03.txt AC 1 ms 3484 KiB
test_04.txt AC 1 ms 3420 KiB
test_05.txt AC 527 ms 7684 KiB
test_06.txt AC 523 ms 7696 KiB
test_07.txt AC 1237 ms 10280 KiB
test_08.txt AC 2059 ms 12776 KiB
test_09.txt AC 533 ms 7660 KiB
test_10.txt AC 534 ms 7612 KiB
test_11.txt AC 670 ms 7684 KiB
test_12.txt AC 531 ms 7612 KiB
test_13.txt AC 528 ms 7844 KiB
test_14.txt AC 570 ms 7924 KiB
test_15.txt AC 635 ms 7728 KiB
test_16.txt AC 528 ms 7768 KiB
test_17.txt AC 534 ms 7812 KiB
test_18.txt AC 597 ms 8212 KiB
test_19.txt AC 554 ms 7768 KiB
test_20.txt AC 531 ms 7804 KiB
test_21.txt AC 919 ms 12560 KiB
test_22.txt AC 1413 ms 16896 KiB
test_23.txt AC 1169 ms 10336 KiB
test_24.txt AC 805 ms 12084 KiB
test_25.txt AC 523 ms 7708 KiB
test_26.txt AC 525 ms 7660 KiB
test_27.txt AC 526 ms 7688 KiB
test_28.txt AC 531 ms 7704 KiB