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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef long long LL;

typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<string, int> P;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

// constant
//--------------------------------------------
const double PI = acos(-1.0);

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
const LL MOD = 1e9 + 7;
// vector <vector<int>> x(20, vector<int> (20)), y(20,vector<int> (20));
/*int counter_1 (int x) {
    if(x == 0) return 0;
    return counter_1(x >> 1) + (x & 1);
}*/

// int Counter(int x)
/*int factorial(int n) {
    if (n > 0) {
        return n * factorial(n - 1);
    } else {
        return 1;
    }
}*/
int factorial(int n) {
  if (n > 0) {
    return n * factorial(n - 1);
  } else {
    return 1;
  }
}
LL sum(LL a, LL b) {
  LL cnt = 0;
  cnt = (b - a + 1) * (a + b) / 2;
  return cnt;
}
/*LL yaku(int x){
    vector<pair<LL, LL> a=[];
    LL xh;
    if(x==1){
        pair<LL, LL> tmp;
        tmp=MP(1,1);
        return tmp;
    }
    else{
        FOR(i, 1, x/2+1){
            if(x%i==0){
                a.append(MP(i,x/i));
            }
        }
    }
    return a;

}*/
LL cul(int a, int b) {
  LL f = pow(a, 4) + pow(a, 3) * b + pow(a, 2) * pow(b, 2) + a * pow(b, 3) +
         pow(b, 4);
  return f;
}
int main() {
  int n;
  cin >> n;
  VI a(n);
  // VI cnt(n-1,0);
  cin >> a[0];
  FOR(i, 1, n) {
    cin >> a[i];
    // cnt[i-1]=a[i]-a[i-1];
    // cout << cnt[i-1] << endl;
  }
  map<int, int> x;
  LL ans = 0;
  REP(i, n) {
    if (x[i - a[i]] != 0) {
      ans += x[i - a[i]];
    }
    x[a[i] + i]++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// math
//-------------------------------------------
template <class T> inline T sqr(T x) { return x * x; }

// typedef
//------------------------------------------
typedef long long LL;

typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<string, int> P;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

// constant
//--------------------------------------------
const double PI = acos(-1.0);

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

// debug
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;
const LL MOD = 1e9 + 7;
// vector <vector<int>> x(20, vector<int> (20)), y(20,vector<int> (20));
/*int counter_1 (int x) {
    if(x == 0) return 0;
    return counter_1(x >> 1) + (x & 1);
}*/

// int Counter(int x)
/*int factorial(int n) {
    if (n > 0) {
        return n * factorial(n - 1);
    } else {
        return 1;
    }
}*/
int factorial(int n) {
  if (n > 0) {
    return n * factorial(n - 1);
  } else {
    return 1;
  }
}
LL sum(LL a, LL b) {
  LL cnt = 0;
  cnt = (b - a + 1) * (a + b) / 2;
  return cnt;
}
/*LL yaku(int x){
    vector<pair<LL, LL> a=[];
    LL xh;
    if(x==1){
        pair<LL, LL> tmp;
        tmp=MP(1,1);
        return tmp;
    }
    else{
        FOR(i, 1, x/2+1){
            if(x%i==0){
                a.append(MP(i,x/i));
            }
        }
    }
    return a;

}*/
LL cul(int a, int b) {
  LL f = pow(a, 4) + pow(a, 3) * b + pow(a, 2) * pow(b, 2) + a * pow(b, 3) +
         pow(b, 4);
  return f;
}
int main() {
  int n;
  cin >> n;
  VI a(n);
  // VI cnt(n-1,0);
  cin >> a[0];
  FOR(i, 1, n) {
    cin >> a[i];
    // cnt[i-1]=a[i]-a[i-1];
    // cout << cnt[i-1] << endl;
  }
  map<int, int> xi, xj;
  REP(i, n) {
    xi[i + a[i]]++;
    xj[i - a[i]]++;
  }
  LL ans = 0;
  /*REP(i,n-1){
      FOR(j,i+1,n){
          if(j-i==a[i]+a[j]){
              LL tmp=a[i]+a[j];
              LL cntj;
              int l=1;
              FOR(k, j, n-1){
                  cntj+=cnt[k];
                  if(l==cnt[k]){
                      ans++;
                  }

              }
          }
      }
  }*/
  for (auto &i : xi) {
    for (auto &j : xj) {
      if (i.first == j.first) {
        ans += i.second * j.second;
      }
    }
  }
  // auto x = find(ALL(a),112829221);
  // cout << *x << endl;
  cout << ans << endl;
}