Problem p01140 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define rangeRep(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

int intervalX[1500];
int intervalY[1500];
int intervalsX[1500 * 1000];
int intervalsY[1500 * 1000];

class Solve {
public:
  Solve(){};
  ~Solve(){};

  void solver() {
    int n, m;
    while (std::cin >> n >> m, n, m) {
      std::memset(intervalX, 0, sizeof(intervalX));
      std::memset(intervalY, 0, sizeof(intervalY));
      std::memset(intervalsX, 0, sizeof(intervalsX));
      std::memset(intervalsY, 0, sizeof(intervalsY));
      // input
      rep(i, n) { std::cin >> intervalX[i]; }
      rep(i, m) { std::cin >> intervalY[i]; }
      // All patterns enumeration
      int squares = 0;
      int edgeXNo = 0;
      int cnt = 0;
      int maxLen = 0;
      rep(i, n) {
        rep(j, n - i) {
          int len = 0;
          rangeRep(k, j, j + i + 1) { len += intervalX[k]; }
          intervalsX[len]++;
          if (maxLen < len)
            maxLen = len;
          ++cnt;
        }
      }
      rep(i, m) {
        edgeXNo = 0;
        rep(j, m - i) {
          int len = 0;
          rangeRep(k, j, j + i + 1) { len += intervalY[k]; }
          intervalsY[len]++;
          if (maxLen < len)
            maxLen = len;
        }
      }
      rep(i, maxLen + 1) { squares += intervalsX[i] * intervalsY[i]; }
      std::cout << squares << std::endl;
    }
  }

private:
};

int main() {
  Solve solve;
  solve.solver();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define rangeRep(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

int intervalX[1500];
int intervalY[1500];
int intervalsX[1500 * 1500];
int intervalsY[1500 * 1500];

class Solve {
public:
  Solve(){};
  ~Solve(){};

  void solver() {
    int n, m;
    while (std::cin >> n >> m, n, m) {
      std::memset(intervalX, 0, sizeof(intervalX));
      std::memset(intervalY, 0, sizeof(intervalY));
      std::memset(intervalsX, 0, sizeof(intervalsX));
      std::memset(intervalsY, 0, sizeof(intervalsY));
      // input
      rep(i, n) { std::cin >> intervalX[i]; }
      rep(i, m) { std::cin >> intervalY[i]; }
      // All patterns enumeration
      int numOfIntervalsX = 0;
      int numOfIntervalsY = 0;
      int squares = 0;
      int edgeXNo = 0;
      int cnt = 0;
      rep(i, n) {
        rep(j, n - i) {
          rangeRep(k, j, j + i + 1) { intervalsX[cnt] += intervalX[k]; }
          ++cnt;
          ++numOfIntervalsX;
        }
      }
      cnt = 0;
      rep(i, m) {
        edgeXNo = 0;
        rep(j, m - i) {
          rangeRep(k, j, j + i + 1) { intervalsY[cnt] += intervalY[k]; }
          rep(k, numOfIntervalsX) {
            if (intervalsX[k] == intervalsY[cnt])
              squares++;
          }
          ++cnt;
        }
      }
      std::cout << squares << "\\n";
    }
    std::cout << std::endl;
  }

private:
};

int main() {
  Solve solve;
  solve.solver();
  return 0;
}