alexjercan/bugnet huggingface dataset:
Problem p02690 (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>;

int main() {
  int x;
  cin >> x;
  for (int a = 0; a < 120; a++) {
    ll a5 = 1;
    rep(i, 5) a5 *= a;
    for (int b = -120; b < a; b++) {
      ll b5 = 1;
      rep(i, 5) b5 *= b;
      if (a5 - b5 == x) {
        cout << a << " " << b << endl;
        return 0;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#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>;

int main() {
  ll x, a, b;
  cin >> x;
  ll n = 50000;
  vector<ll> A(n);
  for (int i = 0; i < n; i++) {
    A.at(i) = i * i * i * i * i;
  }
  rep(i, n) {
    rep(j, n) {
      if (A[i] - A[j] == x) {
        a = i;
        b = j;
        break;
      } else if (A[i] + A[j] == x) {
        a = i;
        b = -j;
        break;
      }
    }
  }
  cout << a << " " << b << endl;
  return 0;
}
Problem p02690 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define sz(a) (int)a.size()
using namespace std;

// mod無しバージョン
ll pow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a;
    a = a * a;
    n >>= 1;
  }
  return res;
}

int main() {
  ll x;
  cin >> x;
  for (ll i = -1000; i <= 1000; i++) {
    for (ll j = -1000; j <= 1000; j++) {
      if (pow(i, 5) - pow(j, 5) == x) {
        cout << i << " " << j << endl;
        return 0;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
#define sz(a) (int)a.size()
using namespace std;

// mod無しバージョン
ll pow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a;
    a = a * a;
    n >>= 1;
  }
  return res;
}

int main() {
  ll x;
  cin >> x;
  for (ll i = -5000; i <= 5000; i++) {
    for (ll j = -5000; j <= 5000; j++) {
      if (pow(i, 5) - pow(j, 5) == x) {
        cout << i << " " << j << endl;
        return 0;
      }
    }
  }
}
Problem p02690 (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>;

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

  for (int i = -1000; i <= 1000; ++i) {
    for (int j = -1000; j <= 1000; ++j) {
      if (pow(i, 5.0) - pow(j, 5.0) == x) {
        cout << i << " " << j << 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;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll x;
  cin >> x;

  for (ll a = 0; a < 100000000; ++a) {
    ll b = x - pow(a, 5);
    double b_root5 = pow(b, 0.2);
    // cout << b << " " << b_root5 << endl;
    if (ceil(b_root5) == floor(b_root5)) {
      cout << a << " " << (int)b_root5 * (-1) << endl;
      return 0;
    }
  }

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

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int Power(ll a, int b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2 != 0)
      res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}
int main() {
  int x;
  scanf("%d", &x);
  ll a[241];
  a[0] = 0;
  for (int i = 1; i <= 120; i++) {
    a[2 * i - 1] = i;
    a[2 * i] = -i;
  }
  int c = 0;
  for (int i = 0; i <= 240; i++) {
    for (int j = 0; j <= 240; j++) {
      if ((Power(a[i], 5) - Power(a[j], 5)) == x) {
        printf("%d %d", a[i], a[j]);
        c++;
        break;
      }
    }
    if (c != 0)
      break;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
int Power(ll a, int b) {
  ll res = 1;
  while (b > 0) {
    if (b % 2 != 0)
      res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}
int main() {
  int x;
  scanf("%d", &x);
  ll a[241];
  for (int i = 0; i <= 240; i += 2) {
    a[i / 2] = 120 - (i / 2);
    a[(i / 2) + 1] = (i / 2) - 120;
  }
  int c = 0;
  for (int i = 0; i <= 241; i++) {
    for (int j = 0; j <= 241; j++) {
      if ((Power(a[i], 5) - Power(a[j], 5)) == x) {
        printf("%d %d", i, j);
        c++;
        break;
      }
    }
    if (c != 0)
      break;
  }
  return 0;
}
Problem p02690 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) (v).begin(), (v).end()
#define out(a) cout << (a) << endl
using namespace std;
using ll = long long;
int main() {
  int x;
  cin >> x;
  int mini = -150;
  int maxi = 150;

  for (ll i = mini; i < maxi; ++i) {
    for (ll j = mini; j < maxi; ++j) {
      if (pow(i, 5) - pow(j, 5) == x) {
        cout << i << " " << j << endl;
        return 0;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) (v).begin(), (v).end()
#define out(a) cout << (a) << endl
using namespace std;
using ll = long long;
int main() {
  int x;
  cin >> x;
  int a = 0, b = 0;
  int anum = pow(x, 1.0 / 5.0);
  while (true) {
    a = pow(anum, 5);
    int bnum = a - x;
    b = pow(abs(bnum), 1.0 / 5.0);
    if (a - b == x || a - (-b) == x) {
      cout << anum << " " << bnum << endl;
      break;
    }
    anum++;
  }
}
Problem p02690 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int64_t x;
  cin >> x;
  int64_t a = 0, b = 0;
  for (int i = -500; i <= 500; i++) {
    for (int j = -500; j <= 500; j++) {
      if (pow(i, 5) - pow(j, 5) == x) {
        a = i;
        b = j;
        break;
      }
    }
  }
  cout << a << " " << b << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INF = 1LL << 60;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int64_t x;
  cin >> x;
  int64_t a = 0, b = 1;
  int64_t insuu;
  for (int i = 2; i * i <= x; i++) {
    if (x % i == 0) {
      insuu = i;
      break;
    }
  }
  for (int64_t i = (-1000) * insuu;; i += insuu) {
    double ggg = pow(i, 5);
    double gggg = ggg + x;
    double g = pow(gggg, 0.2);
    int64_t gg = pow(gggg, 0.2);
    if (floor(g) == g) {
      a = gg;
      b = i;
      break;
    }
  }
  cout << a << " " << b << endl;
}
Problem p02690 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

// #include <bits/stdc++.h>
using namespace std;
#define print(x) cout << (x) << endl
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long int
#define pb push_back

int main() {
  ll x, a, b = 0;
  double t;
  cin >> x;

  rep(i, 1000) {
    rep(j, 1000) {
      if (pow(i, 5) + pow(j, 5) == x) {
        cout << i << " " << 0 - j << endl;
        return 0;
      }
      if (pow(i, 5) - pow(j, 5) == x) {
        cout << i << " " << j << endl;
        return 0;
      }
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>

// #include <bits/stdc++.h>
using namespace std;
#define print(x) cout << (x) << endl
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long int
#define pb push_back

int main() {
  ll x, a, b = 0;
  double t;
  cin >> x;
  bool flag = true;
  a = x;

  while (x > pow(b, 5)) {
    a = x - pow(b, 5);
    t = pow(a, 1. / 5.);
    if (t == int(t)) {
      b = 0 - b;
      a = t;
      flag = false;
      break;
    }
    b++;
  }

  if (flag) {
    b = 1;
    // B > 0　の時
    while (1) {
      a = x + pow(b, 5);
      t = pow(a, 1. / 5.);
      if (t == int(t)) {
        a = int(t);
        break;
      }
      b++;
    }
  }

  cout << a << " " << b << endl;
}
Problem p02690 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

int main() {

  long long x;
  cin >> x;

  long p = ceil(pow(x, 0.2)) * 5;
  for (long i = p; i > -p; i--) {
    for (long q = p; q > -p; q--) {
      if (pow(i, 5) - pow(q, 5) == x) {
        cout << i << " " << q << endl;
        return 0;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

int main() {

  long long x;
  cin >> x;

  long p = ceil(pow(x, 0.2));
  for (;; p--) {
    for (long q = p; q > -p; q--) {
      if (pow(p, 5) - pow(q, 5) == x) {
        cout << p << " " << q << endl;
        return 0;
      }
    }
  }

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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using ll = long long;
namespace mp = boost::multiprecision;
using mpint = mp::cpp_int; // 多倍長整数（任意精度）

int main() {
  ll N;

  cin >> N;

  vector<ll> A(N);
  map<ll, ll> L;

  for (ll i = 0; i < N; ++i) {
    cin >> A[i];

    if (L.find(A[i] + i) != L.end()) {
      ++L[A[i] + i];
      continue;
    }
    L[A[i] + i] = 1;
  }

  ll ans = 0;

  for (ll i = 0; i < N; ++i) {
    if (L.find(i - A[i]) != L.end())
      ans += L[i - A[i]];
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using ll = long long;
namespace mp = boost::multiprecision;
using mpint = mp::cpp_int; // 多倍長整数（任意精度）

int main() {
  int N;

  cin >> N;

  vector<int> A(N);
  map<ll, vector<ll>> L;
  map<ll, vector<ll>> R;

  for (int i = 0; i < N; ++i) {
    cin >> A[i];
    L[A[i] + i + 1].emplace_back(i);
    R[i + 1 - A[i]].emplace_back(i);
  }

  int val = 0;

  for (auto i = L.begin(); i != L.end(); ++i) {
    for (auto v : i->second) {
      for (auto vv : R[i->first]) {
        if (v != vv)
          ++val;
      }
    }
  }

  cout << val << 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;

int main() {
  int max = 1000000000;
  int N;
  cin >> N;
  int a[N];
  rep(i, N) { cin >> a[i]; }
  map<int, int> l, r;
  rep(i, N) {
    if (l.find(i + a[i]) != l.end()) {
      l[i + a[i]]++;
    } else {
      l[i + a[i]] = 1;
    }
  }
  ll count = 0;
  rep(j, N) {
    if (l.find(j - a[j]) != l.end()) {
      count += l[j - a[j]];
    }
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  /* ll t;
  cin>>t;
  while(t--) */
  {
    ll i, j, k, l, m, n, a, b, c, d, cnt = 0, p = 0, q;
    cin >> n;
    ll arr[n + 1];
    map<int, int> mp;
    for (i = 1; i <= n; i++) {
      cin >> arr[i];
      cnt += mp[i - arr[i]];
      mp[i + arr[i]]++;
    }

    cout << cnt << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  /* ll t;
  cin>>t;
  while(t--) */
  {
    ll i, j, k, l, m, n, a, b, c, d, cnt = 0, p = 0, q;
    cin >> n;
    ll arr[n];
    for (i = 0; i < n; i++)
      cin >> arr[i];

    for (i = 0; i < n; i++) {
      for (j = 0; j < i; j++) {
        if (abs(i - j) == arr[i] + arr[j])
          cnt++;
      }
    }
    cout << cnt << endl;
  }
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<ll> V(n);
  REP(i, n) { V[i] = a[i] + i + 1; }
  vector<pair<ll, ll>> v(n);
  REP(i, n) {
    v[i].first = a[i] - i - 1;
    v[i].second = i;
  }
  sort(v.begin(), v.end());
  ll ans = 0;
  REP(i, n) {
    auto it1 = lower_bound(v.begin(), v.end(), pair<ll, ll>(-V[i], -1));
    if (it1 == v.end())
      continue;
    if (it1->first == -V[i]) {
      auto it2 = upper_bound(v.begin(), v.end(), pair<ll, ll>(-V[i], n + 10));
      ans += it2 - it1;
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<ll> V(n);
  REP(i, n) { V[i] = a[i] + i + 1; }
  vector<pair<ll, ll>> v(n);
  REP(i, n) {
    v[i].first = a[i] - i - 1;
    v[i].second = i;
  }
  sort(v.begin(), v.end());
  ll ans = 0;
  REP(i, n) {
    auto it1 = lower_bound(v.begin(), v.end(), pair<ll, ll>(-V[i], -1));
    if (it1 == v.end())
      continue;
    if (it1->first == -V[i]) {
      auto it2 = upper_bound(v.begin(), v.end(), pair<ll, ll>(-V[i], n + 10));
      ans += it2 - it1;
      FOR(j, it1 - v.begin(), it2 - v.begin()) {
        if (v[j].second == i)
          ans--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define VI vector<int>
#define VVI vector<vector<int>>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 1e9
#define All(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;

int main() {
  int N;
  cin >> N;
  // vector<ll> A(N);
  vector<ll> idxp(N), idxm(N);
  map<ll, int> cnt;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    idxp[i] = i + 1 + a;
    idxm[i] = i + 1 - a;
    cnt[idxm[i]]++;
  }
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    cnt[idxm[i]]--;
    ans += cnt[idxp[i]];
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define VI vector<int>
#define VVI vector<vector<int>>
#define REP(i, n) for (int i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 1e9
#define All(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int mod = 1e9 + 7;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int mod = 1e9 + 7;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<unordered_map<int, int>> mp;
  unordered_map<int, int> tmp;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    tmp[a[i] + i]++;
    mp.push_back(tmp);
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += mp[i][i - a[i]];
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;
typedef pair<ll, ll> pii;

long long MOD = 1000000000 + 7;

bool compare(pii x, pii y) { return x.first < y.first; }

int main() {
  cout << setprecision(10);
  int N;
  cin >> N;

  map<ll, int> m;

  ll ans = 0;

  for (ll i = 1; i <= N; i++) {
    ll tmp;
    cin >> tmp;

    if (m.count(i - tmp) == 0) {

    } else {
      ans += m[i - tmp];
    }

    if (m.count(i + tmp) == 0) {
      m[i + tmp] = 1;
    } else {
      m[i + tmp] += 1;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

long long MOD = 1000000000 + 7;

int main() {
  cout << setprecision(10);
  int N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> X(N), Y(N);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
    X[i] = i + A[i];
    Y[i] = i - A[i];
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    sort(Y.begin() + i, Y.end());

    auto itr1 = lower_bound(Y.begin() + i, Y.end(), X[i]);
    auto itr2 = upper_bound(Y.begin() + i, Y.end(), X[i]);

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

#include <bits/stdc++.h>
#define int long long
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define puts(x) cout << x << "\\n"
constexpr int mod = 1000000007;
using namespace std;
int input() {
  int r;
  cin >> r;
  return r;
}
map<int, int> s;
int n, a[234567], ans;
signed main() {
  cin >> n;
  for1(i, n) {
    a[i] = input();
    s[i - a[i]]++;
  }
  for1(i, n) {
    s[i - a[i]]--;
    ans += s[a[i] + i];
  }
  puts(ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define for1(i, n) for (int i = 1; i <= (n); i++)
#define puts(x) cout << x << "\\n"
using namespace std;
int input() {
  int r;
  cin >> r;
  return r;
}
multiset<int> s;
int n, a[234567], ans;
signed main() {
  cin >> n;
  for1(i, n) {
    a[i] = input();
    s.insert(i - a[i]);
  }
  for1(i, n) {
    s.erase(i - a[i]);
    ans += s.count(a[i] + i);
  }
  puts(ans);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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


Error: TLE

Fixed submission:

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

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

#include <algorithm>
#include <bits/stdc++.h>
#include <set>
#define ll long long
#include <string.h>
using namespace std;

int main() {
  ll int n;
  cin >> n;
  ll int a[n], i, j;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll int b[n], c[n];
  for (i = 0; i < n; i++) {
    b[i] = i + 1 + a[i];
  }
  for (i = 0; i < n; i++) {
    c[i] = i + 1 - a[i];
  }
  ll int s = 0;

  unordered_map<ll int, ll int> mp;
  for (i = 0; i < n; i++) {
    mp[c[i]]++;
  }
  for (i = 0; i < n; i++) {
    s += mp[b[i]];
  }
  cout << s << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <set>
#define ll long long
#include <string.h>
using namespace std;

int main() {
  ll int n;
  cin >> n;
  ll int a[n], i, j;
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll int b[n], c[n];
  for (i = 0; i < n; i++) {
    b[i] = i + 1 + a[i];
  }
  for (i = 0; i < n; i++) {
    c[i] = i + 1 - a[i];
  }
  ll int s = 0;

  for (i = 0; i < n; i++) {
    for (j = i + a[i]; j < n; j++) {
      if (b[i] == c[j])
        s++;
    }
  }
  cout << s << endl;
}