#define PROBLEM "https://yukicoder.me/problems/no/755" #include <iostream> #include <vector> #include "Mylib/Algorithm/cumulative_sum_2d.cpp" #include "Mylib/IO/input_tuples.cpp" #include "Mylib/IO/input_vector.cpp" namespace hl = haar_lib; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int N, M; std::cin >> N >> M; auto A = hl::input_vector<int64_t>(M, M); auto c = hl::cumulative_sum_2d_builder<int64_t>(M, M).update(A).build(); for (auto [x, y] : hl::input_tuples<int, int>(N)) { --x, --y; int ans = 0; for (int x1 = 0; x1 <= x; ++x1) { for (int y1 = 0; y1 <= y; ++y1) { for (int x2 = x; x2 < M; ++x2) { for (int y2 = y; y2 < M; ++y2) { if (c.fold({x1, y1}, {x2 + 1, y2 + 1}) == 0) ++ans; } } } } std::cout << ans << std::endl; } return 0; }
#line 1 "test/yukicoder/755/main.test.cpp" #define PROBLEM "https://yukicoder.me/problems/no/755" #include <iostream> #include <vector> #line 2 "Mylib/Algorithm/cumulative_sum_2d.cpp" #include <cassert> #include <functional> #line 5 "Mylib/Algorithm/cumulative_sum_2d.cpp" namespace haar_lib { template <typename T> class cumulative_sum_2d { public: using value_type = T; private: template <typename> friend class cumulative_sum_2d_builder; int N_, M_; std::vector<std::vector<T>> data_; public: T fold(std::pair<int, int> p1, std::pair<int, int> p2) const { const auto [x1, y1] = p1; const auto [x2, y2] = p2; assert(0 <= x1 and x1 <= x2 and x2 <= N_); assert(0 <= y1 and y1 <= y2 and y2 <= M_); return data_[x2][y2] - data_[x1][y2] - data_[x2][y1] + data_[x1][y1]; } }; template <typename T> class cumulative_sum_2d_builder { int N_, M_; std::vector<std::vector<T>> data_; public: cumulative_sum_2d_builder() {} cumulative_sum_2d_builder(int N, int M) : N_(N), M_(M), data_(N + 1, std::vector<T>(M + 1)) {} auto& update(const std::vector<std::vector<T>>& a) { for (int i = 0; i < N_; ++i) { for (int j = 0; j < M_; ++j) { data_[i + 1][j + 1] += a[i][j]; } } return *this; } auto& update(int i, int j, const T& val) { data_[i + 1][j + 1] += val; return *this; } auto build() const { cumulative_sum_2d<T> ret; ret.data_ = data_; ret.N_ = N_; ret.M_ = M_; for (int i = 1; i <= N_; ++i) for (int j = 0; j <= M_; ++j) ret.data_[i][j] += ret.data_[i - 1][j]; for (int i = 0; i <= N_; ++i) for (int j = 1; j <= M_; ++j) ret.data_[i][j] += ret.data_[i][j - 1]; return ret; } }; } // namespace haar_lib #line 2 "Mylib/IO/input_tuples.cpp" #include <initializer_list> #line 4 "Mylib/IO/input_tuples.cpp" #include <tuple> #include <utility> #line 6 "Mylib/IO/input_tuple.cpp" namespace haar_lib { template <typename T, size_t... I> static void input_tuple_helper(std::istream &s, T &val, std::index_sequence<I...>) { (void) std::initializer_list<int>{(void(s >> std::get<I>(val)), 0)...}; } template <typename T, typename U> std::istream &operator>>(std::istream &s, std::pair<T, U> &value) { s >> value.first >> value.second; return s; } template <typename... Args> std::istream &operator>>(std::istream &s, std::tuple<Args...> &value) { input_tuple_helper(s, value, std::make_index_sequence<sizeof...(Args)>()); return s; } } // namespace haar_lib #line 8 "Mylib/IO/input_tuples.cpp" namespace haar_lib { template <typename... Args> class InputTuples { struct iter { using value_type = std::tuple<Args...>; value_type value; bool fetched = false; int N, c = 0; value_type operator*() { if (not fetched) { std::cin >> value; } return value; } void operator++() { ++c; fetched = false; } bool operator!=(iter &) const { return c < N; } iter(int N) : N(N) {} }; int N; public: InputTuples(int N) : N(N) {} iter begin() const { return iter(N); } iter end() const { return iter(N); } }; template <typename... Args> auto input_tuples(int N) { return InputTuples<Args...>(N); } } // namespace haar_lib #line 4 "Mylib/IO/input_vector.cpp" namespace haar_lib { template <typename T> std::vector<T> input_vector(int N) { std::vector<T> ret(N); for (int i = 0; i < N; ++i) std::cin >> ret[i]; return ret; } template <typename T> std::vector<std::vector<T>> input_vector(int N, int M) { std::vector<std::vector<T>> ret(N); for (int i = 0; i < N; ++i) ret[i] = input_vector<T>(M); return ret; } } // namespace haar_lib #line 8 "test/yukicoder/755/main.test.cpp" namespace hl = haar_lib; int main() { std::cin.tie(0); std::ios::sync_with_stdio(false); int N, M; std::cin >> N >> M; auto A = hl::input_vector<int64_t>(M, M); auto c = hl::cumulative_sum_2d_builder<int64_t>(M, M).update(A).build(); for (auto [x, y] : hl::input_tuples<int, int>(N)) { --x, --y; int ans = 0; for (int x1 = 0; x1 <= x; ++x1) { for (int y1 = 0; y1 <= y; ++y1) { for (int x2 = x; x2 < M; ++x2) { for (int y2 = y; y2 < M; ++y2) { if (c.fold({x1, y1}, {x2 + 1, y2 + 1}) == 0) ++ans; } } } } std::cout << ans << std::endl; } return 0; }