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

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> map<T, T> getPrimeFactor(T n) {
  map<T, T> res;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

template <typename T> bool IsPrimeNumber(T num) {
  if (num <= 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  return true;
}

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repeq(i, s, e) for (ll i = s; i <= e; i++)

int main() {
  ll N;
  std::cin >> N;
  vll A(N + 1);
  map<ll, ll> cnt_p, cnt_m;
  set<ll> num;

  repeq(i, 1, N) {
    std::cin >> A[i];
    cnt_p[i + A[i]]++;
    cnt_m[i - A[i]]++;
    num.insert(i + A[i]);
    num.insert(i - A[i]);
  }

  ll ans = 0;
  for (auto itr = num.begin(); itr != num.end(); itr++) {
    ans += cnt_p[*itr] * cnt_m[*itr];
  }

  std::cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using vll = vector<long long>;
using sll = set<long long>;

template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

template <typename T> map<T, T> getPrimeFactor(T n) {
  map<T, T> res;
  for (T i = 2; i * i <= n; ++i) {
    while (n % i == 0) {
      res[i]++;
      n /= i;
    }
  }
  if (n != 1)
    res[n] = 1;
  return res;
}

template <typename T> bool IsPrimeNumber(T num) {
  if (num <= 2)
    return true;
  else if (num % 2 == 0)
    return false;

  double sqrtNum = sqrt(num);
  for (int i = 3; i <= sqrtNum; i += 2) {
    if (num % i == 0) {
      return false;
    }
  }

  return true;
}

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repeq(i, s, e) for (ll i = s; i <= e; i++)

int main() {
  ll N;
  std::cin >> N;
  vll A(N + 1);
  map<ll, vll> dic;

  repeq(i, 1, N) {
    std::cin >> A[i];
    dic[A[i]].push_back(i);
  }

  for (auto itr = dic.begin(); itr != dic.end(); itr++) {
    sort(itr->second.begin(), itr->second.end());
  }

  ll ans = 0;
  for (auto itr = dic.begin(); itr != dic.end(); itr++) {

    ll sum = (itr->first + itr->first) * -1;
    rep(i, 0, itr->second.size()) {
      for (ll j = i - 1; j >= 0; j--) {
        if (sum == itr->second[j] - itr->second[i]) {
          ans++;
        } else if (itr->second[j] - itr->second[i] < sum) {
          break;
        }
      }
    }

    auto itr2 = itr;
    itr2++;
    while (itr2 != dic.end()) {
      sum = itr->first + itr2->first;
      ll s = 0;
      rep(i, 0, itr->second.size()) {
        rep(j, s, itr2->second.size()) {
          if (sum == abs(itr->second[i] - itr2->second[j])) {
            ans++;
          } else if (itr->second[i] - itr2->second[j] < sum * -1) {
            break;
          } else if (itr->second[i] - itr2->second[j] > sum) {
            s = j + 1;
          }
        }
      }
      itr2++;
    }
  }

  std::cout << ans << endl;
  return 0;
}