Problem p03283 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <functional>
#include <iostream>
#include <vector>

template <typename T> class func_sum {
private:
  std::vector<std::vector<T>> sum_table;

protected:
  virtual T acc_sum(int i, int j);

public:
  func_sum(std::function<T(int, int)> func, int nx, int ny);
  ~func_sum();

  T calc_section_sum(int il, int ir, int jl, int jr);
};

template <typename T> T func_sum<T>::acc_sum(int i, int j) {
  return sum_table[i][j];
};

template <typename T>
func_sum<T>::func_sum(std::function<T(int, int)> func, int nx, int ny) {
  sum_table = std::vector<std::vector<T>>(ny + 1, std::vector<T>(nx + 1, T()));
  for (int i = 1; i <= nx; i++) {
    sum_table[i][0] += func(i - 1, 0);
  }
  for (int j = 1; j <= ny; j++) {
    sum_table[0][j] += func(0, j - 1);
  }
  for (int i = 1; i <= nx; i++) {
    for (int j = 1; j <= ny; j++) {
      sum_table[i][j] += func(i - 1, j - 1) + sum_table[i - 1][j] +
                         sum_table[i][j - 1] - sum_table[i - 1][j - 1];
    }
  }
}

template <typename T> func_sum<T>::~func_sum() {}

template <typename T>
T func_sum<T>::calc_section_sum(int il, int ir, int jl, int jr) {
  return acc_sum(ir, jr) - acc_sum(ir, jl - 1) - acc_sum(il - 1, jr) +
         acc_sum(il - 1, jl - 1);
}

int main() {
  int N, M, Q;
  std::cin >> N >> M >> Q;
  std::vector<std::vector<int>> table(N, std::vector<int>(N, 0));
  for (int i = 0; i < M; i++) {
    int L, R;
    std::cin >> L >> R;
    table[L - 1][R - 1]++;
  }

  auto func = [](int i, int j, std::vector<std::vector<int>> table) {
    return table[i][j];
  };

  std::function<int(int, int)> func_curried =
      std::bind(func, std::placeholders::_1, std::placeholders::_2, table);

  func_sum<int> ds(func_curried, N, N);

  for (int i = 0; i < Q; i++) {
    int p, q;
    std::cin >> p >> q;
    std::cout << ds.calc_section_sum(p, q, p, q) << std::endl;
  }
  return 0;
}

Fixed submission:
#include <functional>
#include <iostream>
#include <vector>

template <typename T> class func_sum {
private:
  std::vector<std::vector<T>> sum_table;

protected:
  virtual T acc_sum(int i, int j);

public:
  func_sum(std::function<T(int, int)> func, int nx, int ny);
  ~func_sum();

  T calc_section_sum(int il, int ir, int jl, int jr);
};

template <typename T> T func_sum<T>::acc_sum(int i, int j) {
  return sum_table[i][j];
};

template <typename T>
func_sum<T>::func_sum(std::function<T(int, int)> func, int nx, int ny) {
  sum_table = std::vector<std::vector<T>>(ny + 1, std::vector<T>(nx + 1, T()));
  for (int i = 1; i <= nx; i++) {
    sum_table[i][0] += func(i - 1, 0);
  }
  for (int j = 1; j <= ny; j++) {
    sum_table[0][j] += func(0, j - 1);
  }
  for (int i = 1; i <= nx; i++) {
    for (int j = 1; j <= ny; j++) {
      sum_table[i][j] += func(i - 1, j - 1) + sum_table[i - 1][j] +
                         sum_table[i][j - 1] - sum_table[i - 1][j - 1];
    }
  }
}

template <typename T> func_sum<T>::~func_sum() {}

template <typename T>
T func_sum<T>::calc_section_sum(int il, int ir, int jl, int jr) {
  return acc_sum(ir, jr) - acc_sum(ir, jl - 1) - acc_sum(il - 1, jr) +
         acc_sum(il - 1, jl - 1);
}

int main() {
  int N, M, Q;
  std::cin >> N >> M >> Q;
  std::vector<std::vector<int>> table(N, std::vector<int>(N, 0));
  for (int i = 0; i < M; i++) {
    int L, R;
    std::cin >> L >> R;
    table[L - 1][R - 1]++;
  }

  auto func = [](int i, int j, const std::vector<std::vector<int>> &table) {
    return table[i][j];
  };

  std::function<int(int, int)> func_curried =
      std::bind(func, std::placeholders::_1, std::placeholders::_2, table);

  func_sum<int> ds(func_curried, N, N);

  for (int i = 0; i < Q; i++) {
    int p, q;
    std::cin >> p >> q;
    std::cout << ds.calc_section_sum(p, q, p, q) << std::endl;
  }
  return 0;
}