alexjercan/bugnet huggingface dataset:
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
#define FOR(i, begin, end) for (ll i = (begin); i < (end); i++)
#define rep(i, n) FOR(i, 0, n)
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
template <typename T, typename U> static void chmin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> static void chmax(T &x, U y) {
  if (x < y)
    x = y;
}
int n;
int c2(int x) { return x * (x - 1) / 2; }
signed main() {
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  vi b(a), c(n);
  rep(i, n) b[i] -= i;
  rep(i, n) c[i] = -a[i] - i;
  int ans = 0;
  unordered_map<int, int> m;
  rep(i, n) { m[b[i]]++; }
  rep(j, n) {
    if (b[j] != c[j])
      ans += m[c[j]];
    else
      ans += m[c[j]] - 1;
  }

  // for(auto x : m) {
  //     if(x.second>1)
  //     ans += c2(x.second);
  // }
  cout << ans << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, begin, end) for (ll i = (begin); i < (end); i++)
#define rep(i, n) FOR(i, 0, n)
using ll = long long;
using ull = unsigned long long;
using namespace std;
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
template <typename T, typename U> static void chmin(T &x, U y) {
  if (y < x)
    x = y;
}
template <typename T, typename U> static void chmax(T &x, U y) {
  if (x < y)
    x = y;
}
int n;
int c2(int x) { return x * (x - 1) / 2; }
signed main() {
  cin >> n;
  vi a(n);
  rep(i, n) cin >> a[i];
  vi b(a), c;
  rep(i, n) b[i] -= i;
  rep(i, n) c[i] = -a[i] + i;
  int ans = 0;
  unordered_map<int, int> m;
  rep(i, n) { m[b[i]]++; }
  rep(j, n) { ans += m[c[j]]; }
  ans -= m[0] / 2;
  // for(auto x : m) {
  //     if(x.second>1)
  //     ans += c2(x.second);
  // }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
map<ll, ll> m;
int main() {
  ll n;
  cin >> n;
  ll a[n], ans = 0ll;
  for (ll i = 0ll; i < n; i++)
    cin >> a[i];
  for (ll i = n - 1ll; i >= 0ll; i--) {
    ans += m[i + a[i]];
    m[i - a[i]]++;
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

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

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

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define reps(i, n) for (ll i = 1; i <= (n); ++i)
#define _rep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define _reps(i, n) for (ll i = n; i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define _all(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

  map<ll, ll> l, r;
  reps(i, n) {
    ll a;
    cin >> a;
    if (i + a <= n) {
      l[i + a]++;
    }
    if (i - a >= 0) {
      r[i - a]++;
    }
  }

  ll cnt = 0;
  for (ll i = 0; i <= n; i++) {
    cnt += (l[i] * r[i]);
  }

  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define reps(i, n) for (ll i = 1; i <= (n); ++i)
#define _rep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define _reps(i, n) for (ll i = n; i > 0; --i)
#define all(x) (x).begin(), (x).end()
#define _all(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using P = pair<ll, ll>;

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

  ll a[n + 1];
  reps(i, n) { cin >> a[i]; }

  ll cnt = 0;
  for (ll i = 1; i < n; i++) {
    for (ll j = i + a[i]; j <= n; j++) {
      if ((j - i) == (a[j] + a[i])) {
        cnt++;
      }
    }
  }

  cout << cnt << 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 + 1, 0);
  map<int, int> counts;

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

  long long int ans = 0;

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);

  int n;

  cin >> n;

  vector<int> a(n + 1, 0);

  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 ((j - i) == a[i] + a[j])
        ans++;
    }
  }

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

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

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

int main() {
  int n, tmp;
  cin >> n;
  vector<int> aj;
  vector<int> ai;
  aj.push_back(0);
  ai.push_back(0);
  rep(j, n) {
    cin >> tmp;
    aj.push_back(j + 1 - tmp);
    ai.push_back(j + 1 + tmp);
  }
  sort(aj.begin(), aj.end());
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (upper_bound(aj.begin(), aj.end(), ai[i]) - aj.begin()) -
           (lower_bound(aj.begin(), aj.end(), ai[i]) - aj.begin());
  }
  cout << ans << 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;

int main() {
  int n, a;
  int ans = 0;
  cin >> n;
  vector<int> height(n);
  rep(i, n) {
    cin >> a;
    height[i] = a;
  }

  rep(i, n) {
    for (int j = i + 1; j < n; j++) {
      if (height[i] + height[j] == j - i)
        ans++;
    }
  }

  cout << ans << endl;

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

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
  // your code goes here
  int n;
  cin >> n;
  vector<int> vec(n + 2, 0);
  for (int i = 1; i <= n; i++)
    cin >> vec[i];
  unordered_map<int, int> mp;
  for (int i = 1; i <= n; i++)
    mp[i + vec[i]]++;
  long long count = 0;
  for (int i = 1; i <= n; i++)
    count += mp[i - vec[i]];
  cout << count;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  // your code goes here
  int n;
  cin >> n;
  vector<int> vec(n + 2, 0);
  for (int i = 1; i <= n; i++)
    cin >> vec[i];
  int count = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if ((j - i) == vec[i] + vec[j])
        count++;
    }
  }
  cout << count;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES // M_PIなどの定義を使用するのに必要
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;
int64_t table[200005] = {0};

int main() {
  int64_t N;
  vector<int64_t> A;

  cin >> N;
  A.resize(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A[i];
  }

  // int64_t ans = 0;
  // for (int64_t i = 0; i < N - 1; i++) {
  //   for (int64_t k = i + 1; k < N; k++) {
  //     if (k - i == A[i] + A[k]) {
  //       ans++;
  //     }
  //   }
  // }

  int64_t ans = 0;
  for (int64_t i = 0; i < N; i++) {
    if (i - A[i] > 0) {
      ans += table[i - A[i]];
    }
    if (i + A[i] < N) {
      table[i + A[i]]++;
    }
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES // M_PIなどの定義を使用するのに必要
#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <numeric>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int64_t N;
  vector<int64_t> A;

  cin >> N;
  A.resize(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A[i];
  }

  int64_t ans = 0;
  for (int64_t i = 0; i < N - 1; i++) {
    for (int64_t k = i + 1; k < N; k++) {
      if (k - i == A[i] + A[k]) {
        ans++;
      }
    }
  }

  cout << ans << 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++)

typedef long long ll;

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

  rep(i, n) {
    cin >> a[i];
    if (a[i] - (i + 1) < 0)
      b.push_back(-a[i] + (i + 1));
    a[i] += (i + 1);
  }
  sort(a, a + n);
  sort(b.begin(), b.end());
  int m = b.size();

  ll ans = 0;
  ll tmp = 0;
  ll cc = 0;
  rep(i, n) {
    if (i >= 1 && a[i] == a[i - 1])
      ans += cc;
    else {
      cc = 0;
      for (int j = tmp; j < m; ++j) {
        if (b[j] == a[i])
          cc++;
        else if (b[j] < a[i])
          tmp = j + 1;
        else
          break;
      }
      ans += cc;
    }
  }

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

Error: TLE

Fixed submission:

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

typedef long long ll;

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

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

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

ll inf = 1e9 + 7;

#define pi 3.14159265358979323846L
bool isprime(ll a) {

  ll cnt = 0;
  for (ll i = 1; i * i < a; i++) {

    if (a % i == 0) {
      cnt += 2;
    }
  }

  if (cnt == 2)
    return true;
  else
    return false;
}
ll mod(ll a) {
  if (a < inf)
    return a;
  else
    return a % inf;
}
void solve() {

  ll n;
  cin >> n;

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

  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += m[b[i]];
  }

  cout << ans;
}

int main() {

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

  solve();

  return 0;
}


Error: TLE

Fixed submission:

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

ll inf = 1e9 + 7;

#define pi 3.14159265358979323846L
bool isprime(ll a) {

  ll cnt = 0;
  for (ll i = 1; i * i < a; i++) {

    if (a % i == 0) {
      cnt += 2;
    }
  }

  if (cnt == 2)
    return true;
  else
    return false;
}
ll mod(ll a) {
  if (a < inf)
    return a;
  else
    return a % inf;
}
void solve() {

  ll n;
  cin >> n;
  vector<ll> v(n + 1);

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

  ll cnt = 0;
  for (ll i = 1; i <= n; i++) {
    for (ll j = i + v[i] + 1; j <= n; j++) {
      if (v[i] + v[j] == j - i)
        cnt++;
    }
  }

  cout << cnt;
}

int main() {

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

  solve();

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

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define N 200005

int n, i, a[N], j;
LL ans;
map<int, int> S;

int main(void) {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  ans = 0;
  for (i = 1; i <= n; i++)
    S[i - a[i]]++;
  for (i = 1; i <= n; i++) {
    S[i - a[i]]--;
    ans += S[a[i] + i];
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LL long long
#define N 200005

int n, i, a[N], j;
LL ans;
multiset<int> S;

int main(void) {
  scanf("%d", &n);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  ans = 0;
  for (i = 1; i <= n; i++)
    S.insert(i - a[i]);
  for (i = 1; i <= n; i++) {
    S.erase(S.find(i - a[i]));
    ans += S.count(a[i] + i);
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <map>

using namespace std;
#define ll long long
const int MAX = 2e5 + 111;
int n;
ll h[MAX];
ll ans = 0;
map<ll, ll> p;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    ll m = i - h[i];
    if (p.count(m) != 0)
      ans += p[m];
    p[i + h[i]]++;
  }
  printf("%lld\\n", ans);
}


Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>

using namespace std;
#define ll long long
const int MAX = 1e5 + 111;
int n;
ll h[MAX];
ll ans = 0;
map<ll, ll> p;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    ll m = i - h[i];
    if (p.count(m) != 0)
      ans += p[m];
    p[i + h[i]]++;
  }
  printf("%lld\\n", ans);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

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

  map<ll, int> m;
  ll ans = 0;

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    ans += m[i - a];
    m[i + a]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

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

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

  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    int d = plus[i];
    auto itr = lower_bound(minus.begin(), minus.end(), d);
    while (*itr == d) {
      cnt++;
      itr++;
    }
  }

  cout << cnt << 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 repbtw(i, a, b) for (int i = a; i <= (int)(b); i++)
#define all(v) v.begin(), v.end()
#define pr(x) cout << x << endl

int main() {
  int64_t n;
  cin >> n;
  int64_t a;
  vector<pair<int64_t, int64_t>> ends(n, pair<int64_t, int64_t>(0, 0));
  rep(i, n) {
    cin >> a;
    int64_t l = i - a;
    if (l >= 0)
      ends.at(l).second++;
    int64_t r = i + a;
    if (r < n)
      ends.at(r).first++;
  }

  int64_t cnt = 0;
  rep(i, n) { cnt += ends.at(i).first * ends.at(i).second; }

  pr(cnt);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repbtw(i, a, b) for (int i = a; i <= (int)(b); i++)
#define all(v) v.begin(), v.end()
#define pr(x) cout << x << endl

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a.at(i); }

  int cnt = 0;
  repbtw(i, 0, n - 2) {
    repbtw(j, i + 1, n - 1) {
      if (j - i == a.at(i) + a.at(j))
        cnt++;
    }
  }

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

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

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

  map<long long, long long> L, R;
  for (int i = 0; i < N; i++) {
    L[(i + 1) + A[i]]++;
    R[(i + 1) - A[i]]++;
  }
  long long ans = 0;
  for (auto l : L) {
    ans += l.second * R[l.first];
  }
  cout << ans << endl;
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

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

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

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

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
const int maxn = 2e5 + 5;
ll a[maxn];
map<ll, ll> mp;

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
const int maxn = 1e5 + 5;
ll a[maxn];
map<ll, ll> mp;

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mp[-(a[i] + i)]++;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += mp[a[i] - 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 vector<int> VI;
ll mm = 1000000000;
ll MM = mm + 7;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n;
  cin >> n;
  vector<ll> h(n);
  map<ll, int> mp;
  rep(i, n) {
    cin >> h.at(i);
    if (mp.count(i + h.at(i))) {
      mp.at(i + h.at(i))++;
    } else {
      mp[i + h.at(i)] = 1;
    }
  }
  ll sum = 0;
  rep(i, n) {
    if (mp.count(i - h.at(i))) {
      sum += mp.at(i - h.at(i));
    }
  }

  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> VI;
ll mm = 1000000000;
ll MM = mm + 7;
#define rep(i, n) for (int i = 0; i < n; i++)

int main() {
  int n;
  cin >> n;
  vector<ll> h(n);
  rep(i, n) cin >> h.at(i);
  ll sum = 0;
  rep(i, n) {
    if (i + h.at(i) + 1 >= n)
      continue;
    for (int j = i + h.at(i) + 1; j < n; j++) {
      if (j - i == h.at(i) + h.at(j))
        sum++;
    }
  }
  cout << sum << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  long long int N, ans = 0;
  cin >> N;
  vector<long long int> A(N);
  map<long long int, long long int> tas, hiku;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    tas[A[i] + i + 1]++;
    hiku[i + 1 - A[i]]++;
  }

  for (int i = 0; i < N; i++) {
    hiku[i + 1 - A[i]]--;
    ans += hiku[A[i] + i + 1];
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

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

#include <cstdio>
#include <map>
using namespace std;

const int N = 2e5 + 5;

int n, a[N];
map<int, int> ma;

int read() {
  int x = 0, f = 1;
  char s;
  while ((s = getchar()) < '0' || s > '9')
    if (s == '-')
      f = -1;
  while (s >= '0' && s <= '9') {
    x = (x << 1) + (x << 3) + (s ^ 48);
    s = getchar();
  }
  return x * f;
}

int main() {
  long long ans = 0;
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read(), ++ma[i - a[i]];
  for (int i = 1; i <= n; ++i)
    ans += ma[i + a[i]];
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;

const int N = 2e5 + 5;

int n, a[N];

int read() {
  int x = 0, f = 1;
  char s;
  while ((s = getchar()) < '0' || s > '9')
    if (s == '-')
      f = -1;
  while (s >= '0' && s <= '9') {
    x = (x << 1) + (x << 3) + (s ^ 48);
    s = getchar();
  }
  return x * f;
}

int main() {
  long long ans = 0;
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 1; i < n; ++i)
    for (int j = 1; j <= n - i; ++j)
      if (a[j] + a[j + i] == i)
        ++ans;
  printf("%lld\\n", ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pint;
typedef vector<int> vint;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rep3(elem, vec) for (auto elem : vec)
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
#define COUT(N) cout << fixed << setprecision(N)
lint gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
lint lcm(int a, int b) { return a * b / gcd(a, b); }
lint comb(int a, int b) { return b ? comb(a - 1, b - 1) * a / b : 1; }
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int MOD = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  vint A(N), B(N, 0);
  rep(i, N) cin >> A[i];
  rep(i, N) {
    if (i - A[i] >= 0)
      B[i - A[i]]++;
  }
  lint a = 0;
  rep(i, N) {
    if (i + A[i] < N)
      a += B[i + A[i]];
  }
  cout << a;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pint;
typedef vector<int> vint;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, m, n) for (int i = m; i < n; i++)
#define rep3(elem, vec) for (auto elem : vec)
#define SORT(V) sort((V).begin(), (V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
#define COUT(N) cout << fixed << setprecision(N)
lint gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
lint lcm(int a, int b) { return a * b / gcd(a, b); }
lint comb(int a, int b) { return b ? comb(a - 1, b - 1) * a / b : 1; }
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int MOD = 1e9 + 7;

int main() {
  int N;
  cin >> N;
  vint A(N);
  rep(i, N) cin >> A[i];
  int a, b = 0;
  rep(i, N) {
    for (int j = i + A[i] + 1; j < N; j++) {
      if (j - i == A[i] + A[j])
        b++;
    }
  }
  cout << b;
  return 0;
}