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

// https://atcoder.jp/contests/abc166/tasks/abc166_e
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
const ll LINF = 0x1fffffffffffffff;
#define FOR(i, a, b) for (ll i = (a); i < static_cast<ll>(b); ++i)
#define REP(i, n) FOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
#if LOCAL & 01
  std::ifstream in("./test/sample-1.in");
  // std::ifstream in("./input.txt");
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  ll N;
  cin >> N;
  vector<ll> an(N + 1);
  struct Info {
    ll a;
    ll ij;
    Info(ll ia, ll ii) : a(ia), ij(ii) {}
  };
  unordered_map<ll, vector<Info>> itbl, jtbl;
  FOR(i, 1, N + 1) {
    ll a;
    cin >> a;
    an.at(i) = a;
    auto aii = i + a;
    itbl[aii].push_back(Info(a, i));
    auto jaj = i - a;
    jtbl[jaj].push_back(Info(a, i));
  }

  ll ans = 0;
  for (auto &aij : itbl) {
    auto it = jtbl.find(aij.first);
    if (it != jtbl.end()) {
      ans += (aij.second.size() * it->second.size());
    }
  }

  cout << ans << endl;

  return 0;
}

int main_() {
  ll N;
  cin >> N;
  vector<ll> an(N + 1);
  FOR(i, 1, N + 1) { cin >> an.at(i); }
  ll ans = 0;
  FOR(i, 1, N + 1) {
    FOR(j, i + 1, N + 1) {
      ll v = abs(j - i);
      if (an[i] + an[j] == v) {
        ++ans;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc166/tasks/abc166_e
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
const ll LINF = 0x1fffffffffffffff;
#define FOR(i, a, b) for (ll i = (a); i < static_cast<ll>(b); ++i)
#define REP(i, n) FOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int main() {
  ll N;
  cin >> N;
  vector<ll> an(N + 1);
  FOR(i, 1, N + 1) { cin >> an.at(i); }
  ll ans = 0;
  FOR(i, 1, N + 1) {
    FOR(j, i + 1, N + 1) {
      ll v = abs(j - i);
      if (an[i] + an[j] == v) {
        ++ans;
      }
    }
  }
  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;
  cin >> n;

  vector<int> a(n), x(n), y(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    x[i] = i + a[i];
    y[i] = i - a[i];
  }
  sort(y.begin(), y.end());

  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += upper_bound(y.begin(), y.end(), x[i]) -
           lower_bound(y.begin(), y.end(), x[i]);
  }
  cout << ans << endl;
  return 0;
}

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[i];
  }

  long long int ans = 0;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j + i < n; j++) {
      if (a[j] + a[j + i] == i)
        ans += 1;
    }
  }
  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(void) {
  int n;
  cin >> n;
  vector<int> h(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> h[i];

  vector<pair<int, int>> vec(n + 1);
  vector<int> wa(n + 1);
  for (int i = 1; i <= n; i++) {
    wa[i] = h[i] + i;
    vec[i] = {i - h[i], i};
  }
  sort(vec.begin(), vec.end());
  vector<int> sa(n + 1), sapos(n + 1);
  for (int i = 1; i <= n; i++) {
    sa[i] = vec[i].first;
    sapos[i] = vec[i].second;
  }

  ll count = 0;
  for (int i = 1; i <= n; i++) {
    int itrl = lower_bound(sa.begin(), sa.end(), wa[i]) - sa.begin();
    int itrr = lower_bound(sa.begin(), sa.end(), wa[i] + 1) - sa.begin();
    if (sa[itrl] != wa[i])
      continue;

    while (sapos[itrl] <= i)
      itrl++;
    count += itrr - itrl;
    /*while(sa[itrl]==wa[i]){
        if(sapos[itrl]>i){count++; itrl++;}
    }*/
  }

  cout << count << endl;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int n;
  cin >> n;
  vector<int> h(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> h[i];

  vector<pair<int, int>> vec(n + 1);
  vector<int> wa(n + 1);
  for (int i = 1; i <= n; i++) {
    wa[i] = h[i] + i;
    vec[i] = {i - h[i], i};
  }
  sort(vec.begin(), vec.end());
  vector<int> sa(n + 1), sapos(n + 1);
  for (int i = 1; i <= n; i++) {
    sa[i] = vec[i].first;
    sapos[i] = vec[i].second;
  }

  ll count = 0;
  for (int i = 1; i <= n; i++) {
    int itrl = lower_bound(sa.begin(), sa.end(), wa[i]) - sa.begin();
    if (sa[itrl] != wa[i])
      continue;

    while (sa[itrl] == wa[i]) {
      if (sapos[itrl] > i) {
        count++;
        itrl++;
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
map<int, int> m;
int main() {
  int n;
  ll cnt = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    m[x + i]++;
    cnt += m[i - x];
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int s[200010];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (j - i == s[i] + s[j]) {
        ans++;
      }
    }
  }
  cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld long double
typedef pair<ll, ll> pairs;
int main() {
  // your code goes here
  std::ios::sync_with_stdio(false);
  ll t, n, i, j, k, l, c = 0, f;
  cin >> n;
  i = 1;
  vector<ll> a(n + 1); // b(n+1),d(n+1);
  map<ll, ll> m;
  while (i <= n) {
    cin >> a[i];
    if (m.find(a[i] + i) == m.end())
      m.insert({i + a[i], 1});
    else
      m[i + a[i]]++;
    i++;
  }
  for (i = 1; i <= n; i++) {
    k = i - a[i];
    if (m.find(k) != m.end())
      c += m[k];
  }
  cout << c << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lld long double
typedef pair<ll, ll> pairs;
ll count(vector<ll> d, ll n, ll k) {
  ll p = 0, q = 0;
  ll l = 1, r = n, mid;
  while (l <= r) {
    mid = l + (r - l) / 2;
    if (d[mid] == k && (mid == n || d[mid + 1] > k)) {
      q = mid;
      break;
    } else if (d[mid] <= k)
      l = mid + 1;
    else
      r = mid - 1;
  }
  l = 1;
  r = n;
  while (l <= r) {
    mid = l + (r - l) / 2;
    if (d[mid] == k && (mid == 1 || d[mid - 1] < k)) {
      p = mid;
      break;
    } else if (d[mid] >= k)
      r = mid - 1;
    else
      l = mid + 1;
  }
  /* for (ll a = n; a >= 1; a /= 2) {
       while (p+a <= n && x[p+a] < k) p += a;
       while (q+a <= n && x[q+a] <= k) q += a;
   }*/
  if (p && q)
    return q - p + 1;
  else
    return 0;
}
int main() {
  // your code goes here
  std::ios::sync_with_stdio(false);
  ll t, n, i, j, k, l, c = 0, f;
  cin >> n;
  i = 1;
  vector<ll> a(n + 1), b(n + 1), d(n + 1);
  while (i <= n) {
    cin >> a[i];
    b[i] = i + a[i];
    d[i] = i - a[i];
    i++;
  }
  sort(b.begin() + 1, b.end());
  sort(d.begin() + 1, d.end());
  k = 0;
  for (i = 1; i <= n; i++) {
    // cout<<b[i]<<" ";
    if ((i - 1) && (b[i - 1] == b[i]))
      c += k;
    else {
      k = count(d, n, b[i]);
      c += k;
    }
  }
  cout << c << "\\n";
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

int mid = 400020;

int nums[600030] = {0};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;
  vector<int> as(n);
  for (auto &a : as)
    cin >> a;

  int64_t ans = 0;
  FOR_LT(i, 0, n) {
    if (as[i] >= 200000)
      continue;
    ans += nums[mid + as[i] - i];
    nums[mid - as[i] - i]++;
  }
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n) for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;

int mid = 200010;

int nums[400020] = {0};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);

  int n;
  cin >> n;
  vector<int> as(n);
  for (auto &a : as)
    cin >> a;

  int64_t ans = 0;
  FOR_LT(i, 0, n) {
    if (as[i] >= 200000)
      continue;
    ans += nums[mid + as[i] - i];
    nums[mid - as[i] - i]++;
  }
  cout << ans;

  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++)
typedef long long ll;
using namespace std;

int main() {
  ll n;
  cin >> n;
  map<ll, ll> m;
  ll ans = 0;

  for (ll i = 1; i <= n; i++) {
    ll a;
    cin >> a;
    ans += m[i - a];
    m[i + a]++;
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  int ans = 0;

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

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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#define REP(i, n) for ((i) = 0; (i) < (int)(n); (i)++)
#define ll long long
using namespace std;

ll i, j, k;
const ll MOD = 1000000000 + 7;

int main() {
  int n;
  cin >> n;
  ll a[n];
  ll ans = 0;
  map<ll, ll> memo;
  REP(i, n) { cin >> a[i]; }

  REP(i, n) {
    if (memo.find(i + 1 - a[i]) != memo.end()) {
      ans += memo[i + 1 - a[i]];
    }
    if (memo.find(i + 1 + a[i]) != memo.end()) {
      memo[i + 1 + a[i]]++;
    } else {
      memo[i + 1 + a[i]] = 1;
    }
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, n) for ((i) = 0; (i) < (int)(n); (i)++)
#define ll long long
using namespace std;

ll i, j, k;
const ll MOD = 1000000000 + 7;

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

#pragma GCC optimize("O2")
// #include <bits/stdc++.h>
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>
using namespace std;
#define mod 1000000007

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> a(n);
  long long ans = 0;
  map<long long, long long> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += m[a[i] - i];
    m[-a[i] - i]++;
  }

  cout << ans << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O2")
// #include <bits/stdc++.h>
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
#define mod 1000000007

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> a(n);
  long long mini = mod;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mini = min(mini, a[i]);
  }
  // cout << mini << "\\n";
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 2 * mini; j < n; j++) {
      if (abs(j - i) == a[j] + a[i])
        ans++;
    }
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define deb(x) cout << #x << "=" << x << endl;
#define endl '\\n'
#define M 1000000007
#define int long long
#define max_n 1000000

using namespace std;

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

int32_t main() {
  IOS ll T = 1;
  // cin>>T;
  for (ll i = 1; i <= T; ++i) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define deb(x) cout << #x << "=" << x << endl;
#define endl '\\n'
#define M 1000000007
#define int long long
#define max_n 1000000

using namespace std;

void solve() {
  ll n;
  cin >> n;
  ll a[n];
  multiset<ll> m1, m2;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    m2.insert(i + 1 - a[i]);
  }
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    m2.erase(m2.find(i + 1 - a[i]));
    ans += m2.count(a[i] + i + 1);
  }
  cout << ans << endl;
}

int32_t main() {
  IOS ll T = 1;
  // cin>>T;
  for (ll i = 1; i <= T; ++i) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

// https://atcoder.jp/contests/abc166/tasks/abc166_e
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define DIV(a, b) ((a - 1) / b + 1)

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<int> v(N);
  unordered_map<int, int> mp;
  REP(i, N) {
    cin >> v[i];
    mp[i - v[i]]++;
  }
  int ans = 0;
  REP(i, N) { ans += mp[i + v[i]]; }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc166/tasks/abc166_e
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define DIV(a, b) ((a - 1) / b + 1)

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  vector<int> v(N);
  for (auto &x : v) {
    cin >> x;
  }
  int ans = 0;
  REP(i, N) {
    int t = v[i];
    FOR(j, i + t, N) {
      if (t + v[j] == j - i) {
        ans++;
      }
    }
  }
  cout << ans << endl;

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

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  long long int N;
  long long int X;
  long long int count = 0;
  scanf("%lld", &N);
  long long int A[300000];
  long long int counter_minus[300000];
  long long int counter_plus[300000];

  for (int i = 0; i < 300000; ++i) {
    counter_minus[i] = 0;
    counter_plus[i] = 0;
    A[i] = 0;
  }

  for (int i = 1; i <= N; ++i) {
    scanf("%lld", &A[i]);
    if (abs(A[i] + i) < 300000) {
      counter_plus[A[i] + i]++;
    }
    if (i - A[i] >= 0) {
      counter_minus[i - A[i]]++;
    }
  }

  for (long long int i = 0; i < 300000; ++i) {
    count += counter_minus[i] * counter_plus[i];
  }
  printf("%lld", count);
}

int D() {
  long long int A, B;
  long long int X;
  scanf("%lld", &X);
  for (long long int i = -1000; i <= 1000; ++i) {
    A = pow(i, 5);
    for (long long int j = -1000; j <= 1000; ++j) {

      B = pow(j, 5);
      if (A - B == X) {
        printf("%d %d", i, j);
        return 0;
      }
    }
  }

  printf("A");
}


Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  long long int N;
  long long int X;
  long long int count = 0;
  scanf("%lld", &N);
  long long int A[N + 1];
  for (int i = 1; i <= N; ++i) {
    scanf("%lld", &A[i]);
  }
  for (long long int i = 1; i < N; ++i) {
    for (long long int j = i + 1; j <= N; ++j) {
      if (A[j] + A[i] == j - i) {
        count++;
      }
    }
  }
  printf("%lld", count);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define acc_io                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define all(x) x.begin(), x.end()
#define vvl vector<vector<ll>>
#define vl vector<ll>
#define rep(i, sta, end) for (ll i = sta; i < end; i++)
#define lcm(a, b) (a) / __gcd((a), (b)) * (b)
typedef long long ll;
const ll MOD = 1000000007;
const double PI = acos(-1);
// name[i]   -> vl  name(i)
// name[i][j]-> vvl name(i,vl(j))
// or-> ||
ll n, a;
signed main() {
  acc_io;
  cin >> n;
  map<int, int> mp;
  ll sa, wa;
  ll ans = 0;
  rep(i, 0, n) {
    cin >> a;
    sa = i - a;
    ans += mp[sa];
    wa = i + a;
    mp[wa]++;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define acc_io                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false)
#define MOD (ll)(1e9 + 7)
#define ll long long
#define all(x) x.begin(), x.end()
#define vvl vector<vector<ll>>
#define vl vector<ll>
#define rep(i, sta, end) for (ll i = sta; i < end; i++)
// name[i]   -> vl  name(i)
// name[i][j]-> vvl name(i,vl(j))
// or-> ||

signed main() {
  acc_io;
  ll n;
  cin >> n;
  vl a(n + 1);
  rep(i, 1, n + 1) cin >> a[i];

  ll cnt = 0;
  rep(i, 1, n + 1) {
    rep(j, 1, i) {
      if (i == j)
        continue;

      ll tmp1 = abs(i - j);
      tmp1 %= MOD;
      ll tmp2 = (a[i] + a[j]) % MOD;
      if (tmp1 == tmp2)
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <map>
#include <stdio.h>
using namespace std;
map<int, int> times;

int main() {
  int N;
  scanf("%d", &N);
  long long ans = 0;
  int temp = 0;
  for (int i = 1; i <= N; ++i) {
    scanf("%d", &temp);
    ans += times[i - temp];
    times[i + temp]++;
  }
  printf("%lld ", ans);
}

Error: TLE

Fixed submission:

#include <stdio.h>

int value[200090];

int main() {
  int N;
  scanf("%d", &N);
  int cnt = 0;
  for (int i = 1; i <= N; ++i) {
    scanf("%d", &value[i]);
    for (int j = i - 1; j >= 1; --j)
      if (i - j == value[i] + value[j])
        cnt++;
  }
  printf("%d ", cnt);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  set<ll> s;
  ll ans = 0;
  map<ll, ll> m;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (s.find(A[i] + i) == s.end()) {
      s.insert(A[i] + i);
      m[A[i] + i] = 1;
    } else {
      m[A[i] + i]++;
    }
    if (s.find(i - A[i]) != s.end()) {
      ans += m[i - A[i]];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + A[i] + 1; j < N; j++) {
      if (A[i] + A[j] == abs(i - j))
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}