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

#include <bits/stdc++.h>
#define Dim 200009
using namespace std;
ifstream f("A.in");
int x, N, B[Dim];
unsigned long long ans;

pair<int, int> A[Dim];

multimap<int, int> S;

int CB(int st, int dr, int x, int poz) {
  int sol = 0;
  while (st <= dr) {
    int mij = (st + dr) / 2;
    if (A[mij].first > x)
      dr = mij - 1;
    else if (A[mij].first < x)
      st = mij + 1;
    else if (A[mij].first == x && mij > poz)
      dr = mij - 1;
    else if (A[mij].first == x && mij <= poz) {
      if (mij > sol)
        sol = mij;
      st = mij + 1;
    }
  }
  return sol;
}

bool cmp(pair<int, int> X, pair<int, int> Y) {
  if (X.first < Y.first)
    return 1;
  else if (X.first == Y.first) {
    if (X.second < Y.second)
      return 1;
    else
      return 0;
  } else
    return 0;
}

int CB1(int st, int dr, int x, int poz) {
  int sol = -1;
  while (st <= dr) {
    int mij = (st + dr) / 2;

    if (A[mij].first >= x) {
      dr = mij - 1;
      if (A[mij].first == x)
        sol = mij;
    } else if (A[mij].first < x)
      st = mij + 1;
  }
  if (sol >= poz || sol == -1)
    return 0;
  return sol;
}

int main() {

  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> x;
    A[i].first = x + i;
    A[i].second = i;
    B[i] = i - x;
  }
  sort(A + 1, A + N + 1, cmp);
  //  for(int i=1;i<=N;i++) cout<<A[i].first<<' ';cout<<'\\n';
  // for(int i=1;i<=N;i++) cout<<A[i].second<<' ';cout<<'\\n';
  for (int i = 2; i <= N; i++) {
    int poz1 = CB1(1, i - 1, B[i], i - 1);
    int poz2 = CB(1, i - 1, B[i], i - 1);
    // cout<<B[i]<<' '<<i-1<<' '<<poz1<<' '<<poz2<<'\\n';
    if (poz1 != 0 && poz2 != 0)
      ans += poz2 - poz1 + 1;
  }
  cout << ans;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define Dim 200009
using namespace std;
ifstream f("A.in");
int x, N, A[Dim], B[Dim];
unsigned long long ans;

multiset<int> S;

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> x;
    A[i] = x + i;
    B[i] = i - x;
  }
  for (int i = 2; i <= N; i++) {
    S.insert(A[i - 1]);
    ans += S.count(B[i]);
  }
  cout << ans;

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

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

int main() {

  int N;
  cin >> N;

  map<long long, int> mp;
  long long count = 0;

  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;

    count += mp[i - a];
    mp[i + a]++;
  }

  cout << count << endl;
}

Error: TLE

Fixed submission:

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

int main() {

  int N;
  cin >> N;

  map<int, vector<int>> mp;
  vector<int> H(N);

  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;

    H[i] = a;

    for (int j = 0; j < i; j++) {
      if (H[j] + j == i - a)
        mp[i - a].push_back(j);
    }
  }
  int count = 0;

  for (auto itr = mp.begin(); itr != mp.end(); itr++) {
    int sum = itr->first;
    count += mp[sum].size();
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define LIMIT 1000000000

int main(int argc, char **argv) {
  long N;
  cin >> N;
  vector<long> A;
  map<long, long> L, R;
  for (long i = 0; i < N; ++i) {
    long a;
    cin >> a;
    A.push_back(a);

    long l, r;
    l = i + a;
    r = i - a;

    if (!L.count(l))
      L[l] = 0;
    if (!R.count(r))
      R[r] = 0;

    L[l]++;
    R[r]++;
  }

  long cnt = 0;
  for (auto &l : L) {
    if (R.count(l.first)) {
      cnt += l.second * R[l.first];
    }
  }

  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

int main(int argc, char **argv) {
  int N;
  cin >> N;
  vector<long> A;
  for (int i = 0; i < N; ++i) {
    long a;
    cin >> a;
    A.push_back(a);
  }

  long cnt = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      if (abs(i - j) == (A[i] + A[j]))
        cnt++;
    }
  }
  cnt /= 2;

  cout << cnt << endl;

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

#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define PI 3.141592653589793238
#define mod 1000000007
#define endl '\\n'
#define fastio                                                                 \\
  ;                                                                            \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;

int main() {
  fastio;
  int n;
  cin >> n;
  vi h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vi neg(n), pos(n);
  map<int, vector<int>> MP;
  for (int i = 0; i < n; i++) {
    neg[i] = i - h[i], pos[i] = i + h[i];
    MP[neg[i]].pb(i);
  }
  ll prs = 0;
  for (int i = 0; i < n - 1; i++) {
    if (MP[pos[i]].size() == 0)
      continue;
    int k = MP[pos[i]].size();
    int idx = lower_bound(MP[pos[i]].begin(), MP[pos[i]].end(), pos[i]) -
              MP[pos[i]].begin();
    prs += (k - idx);
  }
  cout << prs << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define PI 3.141592653589793238
#define mod 1000000007
#define endl '\\n'
#define fastio                                                                 \\
  ;                                                                            \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;

int main() {
  fastio;
  int n;
  cin >> n;
  vi h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vi neg(n), pos(n);
  map<int, vector<int>> MP;
  for (int i = 0; i < n; i++) {
    neg[i] = i - h[i], pos[i] = i + h[i];
    MP[neg[i]].pb(i);
  }
  ll prs = 0;
  for (int i = 0; i < n - 1; i++) {
    if (MP[pos[i]].size() == 0)
      continue;

    for (int j = 0; j < MP[pos[i]].size(); j++) {
      if (MP[pos[i]][j] > i)
        prs++;
    }

    // int k=MP[pos[i]].size();
    // int idx=
  }
  cout << prs << endl;

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

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; i < (n); i++)
#define REP(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define MOD 1000000007
#define fcout cout << fixed << setprecision(15)
#define int long long
#define yorn(f) puts((f) ? "Yes" : "No")
#define YORN(f) puts((f) ? "YES" : "NO")
typedef long long ll;
typedef pair<int, int> P;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; };
int lcm(int a, int b) { return a / gcd(a, b) * b; };
int mod(int a, int b) { return (a + b - 1) / b; };
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
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;
};

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

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

  int ans = 0;
  map<int, int> mp;
  rep(i, n) {
    ans += mp[i - a[i]];
    mp[i + a[i]]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; i < (n); i++)
#define REP(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define MOD 1000000007
#define fcout cout << fixed << setprecision(15)
#define int long long
#define yorn(f) puts((f) ? "Yes" : "No")
#define YORN(f) puts((f) ? "YES" : "NO")
typedef long long ll;
typedef pair<int, int> P;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; };
int lcm(int a, int b) { return a / gcd(a, b) * b; };
int mod(int a, int b) { return (a + b - 1) / b; };
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}
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;
};

signed main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int ans = 0;
  rep(i, n) {
    REP(j, i + 1, n) {
      if (a[i] + a[j] == j - i) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
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;
using ll = long long;
using P = pair<int, int>;

// i<j
// j-i = a[i]+a[j]
// a[i]+i = j-a[j]

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  ll ans = 0;
  rep(i, n) {
    int sa = i - a[i];
    int wa = a[i] + i;
    ans += mp[sa];
    mp[wa]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  vector<int> vec(n);
  for (int i = 0; i < n; i++) {
    cin >> vec.at(i);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i >= j)
        continue;
      if (abs(i - j) == vec.at(i) + vec.at(j))
        cnt++;
    }
  }

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

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <limits>
#include <unordered_map>
#include <utility>
#include <vector>

typedef long long ll;

int main(int argc, char *argv[]) {
  int n;

  std::cin >> n;

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

  std::unordered_map<int, int> L;

  for (int i = 0; i < n; i++) {
    const int x = i + a[i];
    L[x]++;
  }

  ll ans = 0;
  for (int j = 0; j < n; j++) {
    int x = j - a[j];
    if (x < 1) {
      continue;
    }
    ans += L[x];
  }
  std::cout << ans << std::endl;

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <limits>
#include <utility>
#include <vector>

typedef long long ll;
typedef std::pair<int, int> pii;

int main(int argc, char *argv[]) {
  int n;

  std::cin >> n;

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

  std::vector<pii> L;
  L.reserve(n + 2);
  L.push_back({std::numeric_limits<int>::min(), -1});
  L.push_back({std::numeric_limits<int>::max(), -1});

  for (int i = 0; i < n; i++) {
    const int x = i + a[i];
    L.push_back({x, i});
  }

  std::sort(L.begin(), L.end());

  ll ans = 0;
  for (int j = 0; j < n; j++) {
    int x = j - a[j];
    if (x < 1) {
      continue;
    }
    auto it_start = std::lower_bound(
        L.begin(), L.end(), pii{x, 0},
        [](const auto &a, const auto &b) { return a.first < b.first; });
    const auto it_end = L.end();
    for (auto it = it_start; it != it_end && it->first == x; it++) {
      const int i = it->second;
      if (i < j) {
        ans++;
      }
    }
  }

  std::cout << ans << std::endl;

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

#include <iostream>
using namespace std;

#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

int main() {

  long long int n;
  cin >> n;

  long long int ans = 0;
  map<long long int, long long int> v;

  for (long long int i = 0; i < n; i++) {
    long long int in;
    cin >> in;
    ans += v[i - in];
    v[in + i]++;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#include <algorithm>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

int main() {

  long long int n;
  cin >> n;

  long long int ans = 0;
  multiset<long long int> v;

  for (long long int i = 0; i < n; i++) {
    long long int in;
    cin >> in;
    ans += v.count(i - in);
    v.insert(in + i);
  }

  cout << ans << endl;

  return 0;
}
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;

int main() {
  long n, ans = 0;
  cin >> n;
  map<int, long> mp;
  rep(i, n) {
    int a;
    cin >> a;
    if (i - a > 0 && mp.find(i - a) != mp.end())
      ans += mp[i - a];
    mp[a + i]++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n, ans = 0;
  cin >> n;
  vector<int> a(n), tj(n);
  vector<pair<int, int>> ti(n);
  vector<vector<int>> dp(n, vector<int>(n, 0));
  rep(i, n) {
    cin >> a.at(i);
    ti.at(i).first = a.at(i) + i;
    ti.at(i).second = i;
    tj.at(i) = i - a.at(i);
  }
  sort(ti.begin(), ti.end());
  rep(i, n - 1) {
    auto it = upper_bound(ti.begin(), ti.end(), make_pair(tj.at(i + 1), i)) -
              lower_bound(ti.begin(), ti.end(), make_pair(tj.at(i + 1), 0));
    ans += (int)it;
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(void) {
  int N;
  long ans = 0;
  cin >> N;
  map<int, int> Li;
  rep(i, N) {
    int Ai;
    cin >> Ai;
    Li[i + 1 + Ai]++;
    ans += Li[i + 1 - Ai];
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(void) {
  int N, ans = 0;
  cin >> N;
  int Aii[N];
  rep(i, N) {
    int Ai;
    cin >> Ai;
    rep(k, i) {
      if (Ai - i - 1 == Aii[k])
        ans++;
    }
    Aii[i] = -i - 1 - Ai;
  }
  cout << ans << 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() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  for (ll i = 0; i < n; ++i)
    a[i] -= i;
  set<ll> s;
  map<ll, ll> m;
  for (ll x : a) {
    ++m[x];
  }
  ll ans = 0;
  for (ll i = 0; i < n - 1; ++i) {
    ll target = -a[i] - 2 * i;
    ans += m[target];
    --m[a[i]];
  }
  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> a(n);
  for (ll i = 0; i < n; ++i)
    cin >> a[i];
  for (ll i = 0; i < n; ++i)
    a[i] -= i;
  multiset<ll> s;
  for (ll x : a)
    s.insert(x);
  ll ans = 0;
  for (ll i = 0; i < n - 1; ++i) {
    ll target = -a[i] - 2 * i;
    ans += s.count(target);
    s.erase(s.find(a[i]));
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (size_t i = 0; i < (size_t)(n); i++)

using namespace std;
using ll = long long;

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

  auto as = vector<ll>(n);
  for (int i = 0; i < n; i++)
    cin >> as[i];

  auto addc = map<ll, vector<int>>();
  auto subc = map<ll, vector<int>>();
  REP(i, n) {
    addc[i + as[i]].push_back(i);
    subc[i - as[i]].push_back(i);
  }

  for (auto &p : addc) {
    sort(ALL(p.second));
    // cout << p.first << ": ";
    // REP(i, p.second.size()) {
    //   cout << p.second[i] << " ";
    // }
    // cout << endl;
  }
  for (auto &p : subc) {
    sort(ALL(p.second));
    // cout << p.first << ": ";
    // REP(i, p.second.size()) {
    //   cout << p.second[i] << " ";
    // }
    // cout << endl;
  }

  ll ans = 0;
  for (const auto &p : subc) {
    if (!addc.count(p.first))
      continue;
    // cout << p.first << endl;

    REP(i, p.second.size()) {
      ans += distance(addc[p.first].begin(),
                      lower_bound(ALL(addc[p.first]), p.second[i]));
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

int main(void) {
  int n;
  cin >> n;
  auto a = vector<ll>(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int ans = 0;
  for (int i = 0; i < n; i++) {
    // cout << "i: " << i << endl;
    for (ll j = i - a[i]; j >= 0; j--) {
      // cout << i << ", " << j << endl;
      if (abs(i - j) == a[i] + a[j])
        ans++;
    }
  }
  cout << ans << endl;
}
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 < (int)(n); i++)

int main() {
  int N;
  long long Sum = 0;
  cin >> N;
  vector<long long> A(N);
  map<long long, int> M;
  rep(i, N) cin >> A.at(i);
  rep(i, N) M[i + A.at(i)]++;
  rep(i, N) Sum += M[i - A.at(i)];
  cout << Sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int N, Sum = 0;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) cin >> A.at(i);
  rep(i, N - 1) {
    if (A.at(i) >= N - 1)
      continue;
    for (int j = i + A.at(i) + 1; j < N; j++) {
      if (A.at(j) >= N - 1)
        continue;
      if (A.at(i) + A.at(j) == abs(j - i))
        Sum++;
    }
  }
  cout << Sum << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar gc
inline char gc() {
  static char buf[1 << 16], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 1 << 16, stdin);
    if (p2 == p1)
      return EOF;
  }
  return *p1++;
}
#endif
template <class t> inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c))
    f |= c == '-', c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return x;
}
template <class t> inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9)
      write(x / 10);
    putchar('0' + x % 10);
  }
}

const int N = 2e5 + 5;
long long ans;
int n, a[N], cnt[N];

signed main() {
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = n; i; i--) {
    if (a[i] + i <= n)
      ans += cnt[a[i] + i];
    if (i >= a[i])
      cnt[i - a[i]]++;
  }
  write(ans);
}

Error: WA

Fixed submission:

#pragma GCC optimize(3, "Ofast", "inline")
#pragma GCC target("avx")
#include <bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
#define getchar gc
inline char gc() {
  static char buf[1 << 16], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 1 << 16, stdin);
    if (p2 == p1)
      return EOF;
  }
  return *p1++;
}
#endif
template <class t> inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c))
    f |= c == '-', c = getchar();
  while (isdigit(c))
    x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f)
    x = -x;
  return x;
}
template <class t> inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9)
      write(x / 10);
    putchar('0' + x % 10);
  }
}

const int N = 1e5 + 5;
long long ans;
int n, a[N], cnt[N];

signed main() {
  read(n);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = n; i; i--) {
    if (a[i] + i <= n)
      ans += cnt[a[i] + i];
    if (i >= a[i])
      cnt[i - a[i]]++;
  }
  write(ans);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  map<int, int> m;
  for (int i = 0; i < N; i++) {
    cin >> h[i];
    m[i - h[i]]++;
  }
  long long count = 0;
  for (int i = 0; i < N; i++) {
    count += m[i + h[i]];
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  int count = 0;
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (j - i == v[j] + v[i])
        count++;
    }
  }
  cout << count;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1'000'000'007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  map<ll, ll> key;
  ll res = 0;
  REP(i, n) {
    res += key[i - a[i]];
    key[i + a[i]] += 1;
  }

  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1'000'000'007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = a; (ll)(b) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  multiset<ll> fv, bv;
  REP(i, n) {
    fv.insert(-a[i] + (i + 1));
    bv.insert(-a[i] - (i + 1));
  }

  ll res = 0;
  REP(i, n) {
    ll obj = a[i] + (i + 1);
    ll num = fv.count(obj);

    if (num) {
      res += num;
    }
    obj = a[i] - (i + 1);
    num = bv.count(obj);

    if (num) {
      res += num;
    }
  }
  cout << res / 2 << endl;
}