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

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define ull unsigned long long

#define max 1000000007

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  while (ll r = a % b)
    a = b, b = r;
  return b;
}

class mod {
public:
  ll val;

  mod(ll v) { val = v % max; }

  mod() { val = 0; }

  void operator-=(const mod &v) {
    val -= v.val;

    if (val < 0)
      val += max;
  }

  void operator+=(const mod &v) {
    if (val + v.val >= max)
      val -= max;
    val += v.val;
  }

  void operator+=(ll &v) {
    if (val + v >= max)
      val -= max;
    val += v;
  }

  void operator*=(const mod &v) {
    val *= v.val;
    val %= max;
  }

  void operator*=(ll &v) {
    val *= v;
    val %= max;
  }

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

  void operator/=(const mod &v) {
    val *= modinv(v.val);
    val %= max;
  }

  void operator/=(ll &v) {
    val *= modinv(v);
    val %= max;
  }
};

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  map<ll, vector<int>> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (m.find(i - a[i]) == m.end())
      m[i - a[i]] = {i};
    else
      m[i - a[i]].push_back(i);
  }

  ll r = 0;
  for (int i = 0; i < n; i++) {
    ll t = i + a[i];
    if (m.find(t) == m.end())
      continue;
    r += m[t].end() - lower_bound(m[t].begin(), m[t].end(), i);
  }

  printf("%lld", r);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define ll long long
#define ull unsigned long long

#define max 1000000007

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  while (ll r = a % b)
    a = b, b = r;
  return b;
}

class mod {
public:
  ll val;

  mod(ll v) { val = v % max; }

  mod() { val = 0; }

  void operator-=(const mod &v) {
    val -= v.val;

    if (val < 0)
      val += max;
  }

  void operator+=(const mod &v) {
    if (val + v.val >= max)
      val -= max;
    val += v.val;
  }

  void operator+=(ll &v) {
    if (val + v >= max)
      val -= max;
    val += v;
  }

  void operator*=(const mod &v) {
    val *= v.val;
    val %= max;
  }

  void operator*=(ll &v) {
    val *= v;
    val %= max;
  }

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

  void operator/=(const mod &v) {
    val *= modinv(v.val);
    val %= max;
  }

  void operator/=(ll &v) {
    val *= modinv(v);
    val %= max;
  }
};

int main() {
  int n;
  cin >> n;
  vector<ull> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll r = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      if (j - i == a[i] + a[j])
        r++;
  printf("%lld", r);
  return 0;
}