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

#include <bits/stdc++.h>
using namespace std;

#define db(x) cout << x << '\\n'
#define db1(x) cout << #x << "=" << x << '\\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\\n'
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << '\\n'
#define repi(i, n) for (int i = 0; i < (n); ++i)
// #define repA(i,a,n) for(int i=a;i<=(n);++i)
// #define repD(i,a,n) for(int i=a;i>=(n);--i)
#define ll long long int
#define endl '\\n'
#define lld long long double
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define err(t) cout << "Error : " << t << endl;
#define all(v) v.begin(), v.end()
#define mp make_pair
#define M 100000
#define MD 1000000007
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= (b); i++)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vec(a) vector<a>
#define se second
#define fi first
#define inf 0xffffffff
#define inchar getchar_unlocked
#define outchar(x) putchar_unlocked(x)

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // int n;cin>>n;
  // string s;cin>>s;

  int n;
  cin >> n;
  vi v(n);
  repi(i, n) cin >> v[i];
  ll c = 0;
  /*repi(i,n-1){
          rep(j,i+v[i]+1,n) if(j-i==v[i]+v[j]) c++;
  }*/
  vi b(n, 0);
  repi(i, n) if ((i - v[i]) > 0) b[i - v[i]]++;
  // repi(i,n) cout<<b[i]<<" ";cout<<endl;
  repi(i, n) if ((i + v[i]) < n) c += b[i + v[i]];

  db(c);
  return 0;
}
/*

1    3
 1 3


                  2  3  3  2  1
    +1            -2











*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define db(x) cout << x << '\\n'
#define db1(x) cout << #x << "=" << x << '\\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\\n'
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << '\\n'
#define repi(i, n) for (int i = 0; i < (n); ++i)
// #define repA(i,a,n) for(int i=a;i<=(n);++i)
// #define repD(i,a,n) for(int i=a;i>=(n);--i)
#define ll long long int
#define endl '\\n'
#define lld long long double
#define vi vector<int>
#define vl vector<ll>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define err(t) cout << "Error : " << t << endl;
#define all(v) v.begin(), v.end()
#define mp make_pair
#define M 100000
#define MD 1000000007
#define pb push_back
#define rep(i, a, b) for (ll i = a; i <= (b); i++)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vec(a) vector<a>
#define se second
#define fi first
#define inf 0xffffffff
#define inchar getchar_unlocked
#define outchar(x) putchar_unlocked(x)

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // int n;cin>>n;
  // string s;cin>>s;

  int n;
  cin >> n;
  vi v(n);
  repi(i, n) cin >> v[i];
  ll c = 0;
  repi(i, n - 1) { rep(j, i + v[i] + 1, n) if (j - i == v[i] + v[j]) c++; }
  db(c);
  /*
  int q;cin>>q;while(q--){

   }
   //*/
  return 0;
}