alexjercan/bugnet huggingface dataset:
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define lol long long
#define SUM(n) ((n) + 1) * (n) / 2 // 1〜nまでの総和を求める式
#define mp make_pair
#define fi first
#define se second
#define pu push_back
#define SYOU(x) fixed << setprecision(x + 1) // 小数点桁数を指定する
#define abs(x, y) max(x, y) - min(x, y)
#define all(v) v.begin(), v.end()
#define UPDigit(a, b) (a + b - 1) / b // 小数点切り上げ
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = int(1e9) + 7;
using namespace std;
using pii = pair<int, int>;
typedef vector<int> vit;
const int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

// 最大公約数
lol gcd(lol x, lol y) {
  if (x < y)
    swap(x, y);
  lol r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }
  return y;
}

// 最小公倍数
lol lcm(lol x, lol y) {
  lol a = x * y;
  return (a / gcd(x, y));
}

signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  lol a[n + 5];
  map<lol, lol> l, r;
  reps(i, n) {
    cin >> a[i];
    l[i - a[i]]++;
    r[i + a[i]]++;
  }
  lol ans = 0;
  for (auto x : l) {
    int foo = r[x.first];
    ans += x.second * foo;
  }
  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define lol long long
#define SUM(n) ((n) + 1) * (n) / 2　 //1〜nまでの総和を求める式
#define mp make_pair
#define fi first
#define se second
#define pu push_back
#define SYOU(x) setprecision(x + 1) // 小数点桁数を指定する
#define abs(x, y) (max(x, y) - min(x, y))
#define all(v) v.begin(), v.end()
#define UPDight(a, b) (a + b - 1) / b // 小数点切り上げ
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = int(1e9) + 7;
using namespace std;
using pii = pair<int, int>;
typedef vector<int> vit;

// 最大公約数
lol gcd(lol x, lol y) {
  if (x < y)
    swap(x, y);
  lol r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }
  return y;
}

// 最小公倍数
lol lcm(lol x, lol y) { return ((x * y) / gcd(x, y)); }

signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int n;
  cin >> n;
  lol a[n + 5];
  reps(i, n) { cin >> a[i]; }
  lol ans = 0;
  reps(i, n) {
    for (int j = i + a[i]; j <= n; j++) {
      if (j - i == a[i] + a[j]) {
        ans++;
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef int64_t ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
ll cntl[200000], cntr[200000];

int main() {
  ll n, x, ans = 0;
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> x;
    if (i + x < n)
      cntl[i + x]++;
    if (i - x > 1)
      cntr[i - x]++;
  }
  for (int i = 2; i < n; i++)
    ans += cntl[i] * cntr[i];
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <iostream>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, s, n) for (ll i = s; i < (n); i++)
using namespace std;

int main() {
  ll n;
  cin >> n;
  ll a;
  ll x[200000];
  ll y[200000];
  rep(i, n) {
    cin >> a;
    if (i - a < n)
      x[i + a]++;
    if (i - a > 1)
      y[i - a]++;
  }
  ll ans = 0;
  rep2(i, 2, n) { ans += x[i] * y[i]; }
  cout << ans << endl;

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using Graph = vector<vector<int>>;

typedef pair<int, int> P;

int main() {
  int N;
  cin >> N;
  vector<ll> a(N, 0);
  rep(i, N) cin >> a[i];
  map<int, int> counter;
  ll ans = 0;

  rep(i, N) { counter[a[i] + i]++; }
  rep(i, N) { ans += counter[i - a[i]]; }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL << 60;

int main() {

  int N;
  cin >> N;
  vector<ll> A(N, 0);
  rep(i, N) { cin >> A[i]; }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int j = i + A[i];
    while (j < N) {
      if (abs(i - j) == A[i] + A[j]) {
        ans++;
      }
      j++;
    }
  }

  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define fir first
#define sec second
#define pb push_back
#define ll long long
#define mp make_pair
#define all(x) (x).begin(), (x).end()
using namespace std;

int main() {
  ll n;
  cin >> n;
  ll ans = 0;
  map<ll, int> mp;
  for (ll i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    ans += mp[i - x];
    if (i + x < n)
      mp[i + x]++;
    // i-j=a[i]+a[j];
  }
  cout << ans << endl;
}

// 1  ....... 1


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define fir first
#define sec second
#define pb push_back
#define ll long long
#define mp make_pair
#define all(x) (x).begin(), (x).end()
using namespace std;
int solve(int cas) {
  int n;
  cin >> n;
  ll ans = 0;
  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    ans += mp[i - x];
    mp[x + i]++;
    // i-j=a[i]+a[j];
  }
  cout << ans << endl;
}

int main() {
  int t = 1;
  //	scanf("%d",&t);
  for (int cas = 1; cas <= t; cas++)
    solve(cas);
}

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3fLL;
constexpr ll mod = 1000000007;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  unordered_map<int, int> m;
  m.reserve(N);
  ll ans = 0;
  for (int i = 0; i < N; ++i) {
    ll t;
    cin >> t;
    int x = i + 1 - t;

    if (m.find(x) != m.end()) {
      ans += m[x];
    }

    if (m.find(i + 1 + t) == m.end()) {
      m.emplace(i + 1 + t, 1);
    } else {
      ++m[i + 1 + t];
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3fLL;
constexpr ll mod = 1000000007;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  unordered_multimap<int, int> m;
  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    if ((i + 1) - A[i] > 0) {
      m.emplace((i + 1) - A[i], i);
    }
  }

  int ans = 0;
  for (int i = 0; i < N - 1; ++i) {
    int x = (i + 1) + A[i];
    auto ret = m.equal_range(x);
    for (auto it = ret.first, end = ret.second; it != end; ++it) {
      if (it->second > i) {
        ++ans;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

int main() {
  ll n, ans = 0, x;
  cin >> n;
  unordered_map<ll, ll> mp;
  for (int i = 1; i <= n; ++i)
    cin >> x, ans += mp[i - x], mp[i + x]++;
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int INF = 1e9;

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (a[i] + a[j] == abs(i - j))
        ans++;
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define endl '\\n'

const int MAXN = 10;
const int MOD = 10000000;

int main() {
  ll n;
  cin >> n;
  ll a[n + 1];
  ll x[n + 1], y[n + 1];
  ll qq = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    x[i] = i + a[i];
    y[i] = i - a[i];
    if (a[i] == 0)
      qq++;
  }
  ll ans = 0;
  sort(x + 1, x + n + 1);
  sort(y + 1, y + n + 1);
  for (ll i = 1, j = 1; i <= n && j <= n;) {
    if (x[i] < y[j])
      i++;
    else if (x[i] > y[j])
      j++;
    else {
      ll aa = 1, bb = 1;
      while (x[i + 1] == y[j]) {
        aa++;
        i++;
      }
      while (y[j + 1] == x[i]) {
        bb++;
        j++;
      }
      ans += aa * bb;
      i++;
      j++;
    }
  }
  cout << ans - qq << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define endl '\\n'

const int MAXN = 10;
const int MOD = 10000000;

int main() {
  int n;
  cin >> n;
  int a[n + 1];
  int x[n + 1], y[n + 1];

  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    x[i] = i + a[i];
    y[i] = i - a[i];
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i < j && x[i] == y[j])
        ans++;
      else if (i > j && x[j] == y[i])
        ans++;
    }
  }
  cout << ans / 2 << endl;
  //	for(int i=1;i<=n;i++){
  //		cout<<x[i]<<" "<<y[i]<<endl;
  //	}
  //
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  map<int, ll> m, p;
  for (int i = 1; i <= N; i++) {
    int x;
    cin >> x;
    m[i - x] += 1;
    p[x + i] += 1;
  }
  ll ans = 0;
  for (auto r : p) {
    ans += r.second * m[r.first];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll N;
  cin >> N;
  vector<ll> H(N);
  for (ll &e : H)
    cin >> e;

  ll ans = 0;
  for (ll i = 0; i < N - 1; i++) {
    for (ll j = N - 1; j > i + H[i]; j--) {
      if (H[i] + H[j] == j - i)
        ans += 1;
    }
  }

  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int i, j, N;
  cin >> N;
  long long ans = 0;
  int A, tmp;
  map<int, int> Am;
  vector<int> Ap(N);
  for (i = 0; i < N; i++) {
    cin >> A;
    Ap[i] = A + i;
    if (Am.count(-A + i)) {
      tmp = Am[i - A];
    } else
      tmp = 0;
    tmp++;
    Am[i - A] = tmp;
  }
  for (i = 0; i < N; i++) {
    if (Am.count(Ap[i])) {
      ans += (long long)Am[Ap[i]];
    }
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long long i, j, N;
  cin >> N;
  int ans = 0;
  vector<int> A(N);
  for (i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }
  int M, L;
  for (i = 0; i < N - 1; i++) {
    M = N - i - A[i];
    L = i + A[i] + 1;
    for (j = 1; j < M; j++) {
      if (A[L] == j)
        ans++;
      L++;
    }
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

using namespace std;

ll mod(ll a, ll b) { return (a % b + b) % b; }
ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}

int main() {
  fast;
  ll n;
  cin >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  ll ans = 0;
  ll arr1[n], arr2[n];
  unordered_map<ll, ll> mp1, mp2;
  for (ll i = 0; i < n; i++) {
    arr1[i] = i + arr[i];
    mp1[arr1[i]]++;
  }
  for (ll i = 0; i < n; i++) {
    arr2[i] = arr[i] - i;
    mp2[arr2[i]]++;
  }
  for (ll i = 0; i < n; i++) {
    ll t = i - arr[i];
    ll tt = -(i + arr[i]);
    ans += mp1[t];
    ans += mp2[tt];
  }
  cout << ans / 2 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

using namespace std;

ll mod(ll a, ll b) { return (a % b + b) % b; }
ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}

int main() {
  fast;
  ll n;
  cin >> n;
  ll arr[n];
  for (ll i = 0; i < n; i++)
    cin >> arr[i];
  ll ans = 0;
  for (ll i = 0; i < n - 1; i++) {
    for (ll j = i + 1; j < n; j++) {
      if (abs(j - i) == arr[i] + arr[j])
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define mod 1000000007
#define db(a) cout << a << endl
#define db2(a, b) cout << a << " " << b << endl
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a)                                                                 \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define adb2(a)                                                                \\
  for (auto i : a)                                                             \\
    cout << i.first << " " << i.second << endl;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define N 100010
using namespace std;

int main() {
  fastIO;
  long long n;
  cin >> n;

  long long a[n], i;
  for (i = 0; i < n; i++)
    cin >> a[i];

  map<long long, long long> mp;
  long long ans = 0;
  for (i = 0; i < n; i++) {
    ans += mp[(i + 1) - a[i]];
    mp[a[i] + (i + 1)]++;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define mod 1000000007
#define db(a) cout << a << endl
#define db2(a, b) cout << a << " " << b << endl
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a)                                                                 \\
  for (auto i : a)                                                             \\
    cout << i << " ";                                                          \\
  cout << endl
#define adb2(a)                                                                \\
  for (auto i : a)                                                             \\
    cout << i.first << " " << i.second << endl;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define N 100010
using namespace std;

int main() {
  fastIO;
  long long n;
  cin >> n;

  long long a[n], i;
  for (i = 0; i < n; i++)
    cin >> a[i];

  multiset<long long> diff;
  long long ans = 0;
  for (i = 0; i < n; i++) {
    ans += diff.count((i + 1) - a[i]);
    diff.insert(a[i] + (i + 1));
  }

  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef long double ld;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF ll(1e18)
#define MOD (ll)1e9 + 7
#define print(a)                                                               \\
  ;                                                                            \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << "\\n";
#define mset(a)                                                                \\
  ;                                                                            \\
  memset(a, 0, sizeof(a));

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  ll arr[n];
  map<ll, ll> right;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    right[i - arr[i]]++;
  }
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += right[i + arr[i]];
    right[i - arr[i]]--;
  }
  cout << cnt << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef long double ld;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define INF ll(1e18)
#define MOD (ll)1e9 + 7
#define print(a)                                                               \\
  ;                                                                            \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << "\\n";
#define mset(a)                                                                \\
  ;                                                                            \\
  memset(a, 0, sizeof(a));

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  ll arr[n];
  multiset<ll> right;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    right.insert(i - arr[i]);
  }
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += right.count(i + arr[i]);
    // right.erase(right.find(i-arr[i]));
  }
  cout << cnt << "\\n";
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
#define int long long
signed main() {
  int n;
  cin >> n;

  int a[200000];
  int b[200000];
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    b[i] = i - a[i];
    a[i] = i + a[i];
  }
  sort(a, a + n);
  sort(b, b + n);
  int ans = 0;
  int j = 0;
  for (int i = 0; i < n and j < n; i++) {
    int acount = 0;
    int bcount = 0;
    while (a[i] > b[j] and j < n) {
      j++;
      if (j == n) {
        break;
      }
    }
    if (j == n) {
      break;
    }
    if (b[j] > a[i]) {
      continue;
    } else {

      int s = a[i];
      while (a[i] == s) {
        acount++;
        i++;
        if (i == n) {
          break;
        }
      }
      i--;
      while (b[j] == s) {
        bcount++;
        j++;
        if (j == n) {
          break;
        }
      }
      ans += acount * bcount;
      // cout<<s<<" "<<acount*bcount<<endl;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int a[200000];
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i - a[i]; j++) {
      if (a[i] + a[j] == i - j) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>      //abs()かfabs()で少数絶対値
#include <cstdlib>    //abs()で整数絶対値
#include <functional> //sort第三引数greater<型名>()で降順
#include <iomanip>    //cout<<setw(数字) で空白による桁揃え
#include <iostream>   //cout<<right で右揃え
#include <map>
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  map<ll, ll> sum;
  map<ll, ll> dif;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    sum[i + A[i]]++;
    if (i - A[i] > 1) {
      dif[i - A[i]]++;
    }
  }
  ll ans = 0;
  for (auto i = dif.begin(); i != dif.end(); i++) {
    ans += i->second * sum[i->first];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>      //abs()かfabs()で少数絶対値
#include <cstdlib>    //abs()で整数絶対値
#include <functional> //sort第三引数greater<型名>()で降順
#include <iomanip>    //cout<<setw(数字) で空白による桁揃え
#include <iostream>   //cout<<right で右揃え
#include <queue>
#include <string>
#include <vector>

using namespace std;
using ll = long long int;

ll binary_search(ll xleft, ll xright, ll K, vector<ll> &sum) {
  while (xright - xleft > 1) {
    ll x = (xright + xleft) / 2;
    if (K <= sum[x])
      xright = x;
    else
      xleft = x;
  }
  if (sum[xright] == K)
    return xright;
  else
    return -1;
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> sum;
  vector<ll> dic(N, -1);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum.push_back(A[i] + i + 1);
  }
  sort(sum.begin(), sum.end());
  int cnt = 0;
  for (ll i = 0; i < sum.size() - 1; i++) {
    if (dic[cnt] == -1)
      dic[cnt] = 1;
    if (sum[i] == sum[i + 1]) {
      sum.erase(sum.begin() + i + 1);
      dic[cnt]++;
      i--;
      continue;
    }
    cnt++;
  }
  if (dic[cnt] == -1) {
    dic[cnt] = 1;
  }
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    if ((i + 1) - A[i] <= 1)
      continue;
    ll a = binary_search(-1, sum.size() - 1, i + 1 - A[i], sum);
    if (a != -1) {
      ans += dic[a];
    }
  }
  cout << ans << endl;
}