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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <vector>
using namespace std;
#define ll long long

int main() {

  int n, x;
  cin >> n;
  map<ll, ll> _map;
  map<ll, ll> _nap;
  set<ll> _set;

  for (int i = 1; i <= n; i++) {
    cin >> x;
    _map[i + x] += 1;
    _nap[i - x] += 1;
    _set.insert(i + x);
  }
  ll ans = 0;
  set<ll>::iterator it = _set.begin();
  for (; it != _set.end(); ++it) {
    ans += _map[*it] * _nap[*it];
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using Graph = vector<vector<int>>;

int main() {
  ll N;
  cin >> N;
  ll res = 0;
  vector<ll> A(N + 1);
  rep1(i, N) cin >> A.at(i);
  map<ll, set<ll>> L;
  map<ll, vector<ll>> R;
  rep1(i, N) {
    L[i - A.at(i)].insert(i);
    R[i + A.at(i)].push_back(i);
  }
  for (auto p : R) {
    sort(begin(p.second), end(p.second));
  }

  for (auto p : L) {
    if (!R.count(p.first))
      continue;
    else {

      for (auto i_l : p.second) {
        auto iter = lower_bound(begin(R[p.first]), end(R[p.first]), i_l);
        res += (ll)distance(begin(R[p.first]), iter);
      }
    }
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using Graph = vector<vector<int>>;

int main() {
  ll N;
  cin >> N;
  ll res = 0;
  vector<ll> A(N + 1);
  rep1(i, N) cin >> A.at(i);
  rep1(i, N) {
    for (ll j = min(N, i + 1 + A.at(i)); j <= N; j++) {
      if (i == j)
        continue;
      if (fabs(i - j) == A.at(i) + A.at(j)) {
        res++;
      }
    }
  }
  cout << res << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void solve(long long N, std::vector<long long> H) {
  long long cnt = 0;
  unordered_map<long, long> m1, m2;
  // I < J, I = i + H[i], J = j - H[j]を満たすi, jを探せば良い
  for (long long i = 0; i < N; i++) {
    cnt += m1[i + H[i]];
    cnt += m2[i - H[i]];
    m2[i + H[i]]++;
    m1[i - H[i]]++;
  }
  cout << cnt << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> H(N);
  for (long long i = 0; i < N; i++) {
    scanf("%lld", &H[i]);
  }
  solve(N, std::move(H));
  return 0;
}


Error: TLE

Fixed submission:

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

void solve(long long N, std::vector<long long> H) {
  long long cnt = 0;
  unordered_map<int, int> m;
  for (long long j = 0; j < N; j++)
    m[j] = j - H[j];
  // I < J, I = i + H[i], J = j - H[j]を満たすi, jを探せば良い
  for (long long i = 0; i < N - 1; i++) {
    for (long long j = i + H[i]; j < N; j++)
      if (i + H[i] == m[j])
        cnt++;
  }
  cout << cnt << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  std::vector<long long> H(N);
  for (long long i = 0; i < N; i++) {
    scanf("%lld", &H[i]);
  }
  solve(N, std::move(H));
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  // i + A[i] = X && j - A[j] = X となる組(i, j) (i < j) の個数を求める
  map<long long, long long> L; // L[X] : i + A[i] = X となるiの個数
  map<long long, long long> R; // R[X] : j - A[j] = X となるjの個数
  for (int i = 0; i < N; ++i) {
    long long leftval = i + A[i];
    ++L[leftval];
    long long rightval = i - A[i];
    if (rightval >= 0)
      ++R[rightval];
  }

  long long ans = 0;
  for (auto itr = L.begin(); itr != L.end(); ++itr) {
    long long ind = itr->first;
    ans += L[ind] * R[ind];
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long> A(N);
  for (int i = 0; i < N; ++i) {
    long long at;
    cin >> at;
    A[i] = at;
  }
  long long ans = 0;
  vector<long long> res(N, 0);
  for (int i = 0; i < N; ++i) {
    for (int j = i + 1; j < N; ++j) {
      long long sum = A[i] + A[j];
      if (sum >= 0 && sum <= N - 1 && sum == abs(j - i)) {
        ++res[sum];
      }
    }
  }
  for (auto r : res) {
    ans += r;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#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
int main() {
  ll a[MAX] = {0}, b[MAX] = {0};
  int n;
  ll sum = 0;
  ll t;
  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: WA

Fixed submission:

#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 100005
int a[MAX] = {0}, b[MAX] = {0};
int main() {
  int n;
  int sum = 0, t;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> t;
    if (i + t < 100000)
      a[i + t]++;
    if (i - t >= 1)
      b[i - t]++;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] != 0 && b[i] != 0)
      sum += max(a[i], b[i]);
  }
  cout << sum;
  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 P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  ll ans = 0;
  rep(i, n) {
    int sa = i - a[i];
    // i<j  j-i==Ai+Aj  Ai+i==j-Aj  Ai+iはmpに入れていくと計算時間を減らせる
    ans += mp[sa];
    int wa = a[i] + i;
    mp[wa]++;
  }
  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;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<ll> A(n);
  rep(i, n) cin >> A[i];
  int ans = 0;
  rep(i, n - 1) {
    for (int j = i + 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;

int main() {
  long long N;
  cin >> N;
  long long A, right, left;
  map<long long, long long> lt;
  long long loop, ans = 0;
  for (loop = 0; loop < N; loop++) {
    cin >> A;
    right = loop - A + 1;
    if (lt.count(right)) {
      ans += lt[right];
    }
    left = loop + A + 1;
    if (lt.count(left)) {
      lt[left]++;
    } else {
      lt[left] = 1;
    }
  }
  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);
  int loop, loop2;
  int max1 = 0, max2 = 0;
  int maxrange, ans = 0;
  for (loop = 0; loop < N; loop++) {
    cin >> A[loop];
    if (A[loop] > max1) {
      max1 = A[loop];
    } else if (A[loop] > max2) {
      max2 = A[loop];
    }
    maxrange = min(loop + 1,
                   max1 == A[loop] ? A[loop] + max2 + 1 : A[loop] + max1 + 1);
    for (loop2 = loop - 1; loop2 > loop - maxrange; loop2--) {
      if (loop - loop2 == A[loop] + A[loop2])
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  fastio ll n;
  cin >> n;
  ll a[n];
  rep(i, 0, n - 1) cin >> a[i];
  ll dp[n + 1];
  rep(i, 0, n) dp[i] = 0;
  ll ans = 0;
  rep(i, 0, n - 1) {
    if (0 <= i - a[i] && i - a[i] <= n) {
      ans += dp[i - a[i]];
    }
    if (0 <= a[i] + i && a[i] + i <= n) {
      dp[a[i] + i]++;
    }
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

int main() {
  fastio ll n;
  cin >> n;
  ll a[n];
  rep(i, 0, n - 1) cin >> a[i];
  ll dp[n + 1];
  rep(i, 0, n) dp[i] = 0;
  ll ans = 0;
  rep(i, 0, n - 1) {
    if (0 <= i - a[i] && i - a[i] <= n) {
      ans += dp[i - a[i]];
    }
    dp[a[i] + i]++;
  }
  cout << ans << endl;

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

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

int n, x;
map<int, int> f;
long long ans;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    ans += f[x - i];
    f[-i - x]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

ll n, cur = 1, ans;
struct peo {
  ll a;
  int num;
} f[200005];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    ll tmp;
    cin >> tmp;
    if (tmp < n) {
      f[cur].a = tmp;
      f[cur].num = i;
      cur++;
    }
  }
  for (int i = 1; i <= cur - 1; i++) {
    for (int j = i + 1; j <= cur - 1; j++) {
      if (f[i].a + f[j].a == abs(f[i].num - f[j].num))
        ans++;
    }
  }
  /*for(int i=1;i<=cur;i++){
          cout<<f[i].a<<" "<<f[i].num<<endl;
  }*/
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#include <map>
#define int long long

const int maxn = 200005;

int n, A[maxn], cnt;
std::map<int, int> count;

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

signed main() {
  n = read();
  for (int i = 1; i <= n; ++i) {
    A[i] = read();
    if (i - A[i] >= 0)
      ++count[i - A[i]];
  }
  for (int i = 1; i <= n; ++i)
    cnt += count[i + A[i]];
  printf("%lld\\n", cnt);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

const int maxn = 200005;

int n, A[maxn], cnt;

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

int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    A[i] = read();
  for (int i = 1; i <= n; ++i)
    for (int j = i + A[i]; j <= n; ++j) {
      if (i == j)
        continue;
      if (A[i] + A[j] == j - i)
        ++cnt;
    }
  printf("%d\\n", cnt);
  return 0;
}
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<int, int> x, y;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    x[i - a]++;
    y[i + a]++;
  }

  ll sum = 0;

  for (int cur = 0; cur <= 200000; cur++) {
    sum += (ll)x[cur] * y[cur];
  }

  cout << sum << endl;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

int main() {
  int n = ri();
  int a[200005];

  for (int i = 0; i < n; i++)
    a[i] = ri();

  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {

      cnt += (j - i == a[i] + a[j]);
    }
  }
  cout << cnt << endl;
}
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;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  map<ll, ll> mp;
  for (int i = 0; i < n; i++)
    mp[a[i] - i]++;

  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += mp[-a[i] - i];
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>

using namespace std;
using ll = long long;

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

int main() {
  int n = ri();
  int a[200005];

  for (int i = 0; i < n; i++)
    a[i] = ri();

  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {

      cnt += (j - i == a[i] + a[j]);
    }
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
const long long mod = 1e9 + 7;
//////////////////////////////////

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> x(n);
  vector<int> y(n);
  map<int, int> mx;
  map<int, int> my;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    x[i] = i + 1 + a[i];
    y[i] = i + 1 - a[i];
    mx[x[i]]++;
    my[y[i]]++;
  }
  sort(all(x));
  sort(all(y));
  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    cnt += my[x[i]];
  }
  cout << cnt << endl;

  return 0;
}

// EOF


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
const long long mod = 1e9 + 7;
//////////////////////////////////

long long f(vector<int> x) {
  int n = x.size();
  int cnt = 0;
  vector<map<int, int>> vm(n);
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    m[i + x[i]]++;
    vm[i] = m;
  }
  for (int i = 0; i < n; i++) {
    cnt += vm[i][i - x[i]];
  }
  return cnt;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> x;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cout << f(a) << endl;

  return 0;
}

// EOF
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#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>;
const int iINF = 1001001001;
const long long llINF = 1LL << 60;
int main() {
  // ABC166-E, 1007,
  int n;
  cin >> n;
  ll ans = 0;
  vector<int> a(n);
  map<int, int> mp;
  rep(i, n) {
    cin >> a[i];
    int b = i + a[i];
    mp[b]++;
  }
  rep(i, n) { ans += mp[i - a[i]]; }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string>
#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>;
const int iINF = 1001001001;
const long long llINF = 1LL << 60;
int main() {
  // ABC166-E, 1007,
  int n;
  cin >> n;
  ll ans = 0;
  vector<int> a(n);
  rep(i, n) {
    cin >> a[i];
    int b = i - a[i] + 1;
    if (b < 0)
      continue;
    for (int j = b; j >= 0; --j) {
      if (i - j == a[i] + a[j])
        ans++;
    }
  }
  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 < (int)(n); i++)
//----------------------------------------------------------------------

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

int main(void) {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  map<ll, int> freq;
  rep(i, n) cin >> a[i + 1];

  for (int i = 1; i <= n; ++i) {
    ll tmp = i - a[i];
    if (tmp > 0) {
      freq[tmp]++;
    }
  }

  ll ans = 0;
  for (int i = 1; i <= n; ++i) {
    ll tmp = i + a[i];
    if (tmp < n) {
      ans += freq[tmp];
    }
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

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

  ll ans = 0;
  rep(i, n) {
    int tmp = a[i];
    for (int j = i + tmp + 1; j < n; ++j) {
      if (j - i == tmp + a[j])
        ans++;
    }
  }
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
int N, A[200005], B[200005];
long long ans;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i - A[i] > 0) {
      B[i - A[i]]++;
    }
  }
  for (int i = 0; i < N; i++) {
  }
  for (int i = 0; i < N; i++) {
    if (i + A[i] < 200005) {
      ans += B[i + A[i]];
    }
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int N, A[200005], B[200005];
long long ans;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i - A[i] > 0) {
      B[i - A[i]]++;
    }
  }
  for (int i = 0; i < N; i++) {
  }
  for (int i = 0; i < N; i++) {
    ans += B[i + A[i]];
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long int;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

ll a[225816];
std::map<ll, ll> mp;

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <vector>

using ll = long long int;
const int INF = (1 << 30);
const ll INFLL = (1ll << 60);
const ll MOD = (ll)(1e9 + 7);

#define l_ength size

void mul_mod(ll &a, ll b) {
  a *= b;
  a %= MOD;
}

void add_mod(ll &a, ll b) {
  a = (a < MOD) ? a : (a - MOD);
  b = (b < MOD) ? b : (b - MOD);
  a += b;
  a = (a < MOD) ? a : (a - MOD);
}

ll a[225816];
std::multiset<ll> st;

int main(void) {
  int n, i;
  ll ans = 0ll;
  std::cin >> n;
  for (i = 0; i < n; ++i) {
    std::cin >> a[i];
    ans += st.count(i - a[i]);
    st.insert(a[i] + i);
  }
  std::cout << ans << std::endl;
  return 0;
}
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>;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
const ll INF = 999999999999999;

int main() {
  ll n;
  cin >> n;
  map<ll, ll> s;
  map<ll, ll> t;
  vector<ll> m;
  for (ll i = 1; i <= n; ++i) {
    ll a;
    cin >> a;
    s[i - a]++;
    if (s[i - a] == 1)
      m.push_back(i - a);
    t[i + a]++;
  }
  ll ans = 0;
  for (ll i = 0; i < (ll)s.size(); ++i) {
    ans += ((ll)s[m.at(i)]) * ((ll)t[m.at(i)]);
  }

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

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ll n;
  cin >> n;
  vector<ll> s;
  vector<ll> t;
  for (ll i = 1; i <= n; ++i) {
    ll a;
    cin >> a;
    s.push_back(i - a);
    t.push_back(i + a);
  }
  ll ans = 0;
  if (s.size() > t.size()) {
    for (ll i = 0; i < t.size(); ++i) {
      ans += count(s.begin(), s.end(), t.at(i));
    }
  } else {
    for (ll i = 0; i < s.size(); ++i) {
      ans += count(t.begin(), t.end(), s.at(i));
    }
  }
  cout << ans << "\\n";

  return 0;
}