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

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

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 ll INF = 1e15;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);

  vector<ll> x(n); // a[i]+i
  vector<ll> y(n); // a[i]-i
  vector<ll> z(n); //-a[i]+i
  vector<ll> w(n); //-a[i]-i
  map<ll, ll> s;
  map<ll, ll> t;

  rep(i, n) {
    cin >> a[i];
    x[i] = a[i] + i;
    y[i] = a[i] - i;
    z[i] = -a[i] + i;
    w[i] = -a[i] - i;
    s[y[i]]++;
    t[w[i]]++;
  }
  ll ans = 0;

  for (auto x : s) {
    ans += t[x.fi] * x.se;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

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 ll INF = 1e15;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);

  vector<ll> x(n); // a[i]+i
  vector<ll> y(n); // a[i]-i
  vector<ll> z(n); //-a[i]+i
  vector<ll> w(n); //-a[i]-i
  multiset<ll> xs;
  multiset<ll> ys;
  multiset<ll> zs;
  multiset<ll> ws;

  rep(i, n) {
    cin >> a[i];
    x[i] = a[i] + i;
    y[i] = a[i] - i;
    z[i] = -a[i] + i;
    w[i] = -a[i] - i;
  }
  ll ans = 0;
  for (ll i = n - 1; i >= 0; i--) { // 自分の左を数える
    ans += ys.count(w[i]);
    // cout << ys.count(w[i]) << endl;
    ys.insert(y[i]);
  }
  /*rep(i,n){
      if(i){
          ans+=xs.count(z[i]);
          xs.insert(x[i]);
      }
  }*/
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];
  ll ans = 0;
  map<ll, ll> m;
  rep(i, N) { m[i - A[i]]++; }
  rep(i, N) {
    if (m[i + A[i]] > 0) {
      ll t = m[i + A[i]];
      ans += t;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  ll N;
  cin >> N;
  vll A(N);
  rep(i, N) cin >> A[i];
  ll ans = 0;
  map<ll, ll> m1, m2;
  rep(i, N) {
    m1[i + A[i]]++;
    m2[i - A[i]]++;
  }
  for (auto &x : m1) {
    ll t1 = x.first;
    for (auto &y : m2) {
      ll t2 = y.first;
      if (t1 == t2) {
        ans += x.second * y.second;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#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 N;
  cin >> N;
  int A[N];
  rep(i, N) cin >> A[i];

  map<int, int> v;
  rep(i, N) {
    int a = i + 1;
    if (a - A[i] > 0) {
      v[a - A[i]]++;
    }
  }

  ll ans = 0;
  rep(i, N) {
    int b = i + 1;
    ans += v[b + A[i]];
  }

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

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


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

const ll NUM = 1e9;

int main() {
  int N;
  cin >> N;
  int A[N];
  rep(i, N) cin >> A[i];

  vector<ll> v(NUM, 0);
  rep(i, N) {
    int a = i + 1;
    if (a - A[i] > 0) {
      v[a - A[i]]++;
    }
  }

  ll ans = 0;
  rep(i, N) {
    int b = i + 1;
    ans += v[b + A[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++)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
const int inf = 1001001001;

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

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

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

// id_diff = 1...5, 2...4, (i from 2 to n-1)
//  combination for each i, n/i, i = 1,2,3...
//  for each i (from 2 to n) calc n-i times
// sum i(n-i) i = 0,1,2,3...n

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

  int ans = 0;
  for (int i = 2; i < n; i++) {
    for (int j = 0; j < n - i; j++) {
      if (a[j + i] + a[j] == i)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;

ll A[200010];
ll L[200010], R[200010];

int main() {
  ll n;
  cin >> n;
  Rep(i, 1, n) cin >> A[i];
  Rep(i, 1, n) {
    if (A[i] + i <= n)
      L[A[i] + i]++;
    if (i - A[i] >= 1)
      R[i - A[i]]++;
  }
  ll ans = 0;
  Rep(i, 1, n) { ans += L[i] * R[i]; }

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
#define endl "\\n"
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;

ll A[200010];
ll L[200010], R[200010];

int main() {
  ll n;
  cin >> n;
  Rep(i, 1, n) cin >> A[i];
  Rep(i, 1, n) {
    L[A[i] + i]++;
    if (i - A[i] >= 1)
      R[i - A[i]]++;
  }
  ll ans = 0;
  Rep(i, 1, n) { ans += L[i] * R[i]; }

  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define reps(i, s, e) for (long long i = s; i < e; i++)
#define repr(i, n) for (long long i = n - 1; i >= 0; i--)
#define reprs(i, s, e) for (long long i = e - 1; i >= s; i--)

ll fifth_pow(ll a) {
  ll aa = a * a;
  return aa * aa * a;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;

  ll a[n];
  map<ll, ll> mp_pos, mp_neg;
  rep(i, n) {
    cin >> a[i];
    mp_pos[i + 1 + a[i]]++;
    mp_neg[i + 1 - a[i]]++;
  }

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

  cout << ans / 2 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define reps(i, s, e) for (long long i = s; i < e; i++)
#define repr(i, n) for (long long i = n - 1; i >= 0; i--)
#define reprs(i, s, e) for (long long i = e - 1; i >= s; i--)

ll fifth_pow(ll a) {
  ll aa = a * a;
  return aa * aa * a;
}

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n;
  cin >> n;

  ll a[n];
  multiset<ll> st_pos, st_neg;
  rep(i, n) {
    cin >> a[i];
    st_pos.insert(i + 1 + a[i]);
    st_neg.insert(i + 1 - a[i]);
  }

  ll ans = 0;
  rep(i, n) {
    ans += st_neg.count(i + 1 + a[i]);
    ans += st_pos.count(i + 1 - a[i]);
  }

  cout << ans / 2 << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(p, a, b) for (int p = a; p < b; p++)
#define INF 0x3f3f3f3f
#define N 100007

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("o2.txt", "w", stdout);
#endif

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

  ll n, i, ans = 0;
  map<ll, ll> m;

  cin >> n;
  vector<ll> h(n + 1);
  for (i = 1; i <= n; i++) {
    cin >> h[i];
    m[i + h[i]]++;
  }

  for (i = 1; i <= n; i++)
    ans += m[i - h[i]];
  cout << ans;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(p, a, b) for (int p = a; p < b; p++)
#define INF 0x3f3f3f3f
#define N 100007

int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("o2.txt", "w", stdout);
#endif

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

  ll n, i, ans = 0;
  map<ll, ll> m;

  cin >> n;
  vector<ll> h(n);
  for (i = 1; i <= n; i++) {
    cin >> h[i];
    m[i + h[i]]++;
  }

  for (i = 1; i <= n; i++)
    ans += m[i - h[i]];
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n, a;
  cin >> n;
  map<ll, ll> b;
  //    rep(i, 1000200001) b.at(i) = 0;
  ll sum = 0;
  rep(i, n) {
    cin >> a;
    ll k = i + 1;
    sum += b[max(k - a, 0LL)];
    if (b[k + a] == 0) {
      b[k + a] = 1;
    } else {
      b[k + a]++;
    }
  }
  cout << sum << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(210000);
  rep(i, 210000) b.at(i) = 0;
  int sum = 0;
  rep(i, n) {
    cin >> a.at(i);
    int k = i + 1;
    if (a.at(i) < 201000) {
      b.at(k + a.at(i))++;
      sum += b.at(max(k - a.at(i), 0));
    }
  }
  cout << sum << 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++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

int main() {
  int N;
  cin >> N;
  vec<ll> A(N);
  map<ll, ll> lmp;
  map<ll, ll> rmp;
  rep(i, N) {
    cin >> A[i];
    ++lmp[A[i] + i + 1];
    ++rmp[i + 1 - A[i]];
  }

  ll ans = 0;
  for (auto [f, s] : lmp) {
    ans += s * (rmp[f]);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
using ll = long long;
using namespace std;
template <typename T> using vec = std::vector<T>;

int main() {
  int N;
  cin >> N;
  vec<int> A(N);
  int mx = 0;
  rep(i, N) {
    cin >> A[i];
    mx = max(mx, min(A[i], 2 * 100000));
  }

  ll ans = 0;
  rep(i, N) {
    for (int j = i + A[i]; j < N; ++j) {
      if (j - i == A[i] + A[j])
        ++ans;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

int main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  long long int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int sum = 0;
  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (abs(j - i) == a[i] + a[j])
        ans++;
    }
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#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 n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];

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

Error: 0

Fixed submission:

#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 n;
  vector<int> a(n);
  cin >> n;
  rep(i, n) cin >> a[i];

  ll ans = 0;
  map<int, int> mp;
  rep(i, n) {
    int J = i - a[i];
    int I = i + a[i];
    mp[I] += 1;
    ans += mp[J];
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#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 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: 0

Fixed submission:

#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 n;
  vector<int> a(n);
  cin >> n;
  rep(i, n) cin >> a[i];

  ll ans = 0;
  map<int, int> mp;
  rep(i, n) {
    int J = i - a[i];
    int I = i + a[i];
    mp[I] += 1;
    ans += mp[J];
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#include <unordered_map>
#include <vector>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define REP(i, k, n) for (ll i = k; i < n; ++i)
// const ll MOD=1e9+7;

int main() {
  ll n;
  std::cin >> n;
  std::unordered_map<ll, ll> m;

  ll ans = 0;
  rep(i, n) {
    ll x;
    std::cin >> x;
    ans += m[i - x];
    m[i + x]++;
  }
  std::cout << ans << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <vector>
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define REP(i, k, n) for (ll i = k; i < n; ++i)
const ll MOD = 1e9 + 7;

int main() {
  std::ios_base::sync_with_stdio(0);
  std::cin.tie(0);
  std::cout.tie(0);
  ll n;
  std::cin >> n;
  std::vector<ll> a(n);
  rep(i, n) { std::cin >> a[i]; }
  ll ans = 0;
  rep(i, n - 1) {
    REP(j, i + 1, n) {
      if (a[i] + a[j] == j - i) {
        ans++;
      }
    }
  }
  std::cout << ans << std::endl;
  return 0;
}
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++)
#define all(v) v.begin(), v.end()
#define int long long
#define _GLIBCXX_DEBUG

signed main() {
  int N;
  cin >> N;
  vector<int> A(N);
  // vector<pair<int,int>> A(N);
  unordered_map<int, int> map;
  rep(i, N) {
    cin >> A[i];
    map[i - A[i]]++;
  }
  /*rep(i, N) {
    int a; cin >> a;
    A[i] = make_pair(a, i);
  }*/

  int ans = 0;
  rep(i, N) {
    int k = i + A[i];
    ans += map[k];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

signed main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  int ans = 0;
  rep(i, N - 1) {
    int r = i + A[i];
    for (int j = r; j < N; j++) {
      if (j - i == A[i] + A[j])
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  map<ll, ll> l, r;
  rep1(i, n) {
    cin >> a[i];
    l[i + a[i]]++;
    r[i - a[i]]++;
  }
  ll ans = 0;
  ll size = l.size();
  for (auto p : l) {
    ll key = p.first;
    ll val = p.second;
    if (r.count(key))
      ans += l[key] * r[key];
    l.erase(key);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = 100000000;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n, 0);
  rep1(i, n) cin >> a[i];
  ll count = 0;
  for (ll i = 1; i < n; i++) {
    for (ll j = 0; j + i < n; j++) {
      if (a[j] + a[j + i] == i)
        count++;
    }
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N;
  cin >> N;
  int64_t A[N + 1];
  map<int64_t, int> B;
  int64_t ans = 0;
  int64_t mn = 0;
  for (int i = 1; i < N + 1; i++) {
    cin >> A[i];
    ans += B[i - A[i]];
    B[i + A[i]]++;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  int64_t A[N + 1];
  int64_t B[1000];
  for (int i = 0; i < 1000; i++)
    B[i] = 0;
  int64_t ans = 0;
  int64_t mn = 0;
  for (int i = 1; i < N + 1; i++) {
    int64_t num;
    cin >> A[i];
    num = max(mn, i - A[i]);
    ans += B[num];
    B[i + A[i]]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define int long long int
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const int mod = 1e9 + 7;
const int N = 2e5 + 5;
const int inf = 1e16 + 2;

void solve(int z) {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    m[i - arr[i]]++;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += m[i + arr[i]];
    m[i - arr[i]]--;
  }
  cout << cnt;
}

/*
 */

int32_t main() {
  fastio int testcase = 1;
  // cin >> testcase;
  int x = 1;
  while (testcase--) {
    solve(x++);
  }

  return 0;
}


Error: TLE

Fixed submission:

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

#define int long long int
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const int mod = 1e9 + 7;
const int N = 2e5 + 5;
const int inf = 1e16 + 2;

void solve(int z) {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int j = i + arr[i];
    for (; j < n; j++) {
      if (arr[i] + arr[j] == (j - i))
        cnt++;
    }
  }
  cout << cnt;
}

/*
 */

int32_t main() {
  fastio int testcase = 1;
  // cin >> testcase;
  int x = 1;
  while (testcase--) {
    solve(x++);
  }

  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() {
  int n;
  cin >> n;
  map<int, int> mp;
  ll ans = 0;

  rep(i, n) {
    int a;
    cin >> a;
    ans += mp[i - a];
    mp[i + a]++;
  }

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


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;
  pair<ll, int> a[n];
  rep(i, n) {
    ll A;
    cin >> A;
    a[i] = make_pair(A, i);
  }
  sort(a, a + n);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ll height = a[i].first + a[j].first;
      ll len = abs(a[i].second - a[j].second);
      if (height == len)
        ans++;
    }
  }

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

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

using namespace std;

const int max_n = 200000;
int n;
int a[max_n + 1];

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

  // int map[max_n+1] = {0};
  // vector<int> map(max_n+1, 0);
  map<long long, int> M;

  long long counter = 0;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    counter += M[i - a];
    M[i + a]++;
  }

  cout << counter << endl;

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

const int max_n = 200000;
const int max_ai = 1000000000;
int n;
int a[max_n + 1];

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

  // int map[max_n+1] = {0};
  vector<int> map(max_n + max_ai + 1, 0);

  int counter = 0;
  for (int i = 0; i < n; i++) {
    if (i >= a[i])
      counter += map[i - a[i]];
    map[i + a[i]]++;
  }

  cout << counter << endl;

  return 0;
}