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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = (s); i < (n); i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define REPR(i, s, n) for (int i = (s); i >= (n); i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define pf push_front
const int mod = 1e9 + 7;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
map<ll, ll> mpl, mpr;
int main() {
  ll n;
  cin >> n;
  rep(i, n) {
    ll a;
    cin >> a;
    mpl[i - a]++;
    mpr[i + a]++;
  }
  ll ans = 0;
  for (auto it = mpl.begin(); it != mpl.end(); it++) {
    ll tmp = it->second;
    ans += tmp * mpr[it->first];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, s, n) for (int i = (s); i < (n); i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
#define REPR(i, s, n) for (int i = (s); i >= (n); i--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define pf push_front
const int mod = 1e9 + 7;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
int main() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> v1(n), v2(n);
  set<pair<ll, ll>> st;
  rep(i, n) {
    ll a;
    cin >> a;
    v1[i] = make_pair(i - a, i);
    v2[i] = make_pair(i + a, i);
  }
  sort(all(v1));
  sort(all(v2));
  /*rep(i,n)cout << v1[i].first << " " << v1[i].second << endl;
  cout << endl;
  rep(i,n)cout << v2[i].first << " " << v2[i].second << endl;
  */
  rep(i, n) {
    pair<ll, ll> tmp = make_pair(v1[i].first, -1);
    auto it = lower_bound(all(v2), tmp);
    for (; it != v2.end(); it++) {
      if (it->first != tmp.first)
        break;
      ll p = max(v1[i].second, it->second);
      ll q = min(v1[i].second, it->second);
      st.insert(make_pair(q, p));
    }
  }
  cout << st.size() << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long x;
  vector<long long> v;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  vector<long long> small, large;
  for (int j = 0; j < v.size() - 1; j++) {
    small.push_back(j + v[j]);
    large.push_back(j + 1 - v[j + 1]);
  }
  map<pair<long long, long long>, int> mp;
  map<long long, int> mp2, mp3;
  for (int h = 0; h < large.size(); h++) {
    mp2[large[h]]++;
    mp[{h, large[h]}] += mp2[large[h]];
  }
  for (int h = 0; h < large.size(); h++) {
    mp3[large[h]]++;
  }
  long long total = 0;
  for (int k = 0; k < small.size(); k++) {
    total += mp3[small[k]] - mp[{k, small[k]}];
    if (small[k] == 0) {
      total--;
    }
  }

  cout << total;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  long long x;
  vector<long long> v;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  vector<long long> small, large;
  for (int j = 0; j < v.size() - 1; j++) {
    small.push_back(j + v[j]);
    large.push_back(j + 1 - v[j + 1]);
  }
  int total = 0;
  for (int k = 0; k < small.size(); k++) {
    total += count(large.begin() + k, large.end(), small[k]);
  }

  cout << total;

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

#include "bits/stdc++.h"
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double EPS = 1e-9;
#define rep(i, n) REP(i, 0, n) clim
#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rrep(i, n) RREP(i, 0, n)
#define RREP(i, x, n) for (int i = (int)(n)-1 - (int)(x); i >= 0; i--)

vector<int> A(10e6 + 1);

int main(void) {
  // Your code here!
  int N;
  cin >> N;
  REP(i, 1, N + 1) {
    int h;
    cin >> h;
    A[i] = h;
  }
  ll count = 0;
  map<int, int> mp;
  REP(i, 1, N + 1) {
    // こうすることでi==jのときを弾ける
    count += mp[i - A[i]];
    mp[i + A[i]]++;
  }
  // REP(i,1,N+1){
  //     int r = i-A[i];
  //     count += mp[r];
  // }

  // REP(i,1,N+1){
  //     if (i+A[i] > N) continue;
  //     REP(j,i+A[i],N+1){
  //         if(j-i == A[i]+A[j]) count++;
  //     }
  // }
  cout << count;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double EPS = 1e-9;
const double INT_MAX = 2147483647;
#define rep(i, n) REP(i, 0, n) clim
#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rrep(i, n) RREP(i, 0, n)
#define RREP(i, x, n) for (int i = (int)(n)-1 - (int)(x); i >= 0; i--)

vector<int> A(10e6 + 1);

int main(void) {
  // Your code here!
  int N;
  cin >> N;
  REP(i, 1, N + 1) {
    int h;
    cin >> h;
    A[i] = h;
  }
  int count = 0;
  REP(i, 1, N + 1) {
    if (i + A[i] > N)
      continue;
    REP(j, i + A[i], N + 1) {
      if (j - i == A[i] + A[j])
        count++;
    }
  }
  cout << count;
}
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 vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define mod 1000000007
using ll = long long;
using namespace std;
int main() {
  int n;
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> cnt;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans += cnt[i - a[i]];
    cnt[i + a[i]]++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define mod 1000000007
using ll = long long;
using namespace std;
int main() {
  int n;
  cin >> n;
  vi a(n);
  ll ans = 0;
  rep(i, n) cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (j - i == a[i] + a[j])
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

int64_t b[200000000] = {0};
int64_t a[2000000];
int main() {
  int64_t n;
  scanf("%lld", &n);

  int64_t i;
  int64_t sum = 0;
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] <= i)
      b[(i - a[i])]++;
  }
  for (i = 1; i <= n; i++)
    if (a[i] + i < 2000000)
      sum = sum + b[a[i] + i];

  cout << sum << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

int64_t b[200000000] = {0};
int64_t a[2000000];
int main() {
  int64_t n;
  scanf("%lld", &n);

  int64_t i;
  int64_t sum = 0;
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] <= i)
      b[(i - a[i])]++;
  }
  for (i = 1; i <= n; i++)
    sum = sum + b[a[i] + i];

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> mp1, mp2;
  rep(i, n) {
    mp1[i + a[i]]++;
    mp2[i - a[i]]++;
  }
  ll ans = 0;
  for (auto p : mp1)
    ans += p.second * mp2[p.first];
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep1(i, n) for (ll i = 1; i < n + 1; i++)
#define sort(A) sort(A.begin(), A.end())
#define reverse(A) reverse(A.begin(), A.end());
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n) {
    int start = i + a[i];
    for (ll j = 1; start + j < n; j++) {
      if (a[start + j] == j)
        ans++;
    }
    // cout << endl;
  } // i
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define _LL long long

int *mallocint(int si) { return (int *)malloc(si * sizeof(int)); }
int *_p;

int main() {
  int n;
  cin >> n;
  int *a = mallocint(n);
  int *l = mallocint(n);
  int *r = mallocint(n);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
    l[i] = a[i] + i;
    r[i] = -a[i] + i;
  }

  _p = l;
  sort(l, l + n);
  sort(r, r + n);

  int i = 0;
  int j = 0;
  _LL ans = 0;
  do {
    if (l[i] == r[j]) {
      _LL lcount = 1;
      while (i + lcount < n && l[i] == l[i + lcount])
        lcount++;
      _LL rcount = 1;
      while (j + rcount < n && r[j] == r[j + rcount])
        rcount++;
      ans += lcount * rcount;

      i += lcount;
      j += rcount;
    } else {
      if (l[i] < r[j]) {
        i++;
      } else {
        j++;
      }
    }
    /* code */
  } while (i < n && j < n);
  cout << ans;
}

Error: TLE

Fixed submission:

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

int *mallocint(int si) { return (int *)malloc(si * sizeof(int)); }

int main() {
  int n;
  cin >> n;
  int *a = mallocint(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int ans = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (j - i == a[i] + a[j])
        ans++;
    }
  }
  cout << ans;
}
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;

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

  map<int, int> cnt;

  ll ans = 0;
  rep(i, n) {
    ans += cnt[i - a[i]];
    cnt[a[i] + i]++;
  }

  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;

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

  ll ans = 0;
  for (int d = 2; d <= n - 1; d++) {
    rep(i, d) {
      for (int j = i; j + d < n; j += d) {
        if (a[j] + a[j + d] == d)
          ans++;
      }
    }
  }

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

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define pb push_back
#define mpr make_pair
#define pii pair<int, int>
#define ll long long
#define pll pair<ll, ll>
#define all(arr) arr.begin(), arr.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define INF 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846264338327950L
#define N 1000005
#define mid(l, r) l + (r - l) / 2
#define vec vector<int>
#define vecl vector<ll>
#define umap unordered_map<ll, ll>
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define endl "\\n"
using namespace std;

void solve() {

  int n;
  cin >> n;

  vec arr(n + 1);
  fo(i, 1, n) { cin >> arr[i]; }

  umap mp;
  mp[arr[1] + 1]++;

  ll ans = 0;

  for (int i = 2; i <= n; i++) {

    int val = i - arr[i];

    ans += mp[val];

    mp[arr[i] + i]++;
  }

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#define initdp(a, b)                                                           \\
  for (int i = 0; i <= a; i++)                                                 \\
    for (int j = 0; j <= b; j++)                                               \\
      dp[i][j] = -1;
#define pb push_back
#define mpr make_pair
#define pii pair<int, int>
#define ll long long
#define pll pair<ll, ll>
#define all(arr) arr.begin(), arr.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repd(i, n) for (int i = n - 1; i >= 0; i--)
#define fo(i, l, r) for (int i = l; i <= r; i++)
#define INF 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846264338327950L
#define N 1000005
#define mid(l, r) l + (r - l) / 2
#define vec vector<int>
#define vecl vector<ll>
#define umap unordered_map<ll, ll>
#define yes cout << "Yes" << endl;
#define no cout << "No" << endl;
#define endl "\\n"
using namespace std;

void solve() {

  int n;
  cin >> n;

  vec arr(n + 1);
  fo(i, 1, n) { cin >> arr[i]; }

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

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(20);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

// -----------------------------------
// Author     : MatsuTaku
// Country    : Japan
// Created    : 05/03/20 20:34:41
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  map<ll, int> A;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    ans += A[i - a];
    A[i + a]++;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

// -----------------------------------
// Author     : MatsuTaku
// Country    : Japan
// Created    : 05/03/20 20:34:41
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  multiset<ll> A;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll a;
    cin >> a;
    ans += A.count(i - a);
    A.insert(i + a);
  }
  cout << ans << endl;

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }

ll a[220000];
ll x[410000];
ll y[410000];
int main() {
  ll n, k, ans = 0;
  cin >> n;
  rep(i, 1, n + 1) cin >> a[i];
  rep(i, 1, n + 1) if (i + a[i] <= 4e5) x[a[i] + i]++;
  rep(i, 1, n + 1) if (i - a[i] <= 4e5 && i >= a[i]) y[i - a[i]]++;
  rep(i, 0, 4e5 + 1) ans += x[i] * y[i];
  cout << ans;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
using namespace std;
ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }

ll a[220000];
ll x[220000];
ll y[220000];
int main() {
  ll n, k, ans = 0;
  cin >> n;
  rep(i, 1, n + 1) cin >> a[i];
  rep(i, 1, n + 1) x[a[i] + i]++;
  rep(i, 1, n + 1) if (i >= a[i]) y[i - a[i]]++;
  rep(i, 0, n + 1) ans += x[i] * y[i];

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

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

// 解説資料：https://img.atcoder.jp/abc166/editorial.pdf
// 変数分離により、計算量を減らしている
// 連想配列の意識も足りなかった（思いつく可能性はあったと思う）

#define NPERSON 200000

int main() {
  // 入力
  int nPerson;
  cin >> nPerson;
  int a[NPERSON + 1];
  for (int i = 1; i <= nPerson; i++) {
    cin >> a[i];
  }

  // i + A[i] を求め、連想配列で格納する（i + A[i] を満たす i
  // の数を配列に記録する）
  int cntList[NPERSON + 1]; //
  for (int i = 1; i <= nPerson; i++) {
    cntList[i] = 0;
  }

  for (int i = 1; i <= nPerson; i++) {
    int tmp = i + a[i];
    if (tmp > nPerson) {
      continue;
    }
    cntList[tmp]++;
  }

  // j - A[j] を求め、その値の連想配列へアクセスすることで、条件を満たす (i +
  // A[i] = j - A[j] となる) ペア数がわかるので、その値を加算する
  long cnt = 0;
  for (int i = 1; i <= nPerson; i++) {
    int tmp = i - a[i];
    if (tmp <= 0) {
      continue;
    }
    cnt += (long)cntList[tmp];
  }

  // 出力
  cout << cnt << endl;

  return 0;
}


Error: 0

Fixed submission:

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

// 解説資料：https://img.atcoder.jp/abc166/editorial.pdf
// 変数分離により、計算量を減らしている

#define AMAX 1000000000
#define NPERSON 100000

int main() {
  // 入力
  int nPerson;
  cin >> nPerson;
  int a[NPERSON + 1];
  for (int i = 1; i <= nPerson; i++) {
    cin >> a[i];
  }

  // i + A[i] を求め、連想配列で格納する（i + A[i] を満たす i
  // の数を配列に記録する）
  int list[NPERSON + 1]; //
  for (int i = 1; i <= nPerson; i++) {
    list[i] = 0;
  }

  for (int i = 1; i <= nPerson; i++) {
    int tmp = i + a[i];
    if (tmp > nPerson) {
      continue;
    }
    list[tmp]++;
  }

  // j - A[j] を求め、その値の連想配列へアクセスすることで、条件を満たす (i +
  // A[i] = j - A[j] となる) ペア数がわかるので、その値を加算する
  int cnt = 0;
  for (int i = 1; i <= nPerson; i++) {
    int tmp = i - a[i];
    if (tmp <= 0) {
      continue;
    }
    cnt += list[tmp];
  }

  // 出力
  cout << cnt << endl;

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  map<long long, long long> C;
  vector<long long> B;
  for (int i = 0; i < n; i++) {
    long long a, b, c;
    cin >> a;
    b = a + i + 1;
    c = i + 1 - a;
    B.push_back(b);
    if (!C.count(c)) {
      C[c] = 1;
    } else {
      C.at(c)++;
    }
  }
  long long cnt = 0;
  for (long long b : B) {
    if (C.count(b))
      cnt += C.at(b);
  }
  cout << cnt << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main(int, char **) {
  int n;
  cin >> n;
  vector<long long> A;
  int diff = 0;
  for (int i = 0; i < n; i++) {
    long long a;
    cin >> a;
    if (a > n) {
      diff++;
      continue;
    } else {
      A.push_back(a - diff);
    }
  }
  n = n - diff;
  long long count = 0;
  for (int s = 0; s < n; s++) {
    long long a = A[s];
    for (int i = s + a + 1; i < n; i++) {
      // cout << a << " " << i << endl;
      if (A[i] == i - s - a)
        count++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) { cin >> a.at(i); }

  map<ll, ll> count;

  ll ans = 0;
  REP(i, n) {
    ans += count[i - a.at(i)];
    count[i + a.at(i)]++;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define ALL(v) (v).begin(), (v).end()

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) { cin >> a.at(i); }

  vector<ll> maxval(n, 0);

  ll ans = 0;
  REP(i, n) {
    const ll ai = a[i];
    for (ll d = ai + 1; d <= n - 1 && i - d >= 0 && maxval[i - d] >= d - ai;
         ++d) {
      if (a[i - d] == d - ai) {
        ans++;
      }
    }

    if (i == 0) {
      maxval.at(i) = ai;
    } else {
      maxval.at(i) = max(maxval.at(i - 1), ai);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  ll a[n];
  map<ll, int> l;
  map<ll, int> r;
  ll max = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ll k = (i + 1) - a[i];
    auto itr = l.find(k);
    if (itr == l.end()) {
      l[k] = 1;
    } else {
      l[k]++;
    }
  }

  ll ans = 0;

  for (int i = 0; i < n; i++) {
    ll k = (i + 1) + a[i];
    auto itr = l.find(k);
    if (itr != l.end()) {
      ans += itr->second;
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

  ll a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  ll ans = 0;

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

  cout << ans << endl;
}