Problem p01540 (C++) — buggy submission, verdict: Memory Limit Exceeded


//?¶??????????????????????

#if 0
#include <algorithm>
#include <array>
#include <assert.h>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto& in = std::cin;
auto& out = std::cout;

int32_t N,W;
int32_t w[100];
int32_t v[100];

int dp[100][10001];
//i??\\???????????????????????§??????capacity??????????????¶???????????°????????¨???????????§???
int func(int i, int capacity)
{
	if (capacity == 0) {
		return 0;
	}
	if (i >= N) {
		return 0;
	}
	if (dp[i][capacity] != -1) {
		return dp[i][capacity];
	}
	int res = 0;
	//??\\??????
	if (capacity >= w[i]) { res = std::max(res, func(i + 1, capacity - w[i]) + v[i]); }
	//??\\?????????
	res = std::max(res, func(i + 1, capacity));
	return dp[i][capacity] = res;
}

int main()
{
	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);

	in >> N>>W;
	for (int i = 0; i < N; i++)
	{
		in >> v[i] >> w[i];
	}
	bool used[100] = {};
	out << func(0, used)<<endl;

	return 0;
}
#endif

#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;

int32_t N, Q;
// Coordinate Compression
using INT_T = int32_t;
constexpr int32_t N_MAX = 10010;
INT_T t_x[N_MAX];
INT_T t_y[N_MAX];
INT_T CC_X[N_MAX];
auto CC_X_end = CC_X;
INT_T CC_Y[N_MAX];
auto CC_Y_end = CC_Y;

void Init_CoordinateCompression(INT_T (&bace)[N_MAX], INT_T (&CC_buf)[N_MAX],
                                INT_T *&CC_buf_end) {
  if (bace != CC_buf) {
    std::copy_n(bace, N, CC_buf);
    for (int i = 0; i < N; ++i) {
      CC_buf[N + i] = bace[i] - 1;
    }
  }
  CC_buf_end = CC_buf + 2 * N;
  std::sort(CC_buf, CC_buf + 2 * N);
  CC_buf_end = std::unique(CC_buf, CC_buf_end);
}
INT_T get_coordinate(INT_T (&CC_buf)[N_MAX], INT_T *CC_buf_end, INT_T v) {
  return std::lower_bound(CC_buf, CC_buf_end, v) - CC_buf + 1;
}
void Do_CoordinateCompression(INT_T (&bace)[N_MAX], INT_T (&CC_buf)[N_MAX],
                              INT_T *&CC_buf_end) {
  Init_CoordinateCompression(bace, CC_buf, CC_buf_end);
  for (int32_t i = 0; i < N; ++i) {
    bace[i] = get_coordinate(CC_buf, CC_buf_end, bace[i]);
  }
}

using INT_T_CS = int16_t;
INT_T_CS map[N_MAX][N_MAX];
template <typename ARR> inline void add_full(ARR &v1, ARR &v2) { v1 += v2; }
template <typename ARR, size_t N>
inline void add_full(ARR (&v1)[N], ARR (&v2)[N]) {
  auto iter1 = v1, iter2 = v2, iter1end = v1 + N;
  while (iter1 != iter1end) {
    add_full(*iter1++, *iter2++);
  }
}
// 1-indexed?´???????
template <size_t N> void CuSum(INT_T_CS (&arr)[N]) {
  for (size_t i = 2; i < N; i++) {
    arr[i] += arr[i - 1];
  }
}
template <typename ARR, size_t N> void CuSum(ARR (&arr)[N]) {
  for (auto &arr2 : arr) {
    CuSum(arr2);
  }
  for (size_t i = 2; i < N; i++) {
    add_full(arr[i], arr[i - 1]);
  }
}
#if 0
template<size_t N, size_t M>void CuSum(INT_T_CS(&arr)[N][M])
{
	for (auto& arr2 : arr) {
		CuSum(arr2);
	}

	for (size_t i = 2; i < N; i++)
	{
		add_full(arr[i], arr[i - 1]);
	}
}
template<size_t N, size_t M, size_t O>void CuSum(INT_T_CS(&arr)[N][M][O])
{
	for (auto& arr2 : arr) {
		CuSum(arr2);
	}

	for (size_t i = 2; i < N; i++)
	{
		add_full(arr[i], arr[i - 1]);
	}
}
#endif
// 1-indexed???(]??????
template <size_t N> INT_T_CS get_sum(INT_T_CS (&arr)[N], int x, int x2) {
  return (arr[x2] - arr[x]);
}
// 1-indexed???(]??????
template <size_t N, size_t M>
INT_T_CS get_sum(INT_T_CS (&arr)[N][M], int x, int y, int x2, int y2) {
  return (arr[y2][x2] - arr[y][x2] - arr[y2][x] + arr[y][x]);
}
// 1-indexed???(]??????
template <size_t N, size_t M, size_t O>
INT_T_CS get_sum(INT_T_CS (&arr)[N][M][O], int x, int y, int z, int x2, int y2,
                 int z2) {
  return (arr[z2][y2][x2] - arr[z][y2][x2] - arr[z2][y][x2] - arr[z2][y2][x] +
          arr[z2][y][x] + arr[z2][y][x] + arr[z][y][x2] - arr[z][y][x]);
}

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);

  in >> N >> Q;
  for (int32_t i = 0; i < N; ++i) {
    in >> t_x[i] >> t_y[i];
  }
  Do_CoordinateCompression(t_x, CC_X, CC_X_end);
  Do_CoordinateCompression(t_y, CC_Y, CC_Y_end);
  for (int32_t i = 0; i < N; ++i) {
    ++map[t_y[i]][t_x[i]];
  }
  CuSum(map);
  while (Q--) {
    int x, y, x2, y2;
    in >> x >> y >> x2 >> y2;
    x = get_coordinate(CC_X, CC_X_end, x);
    --x;
    x2 = get_coordinate(CC_X, CC_X_end, x2);
    y = get_coordinate(CC_Y, CC_Y_end, y);
    --y;
    y2 = get_coordinate(CC_Y, CC_Y_end, y2);
    out << get_sum(map, x, y, x2, y2) << endl;
  }

  return 0;
}
#endif

Error: MLE

Fixed submission:


//?¶??????????????????????

#if 0
#include <algorithm>
#include <array>
#include <assert.h>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto& in = std::cin;
auto& out = std::cout;

int32_t N,W;
int32_t w[100];
int32_t v[100];

int dp[100][10001];
//i??\\???????????????????????§??????capacity??????????????¶???????????°????????¨???????????§???
int func(int i, int capacity)
{
	if (capacity == 0) {
		return 0;
	}
	if (i >= N) {
		return 0;
	}
	if (dp[i][capacity] != -1) {
		return dp[i][capacity];
	}
	int res = 0;
	//??\\??????
	if (capacity >= w[i]) { res = std::max(res, func(i + 1, capacity - w[i]) + v[i]); }
	//??\\?????????
	res = std::max(res, func(i + 1, capacity));
	return dp[i][capacity] = res;
}

int main()
{
	using std::endl;
	in.sync_with_stdio(false);
	out.sync_with_stdio(false);

	in >> N>>W;
	for (int i = 0; i < N; i++)
	{
		in >> v[i] >> w[i];
	}
	bool used[100] = {};
	out << func(0, used)<<endl;

	return 0;
}
#endif

#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;

int32_t N, Q;
// Coordinate Compression
using INT_T = int32_t;
constexpr int32_t N_MAX = 10010;
INT_T t_x[N_MAX];
INT_T t_y[N_MAX];
INT_T CC_X[N_MAX];
auto CC_X_end = CC_X;
INT_T CC_Y[N_MAX];
auto CC_Y_end = CC_Y;
INT_T map[N_MAX][N_MAX];

void Init_CoordinateCompression(INT_T (&bace)[N_MAX], INT_T (&CC_buf)[N_MAX],
                                INT_T *&CC_buf_end) {
  if (bace != CC_buf) {
    std::copy_n(bace, N, CC_buf);
    for (int i = 0; i < N; ++i) {
      CC_buf[N + i] = bace[i] - 1;
    }
  }
  CC_buf_end = CC_buf + 2 * N;
  std::sort(CC_buf, CC_buf + 2 * N);
  CC_buf_end = std::unique(CC_buf, CC_buf_end);
}
INT_T get_coordinate(INT_T (&CC_buf)[N_MAX], INT_T *CC_buf_end, INT_T v) {
  return std::lower_bound(CC_buf, CC_buf_end, v) - CC_buf + 1;
}
void Do_CoordinateCompression(INT_T (&bace)[N_MAX], INT_T (&CC_buf)[N_MAX],
                              INT_T *&CC_buf_end) {
  Init_CoordinateCompression(bace, CC_buf, CC_buf_end);
  for (int32_t i = 0; i < N; ++i) {
    bace[i] = get_coordinate(CC_buf, CC_buf_end, bace[i]);
  }
}

template <typename ARR> inline void add_full(ARR &v1, ARR &v2) { v1 += v2; }
template <typename ARR, size_t N>
inline void add_full(ARR (&v1)[N], ARR (&v2)[N]) {
  auto iter1 = v1, iter2 = v2, iter1end = v1 + N;
  while (iter1 != iter1end) {
    add_full(*iter1++, *iter2++);
  }
}
// 1-indexed?´???????
template <size_t N> void CuSum(INT_T (&arr)[N]) {
  for (size_t i = 2; i < N; i++) {
    arr[i] += arr[i - 1];
  }
}
template <typename ARR, size_t N> void CuSum(ARR (&arr)[N]) {
  for (auto &arr2 : arr) {
    CuSum(arr2);
  }
  for (size_t i = 2; i < N; i++) {
    add_full(arr[i], arr[i - 1]);
  }
}
#if 0
template<size_t N, size_t M>void CuSum(INT_T(&arr)[N][M])
{
	for (auto& arr2 : arr) {
		CuSum(arr2);
	}

	for (size_t i = 2; i < N; i++)
	{
		add_full(arr[i], arr[i - 1]);
	}
}
template<size_t N, size_t M, size_t O>void CuSum(INT_T(&arr)[N][M][O])
{
	for (auto& arr2 : arr) {
		CuSum(arr2);
	}

	for (size_t i = 2; i < N; i++)
	{
		add_full(arr[i], arr[i - 1]);
	}
}
#endif
// 1-indexed???(]??????
template <size_t N> int64_t get_sum(INT_T (&arr)[N], int x, int x2) {
  return (arr[x2] - arr[x]);
}
// 1-indexed???(]??????
template <size_t N, size_t M>
int64_t get_sum(INT_T (&arr)[N][M], int x, int y, int x2, int y2) {
  return (arr[y2][x2] - arr[y][x2] - arr[y2][x] + arr[y][x]);
}
// 1-indexed???(]??????
template <size_t N, size_t M, size_t O>
int64_t get_sum(INT_T (&arr)[N][M][O], int x, int y, int z, int x2, int y2,
                int z2) {

  return arr(arr[z2][y2][x2] - arr[z][y2][x2] - arr[z2][y][x2] -
             arr[z2][y2][x] + arr[z2][y][x] + arr[z2][y][x] + arr[z][y][x2] -
             arr[z][y][x]);
}

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);

  in >> N >> Q;
  for (int32_t i = 0; i < N; ++i) {
    in >> t_x[i] >> t_y[i];
  }
  Do_CoordinateCompression(t_x, CC_X, CC_X_end);
  Do_CoordinateCompression(t_y, CC_Y, CC_Y_end);
  for (int32_t i = 0; i < N; ++i) {
    ++map[t_y[i]][t_x[i]];
  }
  CuSum(map);
  while (Q--) {
    int x, y, x2, y2;
    in >> x >> y >> x2 >> y2;
    x = get_coordinate(CC_X, CC_X_end, x);
    --x;
    x2 = get_coordinate(CC_X, CC_X_end, x2);
    y = get_coordinate(CC_Y, CC_Y_end, y);
    --y;
    y2 = get_coordinate(CC_Y, CC_Y_end, y2);
    out << get_sum(map, x, y, x2, y2) << endl;
  }

  return 0;
}
#endif