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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
inline ll read() {
  ll kk = 0, f = 1;
  char cc = getchar();
  while (cc < '0' || cc > '9') {
    if (cc == '-')
      f = -1;
    cc = getchar();
  }
  while (cc >= '0' && cc <= '9') {
    kk = (kk << 1) + (kk << 3) + cc - '0';
    cc = getchar();
  }
  return kk * f;
}
inline void write(ll xx) {
  if (xx < 0) {
    putchar('-');
    xx = -xx;
  }
  if (xx > 9)
    write(xx / 10);
  putchar(xx % 10 + '0');
  return;
}
map<pair<ll, int>, ll> mp;
ll a[200005];
int n;
int main() {
  n = read();
  ll res = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    mp[make_pair(a[i] + i, 0)]++;
    mp[make_pair(i - a[i], 1)]++;
  }
  for (int i = 1; i <= n; i++) {
    if (mp[make_pair(a[i] + i, 1)]) {
      res += (mp[make_pair(i + a[i], 0)] * mp[make_pair(i + a[i], 1)]);
      mp[make_pair(a[i] + i, 1)] = 0;
      mp[make_pair(a[i] + i, 0)] = 0;
    } else if (mp[make_pair(i - a[i], 0)]) {
      res += (mp[make_pair(i - a[i], 0)] * mp[make_pair(i - a[i], 1)]);
      mp[make_pair(i - a[i], 0)] = 0;
      mp[make_pair(i - a[i], 1)] = 0;
    }
  }
  write(res);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
inline ll read() {
  ll kk = 0, f = 1;
  char cc = getchar();
  while (cc < '0' || cc > '9') {
    if (cc == '-')
      f = -1;
    cc = getchar();
  }
  while (cc >= '0' && cc <= '9') {
    kk = (kk << 1) + (kk << 3) + cc - '0';
    cc = getchar();
  }
  return kk * f;
}
inline void write(ll xx) {
  if (xx < 0) {
    putchar('-');
    xx = -xx;
  }
  if (xx > 9)
    write(xx / 10);
  putchar(xx % 10 + '0');
  return;
}
ll vis[200005][2];
ll a[200005];
int n;
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    vis[i][0] = i - a[i];
    vis[i][1] = i + a[i];
  }
  ll res = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (vis[i][0] == vis[j][1] || vis[i][1] == vis[j][0]) {
        res++;
      }
    }
  }
  write(res);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

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

  vector<long long> A(N);
  map<long long, int> mp;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i] + i]++;
  }

  long long ans = 0;
  for (int j = 0; j < N; j++) {
    ans += mp[j - A[j]];
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
const long long LINF = 1000000000000000018ll;
const int INF = 1000000009;
const int mod = 1000000007;
const int mod2 = 998244353;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};

int main() {
  std::cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::setprecision(20) << std::fixed;

  int N;
  cin >> N;

  vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  long long cnt = 0;
  for (int dif = 2; dif <= N - 1; dif++) {
    for (int l = 0; l + dif < N; l++) {
      int r = l + dif;
      if (A[l] + A[r] == dif) {
        cnt++;
      }
    }
  }
  cout << cnt << endl;

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

#include <bits/stdc++.h>

using namespace std;
#define DBG(x)                                                                 \\
  (void)(cout << "L" << __LINE__ << ": " << #x << " = " << (x) << '\\n')
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 110000;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setiosflags(ios::fixed) << setprecision(12);
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  vector<ll> b(n + 1);
  map<ll, int> mp;
  for (int i = 1; i <= n; i++) {
    b[i] = i - a[i];
    mp[a[i] + i]++;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ll cur = b[i];
    ans += mp[cur];
  }
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define DBG(x)                                                                 \\
  (void)(cout << "L" << __LINE__ << ": " << #x << " = " << (x) << '\\n')
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = 110000;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setiosflags(ios::fixed) << setprecision(12);
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = i + 1; j <= n; j++) {
      ll cur1 = a[i];
      ll cur2 = a[j];
      if (cur1 + cur2 == j - i)
        ans++;
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 100000000;
const ll half_inf = 0x3f3f3f3f;
const ll inf = 0x7fffffff;
const ll mod = 998244353;
const ll maxn = 2000005;
int a[200005];
map<int, int> m;
int main() {
  int n;
  ll ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (i > a[i])
      ans += m[i - a[i]];
    m[a[i] + i]++;
  }
  printf("%lld", ans);

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 100000000;
const ll half_inf = 0x3f3f3f3f;
const ll inf = 0x7fffffff;
const ll mod = 998244353;
const ll maxn = 2000005;
int a[200005], b[400005] = {0};
int main() {
  int n;
  ll ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (i > a[i])
      ans += b[i - a[i]];
    b[a[i] + i]++;
  }
  printf("%lld", ans);

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

#include "bits/stdc++.h"
#include <unordered_map>
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for (LLI i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (LLI i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

int main() {
  cout << fixed << setprecision(10);

  unordered_map<LLI, vector<LLI>> m(220000);

  LLI n;
  cin >> n;
  vector<LLI> a(n);

  REP(i, n) {
    cin >> a[i];

    if (m.count(a[i] - i) == 0) {
      m[a[i] - i] = {};
    }

    m[a[i] - i].push_back(i);
  }

  LLI ans = 0;
  REP(i, n) {
    if (m.count(-(a[i] + i)) == 0)
      continue;

    auto &ve = m[-(a[i] + i)];

    LLI add = ve.end() - upper_bound(ve.begin(), ve.end(), i);
    ans += add;
  }

  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#define dlog(str) cout << "====" << str << endl;
#else
#define dlog(str)
#endif
#define INF 999999999
#define MOD 1000000007
#define REP(i, n) for (int i = 0, i##_l = (n); i < i##_l; i++)
#define FOR(i, s, e) for (int i = s, i##_l = (e); i < i##_l; i++)
#define LLI long long int
#define _min(a, b) ((a < b) ? a : b)
#define _max(a, b) ((a < b) ? b : a)
#define chmax(a, b) a = _max(a, b)
#define chmin(a, b) a = _min(a, b)
#define bit(a, shift) ((a>>shift)&1))
#define pm(a) ((a) ? 1 : -1)
#define SORT(v) sort(v.begin(), v.end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
// int 2.14E±9    lli 9.2E±18    double 1.7E±380

int main() {
  cout << fixed << setprecision(10);

  int n;
  cin >> n;
  vector<int> a(n);

  REP(i, n) { cin >> a[i]; }

  vector<int> next(n, -1);

  int pre = -1;
  for (int i = n - 2; 0 <= i; i--) {
    if (a[i] >= a[i + 1]) {
      pre = i + 1;
    }
    next[i] = pre;
  }

  int ans = 0;
  REP(i, n) {
    FOR(j, i + a[i], n) {
      if (a[i] + a[j] > j - i) {
        j = next[j];
        if (j == -1)
          break;
      }

      if (a[i] + a[j] == j - i) {
        ans++;
      }
    }
  }
  cout << ans;

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

#include <bits/stdc++.h>
#define lint long long
#define st first
#define nd second

using namespace std;

// int t;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // cin>>t;
  // while(t--){}

  int n;
  cin >> n;

  lint cont = 0;
  map<lint, lint> freq;
  for (lint i = 1; i <= n; i++) {
    lint x;
    cin >> x;
    cont += freq[i - x];
    freq[x + i]++;
  }
  cout << cont << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lint long long
#define st first
#define nd second

using namespace std;

// int t;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // cin>>t;
  // while(t--){}

  int n;
  cin >> n;
  vector<lint> v(n);
  for (lint &x : v)
    cin >> x;

  int cont = 0;
  for (lint dif = 1; dif < n; dif++) {
    for (lint c = 0; c < dif; c++) {
      for (lint i = c + dif; i < n; i += dif) {
        //	cerr<<"v["<<i<<"]+ v["<<i-dif<<"] = "<<v[i]+v[i-dif]<<"\\n";
        if (v[i] + v[i - dif] == dif)
          cont++;
      }
    }
  }
  cout << cont << "\\n";

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

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  map<int, set<int>> L, R;
  int LL, RR;
  for (int i = 0; i < N; i++) {
    LL = (i + 1) + A.at(i);
    RR = (i + 1) - A.at(i);
    if (L.count(LL))
      L.at(LL).insert(i);
    else
      L[LL] = {i};
    if (R.count(RR))
      R.at(RR).insert(i);
    else
      R[RR] = {i};
  }
  int64_t count = 0;
  set<int> Rset;
  for (auto p : L) {
    if (R.count(p.first)) {
      Rset = R.at(p.first);
      auto itr = Rset.begin();
      int Rsize = Rset.size();
      for (auto l : p.second) {
        int cnt = 0;
        while (l >= *itr) {
          itr++;
          cnt++;
          if (itr == Rset.end())
            break;
        }
        count += (Rsize - cnt);
      }
    }
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  map<int, set<int>> L, R;
  int LL, RR;
  for (int i = 0; i < N; i++) {
    LL = (i + 1) + A.at(i);
    RR = (i + 1) - A.at(i);
    if (L.count(LL))
      L.at(LL).insert(i);
    else
      L[LL] = {i};
    if (R.count(RR))
      R.at(RR).insert(i);
    else
      R[RR] = {i};
  }
  int count = 0;
  for (auto p : L) {
    if (R.count(p.first)) {
      for (auto l : p.second) {
        for (auto r : R.at(p.first))
          if (l < r)
            count++;
      }
    }
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll N;
  cin >> N;
  vector<int> A(N);
  map<int, int> M;
  rep(i, N) {
    cin >> A[i];
    M[i - A[i]]++;
  }

  ll sum = 0;
  rep(i, N) {
    if (M.count(i + A[i]) > 0)
      sum += M[i + A[i]];
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  ll N;
  cin >> N;
  map<ll, set<ll>> M;
  rep(i, N) {
    ll A;
    cin >> A;
    M[A].insert(i);
  }
  ll sum = 0;
  set<pair<ll, ll>> USED;
  for (auto x : M) {
    for (auto y : M) {
      ll h = x.first + y.first;
      auto cur = begin(x.second);
      for (; *cur + h <= N && cur != end(x.second); cur++) {
        set<ll> &s = y.second;
        auto n = lower_bound(begin(s), end(s), *cur + h);
        if (n != end(s) && *n == *cur + h && *n != *cur) {
          ll a, b;
          a = *n, b = *cur;
          if (a > b)
            swap(a, b);
          if (USED.count({a, b}) > 0)
            continue;
          USED.insert({a, b});
          // printf("%ld %ld %ld %ld\\n", a, b, x.first, y.first);
          sum++;
        }
      }
    }
  }
  cout << sum << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

// C++ 14
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for (ll __x = __start; __x < __end; __x++)

int main() {
  int n;
  cin >> n;
  vector<ll> A(n);
  loop(i, 0, n) cin >> A[i];
  map<int, int> M;
  ll ans = 0;
  loop(i, 1, n + 1) {
    ll v = i + A[i - 1];
    if (i == 1) {
      if (v < 200000)
        M[v]++;
      continue;
    }
    ll val = i - A[i - 1];
    if (val >= 0) {
      ans += M[val];
    }
    if (v < 200000)
      M[v]++;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// C++ 14
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define loop(__x, __start, __end) for (ll __x = __start; __x < __end; __x++)

int main() {
  int n;
  cin >> n;
  vector<ll> A(n);
  loop(i, 0, n) cin >> A[i];
  multiset<ll> S;
  ll ans = 0;
  loop(i, 1, n + 1) {
    if (i == 1) {
      S.insert(i + A[i - 1]);
      continue;
    }
    ll val = i - A[i - 1];
    if (val >= 0) {
      ans += S.count(val);
    }
    S.insert(i + A[i - 1]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f; // 1061109567
const int INF = 0x7fffffff; // 2147483647
const int SUP = 0x80000000; //-2147483648
#define MAX 1000000
ll a[MAX] = {0}, b[MAX] = {0};
int main() {
  int n;
  ll t;
  ll sum = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> t;
    if (i + t <= n)
      a[i + t]++;
    if (i - t >= 1)
      b[i - t]++;
  }
  for (int i = 1; i <= n; i++) {
    sum += a[i] * b[i];
  }
  cout << sum;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#define endl '\\n'
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;

#define int long long

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#define FAST_IO                                                                \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define flood(arr, c, s)                                                       \\
  for (int i = 0; i < s; i++)                                                  \\
  arr[i] = c
#define int long long
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<pair<int, int>> vii;
const int N = 1e5 + 2;
const int INF = 1e9 + 5;
const int MOD = 1e9 + 7;

int32_t main() {
  FAST_IO;
  int n;
  cin >> n;
  int h[n + 1];
  unordered_map<int, int> c;
  int f = 0;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    f += c[i - h[i]];
    c[i + h[i]]++;
  }
  cout << f;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define FAST_IO                                                                \\
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
#define flood(arr, char, size)                                                 \\
  for (int i = 0; i < size; i++)                                               \\
  arr[i] = char
// #define int long long
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<pair<int, int>> vii;
const int N = 1e5 + 2;
const int INF = 1e9 + 5;
const int MOD = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vi height(n);
  for (int i = 0; i < n; i++)
    cin >> height[i];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + height[i]; j < n; j++) {
      if (height[i] + height[j] == j - i)
        cnt++;
    }
  }
  cout << cnt;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(A) A.begin(), A.end()

int main() {
  int n;
  cin >> n;
  vector<ll> x[n], y[n];
  rep(i, n) {
    x[i] = vector<ll>(0);
    y[i] = vector<ll>(0);
  }
  rep(i, n) {
    ll a;
    cin >> a;
    if (i - a >= 0) {
      x[i - a].push_back(i);
    }
    if (i + a < n) {
      y[i + a].push_back(i);
    }
  }
  ll ans = 0;
  ll p, q, r, s;
  rep(i, n) { ans += x[i].size() * y[i].size(); }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(A) A.begin(), A.end()

int main() {
  int n;
  cin >> n;
  vector<ll> x[n];
  rep(i, n) { x[i] = vector<ll>(0); }
  rep(i, n) {
    ll a;
    cin >> a;
    if (i - a >= 0) {
      x[i - a].push_back(i);
    }
    if (i + a < n) {
      x[i + a].push_back(i);
    }
  }
  ll ans = 0;
  ll p, q, r, s;
  rep(i, n) {
    rep(j, x[i].size()) {
      p = x[i][j];
      rep(k, j) {
        q = x[i][k];
        if ((p - i) * (q - i) < 0) {
          ans++;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int, int>
#define all(x) x.begin(), x.end()
#define Matrix vector<vector<int>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\\n'

constexpr int INF = ((int64_t)2e18);
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 2e5 + 3;

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;

  // cin >> t;
  t = 1;

  while (t--) {
    int n;
    cin >> n;

    map<int, int> m;
    int ans = 0;
    for (int i = 1; i <= n; ++i) {

      int x;
      cin >> x;

      ans += m[(i - x)];

      ++m[(i + x)];
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define eb emplace_back
#define ii pair<int, int>
#define OK (cerr << "OK" << endl)
#define debug(x) cerr << #x " = " << (x) << endl
#define ff first
#define ss second
#define int long long
#define tt tuple<int, int, int, int>
#define all(x) x.begin(), x.end()
#define Matrix vector<vector<int>>
#define Mat(n, m, v) vector<vector<int>>(n, vector<int>(m, v))
#define endl '\\n'

constexpr int INF = ((int64_t)2e18);
constexpr int MOD = 1e9 + 7;
constexpr int MAXN = 2e5 + 3;

signed main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;

  // cin >> t;
  t = 1;

  while (t--) {
    int n;
    cin >> n;

    multiset<int> s1, s2;
    int ans = 0;
    for (int i = 1; i <= n; ++i) {

      int x;
      cin >> x;

      ans += s1.count(i - x);

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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>

#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#define int64 long long
#define uint64 unsigned long long
using namespace std;

int main() {
  int64 nn, ans = 0;
  cin >> nn;
  vector<int64> aa(nn + 1), memo(nn + 1, 0);
  for (int64 ii = 1; ii <= nn; ii++) {
    cin >> aa[ii];
    if (ii - aa[ii] > 0) {
      memo[ii - aa[ii]]++;
    }
  }
  for (int64 ii = 1; ii <= nn; ii++) {
    if (aa[ii] + ii <= nn) {
      ans += memo[aa[ii] + ii];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>

#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#define int64 long long
#define uint64 unsigned long long
using namespace std;

int main() {
  int64 nn, ans = 0;
  cin >> nn;
  vector<int64> aa(nn + 1);
  multiset<int64> memo;
  for (int64 ii = 1; ii <= nn; ii++) {
    cin >> aa[ii];
    if (ii - aa[ii] > 0) {
      memo.insert(ii - aa[ii]);
    }
  }
  for (int64 ii = 1; ii <= nn; ii++) {
    ans += memo.count(aa[ii] + ii);
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, res = 0;
  cin >> n;
  long long ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  unordered_map<long long, int> mp;
  for (int i = 0; i < n; i++) {
    mp[i - ar[i]]++;
  }
  long long sol = 0;
  for (int i = 0; i < n; i++) {
    sol += mp[ar[i] + i];
  }
  cout << sol << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, res = 0;
  cin >> n;
  long long ar[n];
  for (int i = 0; i < n; i++) {
    cin >> ar[i];
  }
  for (int i = 0; i < n; i++) {
    long long altura = ar[i];
    for (int j = i + altura; j < n; j++) {
      if (abs(j - i) == altura + ar[j])
        res++;
    }
  }
  cout << res << '\\n';
}