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

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

int main() {
  int64_t N;
  cin >> N;
  vector<int64_t> A(N), B(N), C(N);
  for (int64_t i = 0; i < N; i++) {
    cin >> A.at(i);
    B.at(i) = A.at(i) + i;
    C.at(i) = i - A.at(i);
  }
  map<int64_t, int64_t> mB, mC;
  for (int64_t i = 0; i < N; i++) {
    mB[B.at(i)] = mB[B.at(i)] + 1;
    mC[C.at(i)] = mC[C.at(i)] + 1;
  }
  int64_t Ans = 0;
  for (int64_t i = 0; i < N; i++) {
    Ans += mC[B.at(i)];
  }
  cout << Ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int64_t N;
  cin >> N;
  vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
    B.at(i) = A.at(i) + i;
    C.at(i) = i - A.at(i);
  }
  int Ans = 0;
  for (int i = 0; i < N; i++) {
    Ans += count(C.begin() + i, C.end(), B.at(i));
  }
  cout << Ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(v) (v).begin(), (v).end()

const int INF = 1001001001;

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

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

  cout << ans << endl;
}

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;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
#define all(v) (v).begin(), (v).end()

const int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  vl a1(n), a2(n);
  rep(i, n) {
    ll t;
    cin >> t;
    a1[i] = abs(t - (i + 1));
    a2[i] = t + (i + 1);
  }
  int v = n + 1001001001;
  vl d(v, 0);
  ll ans = 0;
  rep(i, n) {
    ans += d[a1[i]];
    d[a2[i]]++;
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  long long ans = 0;
  cin >> n;
  vector<int> a(n + 1);
  map<int, int> mp;
  for (int i = 1; i <= n; i++) {
    cin >> a.at(i);
    if (mp.count(i + a.at(i)))
      mp[i + a.at(i)]++;
    else
      mp[i + a.at(i)] = 1;
  }

  for (int i = 1; i <= n; i++) {
    int x = i - a.at(i);
    if (x <= 0)
      continue;
    else
      ans += mp[x];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, ans = 0, j = 0, d = 0;
  cin >> n;
  vector<int> a(n), b(n);

  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    b.at(i) = i + 1 - a.at(i);
    a.at(i) = i + 1 + a.at(i);
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  // bの下限は制限したがaの上限は制限してない
  for (int i = 0; i < n; i++) {
    if (b.at(i) >= a.at(0)) {
      j = 0;
      while (b.at(i) >= a.at(j)) {
        if (b.at(i) == a.at(j))
          ans++;
        j++;
        if (j >= n) {

          break;
        }
      }
    }
  }

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

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

typedef long long ll;
typedef long double ld;
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  ll ans = 0;
  map<ll, ll> dist, sum;
  for (int i = 0; i < N; i++) {
    ll d = i - A.at(i);
    ll s = i + A.at(i);
    if (dist.count(d)) {
      dist.at(d)++;
    } else {
      dist[d] = 1;
    }
    if (sum.count(s)) {
      sum.at(s)++;
    } else {
      sum[s] = 1;
    }
  }
  for (int i = 0; i < N; i++) {
    if (dist.count(i) && sum.count(i)) {
      ans += dist.at(i) * sum.at(i);
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef long double ld;
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + A.at(i) + 1; j < N; j++) {
      if (j - i == A.at(i) + A.at(j)) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (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>
using namespace std;
typedef long long int ll;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void P(int x) { cout << x << endl; }
void P(long x) { cout << x << endl; }
void P(double x) { cout << x << endl; }
void P(ll x) { cout << x << endl; }
void P(string x) { cout << x << endl; }
void P(char x) { cout << x << endl; }

#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define drep(i, n) for (int i = n - 1; i >= 0; i--)

const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

int main() {
  int n;
  cin >> n;
  vector<map<ll, int>> v(n);
  vector<int> vT(n);
  vector<int> origin(n);

  map<ll, int> mp;
  ll ans = 0;
  rep(i, n) {
    int tmp;
    cin >> tmp;

    ll target = tmp - i - 1;
    auto itr = mp.find(target);
    if (itr != mp.end())
      ans += mp[target];

    ll inNum = (tmp + i + 1) * -1;
    itr = mp.find(inNum);
    if (itr != mp.end())
      mp[inNum]++;
    else
      mp[inNum] = 1;
  }
  P(ans);
  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>
using namespace std;
typedef long long int ll;

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void P(int x) { cout << x << endl; }
void P(long x) { cout << x << endl; }
void P(double x) { cout << x << endl; }
void P(ll x) { cout << x << endl; }
void P(string x) { cout << x << endl; }
void P(char x) { cout << x << endl; }

#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define drep(i, n) for (int i = n - 1; i >= 0; i--)

const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;

int main() {
  int n;
  cin >> n;
  vector<map<ll, int>> v(n);
  vector<int> vT(n);
  vector<int> origin(n);
  rep(i, n) {
    int tmp;
    cin >> tmp;
    origin[i] = tmp;
    tmp -= (i + 1);
    vT[i] = tmp;
    auto itr = v[n - 1].find(tmp);
    if (itr != v[n - 1].end())
      v[n - 1][tmp]++;
    else
      v[n - 1][tmp] = 1;
  }

  v[0] = v[n - 1];
  v[0][vT[0]]--;
  for (int i = 1; i < n; i++) {
    v[i] = v[i - 1];
    v[i][vT[i]]--;
  }

  // rep (i, n) {
  //   cout << i << ": " << endl;
  //   for (auto itr = v[i].begin(); itr != v[i].end(); itr++) {
  //     cout << "\\t" << itr->first << " => " << itr->second << endl;
  //   }
  // }

  int ans = 0;
  rep(i, n) { ans += v[i][(origin[i] + i + 1) * -1]; }
  P(ans);

  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;

  vector<long long> T(N, 0);
  map<long long, long long> A;

  for (long long i = 0; i < N; i++) {
    cin >> T.at(i);
    A[T.at(i) + i]++;
  }

  long long count = 0;

  for (long long i = 0; i < N; i++) {
    count += A[i - T.at(i)];
  }
  cout << count << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {

  int N;
  cin >> N;

  vector<int> T(N, 0);

  for (int i = 0; i < N; i++) {
    cin >> T.at(i);
  }

  int count = 0;

  for (int i = 0; i < N - 1; i++) {
    for (int j = 0; j < N - i - 1; j++) {
      if (T.at(i) + T.at(j + i + 1) == j + 1)
        count += 1;
    }
  }

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

#define _USE_MATH_DEFINES

// #include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

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

// int 最大値は2147483647 > 10^9
// ll gojou[200];

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

  ll max_h = 0;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    if (max_h < a[i])
      max_h = a[i];
    // max_h = max(a[i],max_h);
  }

  ll ans = 0;
  // printf("1 1");return 0; //RE箇所探索用
  map<ll, ll> l;
  map<ll, ll> r;

  /*
  vector<ll> l(n+max_h),r(n+max_h);
  rep(i,n+max_h) l[i]=0;
  rep(i,n+max_h) r[i]=0;
  */
  rep(i, n) {
    if (i - a[i] >= 0)
      l[i - a[i]]++;
    r[i + a[i]]++;
  }
  for (auto itr = l.begin(); itr != l.end(); ++itr) {
    ans += itr->second * r[itr->first];
  }
  cout << ans << endl;
  return 0;
  /*
      rep(i,n+max_h) ans+=l[i]*r[i];
      cout<<ans<<endl;
      return 0;
      */

} // end of main

// 実行は、Opt + Ctl + N

// pythonの入力
// s, w = map(int, input().split())


Error: WA

Fixed submission:

#define _USE_MATH_DEFINES

// #include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <vector>

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

// int 最大値は2147483647 > 10^9
// ll gojou[200];

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

  ll max_h = 0;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
    if (max_h < a[i])
      max_h = a[i];
    // max_h = max(a[i],max_h);
  }

  ll ans = 0;

  vector<ll> l(n + max_h), r(n + max_h);
  printf("1 1");
  return 0;
  rep(i, n + max_h) l[i] = 0;
  rep(i, n + max_h) r[i] = 0;
  rep(i, n) {
    if (i - a[i] >= 0)
      l[i - a[i]] += 1;
    r[i + a[i]] += 1;
  }
  rep(i, n + max_h) ans += l[i] * r[i];
  cout << ans << endl;
  return 0;

} // end of main

// 実行は、Opt + Ctl + N

// pythonの入力
// s, w = map(int, input().split())
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <vector>

void stupid() {
  int N;
  std::cin >> N;
  std::vector<long long> A(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
  }
  long long ans = 0;
  for (int i = 1; i < N; ++i) {
    for (int j = 0; (j + i) < N; ++j) {
      if (A[j] + A[i + j] == i) {
        ans++;
      }
    }
  }
  std::cout << ans << std::endl;
}

void correct() {
  int N;
  std::cin >> N;
  std::vector<long long> A(N);
  long long maxA, minA;
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
    if (i == 0) {
      maxA = A[0];
      minA = A[0];
    } else if (maxA < A[i]) {
      maxA = A[i];
    } else if (minA > A[i]) {
      minA = A[i];
    }
  }
  std::map<long long, long long> L, R;
  for (int i = 0; i < N; ++i) {
    L[i + 1 + A[i]]++;
    R[i + 1 - A[i]]++;
  }
  unsigned long long ans = 0;
  for (int x = minA; x <= N - minA; ++x) {
    // std::cout<<"x"<<x<<"r"<<R[x]<<"l"<<L[x]<<std::endl;
    ans += L[x] * R[x];
  }
  std::cout << ans << std::endl;
  return;
}

int main() { correct(); }

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  std::vector<long long> A(N);
  for (int i = 0; i < N; ++i) {
    std::cin >> A[i];
  }
  long long ans = 0;
  for (int i = 1; i < N; ++i) {
    for (int j = 0; (j + i) < N; ++j) {
      if (A[j] + A[i + j] == i) {
        ans++;
      }
    }
  }
  std::cout << ans << std::endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
typedef long long ll;
const int mod = 1e9 + 7;
using namespace std;

ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll qpow(ll a, ll b, ll p) {
  a %= p;
  ll ret = 1;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1)
      ret = ret * a % p;
  return ret;
}
ll getInv(ll x, ll p) { return qpow(x, p - 2, p); }

int main() {
  ll n;
  cin >> n;
  vector<ll> kkk(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> kkk[i];
  }

  map<ll, int> hhh;

  for (int i = 1; i <= n; i++) {

    if (hhh[kkk[i] - i]) {
      hhh[kkk[i] - i]++;
    }

    else
      hhh[kkk[i] - i] = 1;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (hhh[-kkk[i] - i]) {
      ans += hhh[-kkk[i] - i];
    }
  }
  cout << ans << '\\n';
}

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;
typedef long long ll;
typedef unsigned long long ull;
const int mod = 7 + 1e9;
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll qpow(ll a, ll b, ll p) {
  a %= p;
  ll ret = 1;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1)
      ret = ret * a % p;
  return ret;
}
ll getInv(ll x, ll p) { return qpow(x, p - 2, p); }

int main() {
  ll n;
  scanf("%d", &n);
  vector<ll> kkk(n + 1);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &kkk[i]);
  }

  map<ll, int> hhh;

  for (int i = 1; i <= n; i++) {

    if (hhh[kkk[i] - i]) {
      hhh[kkk[i] - i]++;
    }

    else
      hhh[kkk[i] - i] = 1;
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    if (hhh[-kkk[i] - i]) {
      ans += hhh[-kkk[i] - i];
    }
  }
  printf("%lld\\n", ans);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <map>
#include <stdio.h>
#define maxn 200005

using namespace std;
map<int, int> f;
long long ans;
int main() {
  int n, i, j, m;
  int num[maxn];
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
  }
  for (i = 1; i <= n; i++) {
    ans += f[i - num[i]];
    f[i + num[i]]++;
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#define maxn 200005
int main() {
  int n, i, j, cnt = 0;
  int num[maxn];
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%d", &num[i]);
  }
  for (i = 1; i <= n; i++) {
    for (j = i + num[i]; j <= n; j++) {
      if ((j - i) == (num[i] + num[j])) {
        cnt++;
      }
    }
  }
  printf("%d\\n", cnt);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i > b; --i)
#define sq(a) (a) * (a)
#define nitro                                                                  \\
  std::ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define debug(x) std::cerr << #x << "=" << x << '\\n'
#define text std::cerr << "SacredCoupoid08\\n"
#define endl '\\n'
#define all(c) (c).begin(), (c).end()
#define U 1000000007
#define N 100005
#define sz(c) (int)c.size()
typedef long long ll;
typedef std::vector<int> vi;
typedef std::pair<int, int> pi;
#define cin std::cin
#define cout std::cout
#define vector std::vector
#define pb push_back
#define pv(x)                                                                  \\
  for (auto y : x)                                                             \\
    cerr << y << " ";                                                          \\
  cerr << '\\n';
#define fr first
#define sc second
#define ppb pop_back
#define max(a, b) a = max(a, b)
#define min(a, b) a = min(a, b)
#define int ll
#define tr(c, it) for (typeof(c.begin()) it = c.begin(); it != c.end(); it++)
signed main() {
  nitro;
  int VASU = 1;
  // cin>>VASU;
  while (VASU--) {
    int n;
    cin >> n;
    map<int, int> m;
    int ans = 0, a;
    rep(i, 0, n) {
      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;
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rrep(i, a, b) for (int i = a; i > b; --i)
#define sq(a) (a) * (a)
#define nitro                                                                  \\
  std::ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
#define debug(x) std::cerr << #x << "=" << x << '\\n'
#define text std::cerr << "SacredCoupoid08\\n"
#define endl '\\n'
#define all(c) (c).begin(), (c).end()
#define U 1000000007
#define N 100005
#define sz(c) (int)c.size()
typedef long long ll;
typedef std::vector<int> vi;
typedef std::pair<int, int> pi;
#define cin std::cin
#define cout std::cout
#define vector std::vector
#define pb push_back
#define pv(x)                                                                  \\
  for (auto y : x)                                                             \\
    cerr << y << " ";                                                          \\
  cerr << '\\n';
#define fr first
#define sc second
#define ppb pop_back
#define max(a, b) a = max(a, b)
#define min(a, b) a = min(a, b)
#define int ll
#define tr(c, it) for (typeof(c.begin()) it = c.begin(); it != c.end(); it++)
signed main() {
  nitro;
  int VASU = 1;
  // cin>>VASU;
  while (VASU--) {
    int n;
    cin >> n;
    vi a(n);
    rep(i, 0, n) { cin >> a[i]; }
    int ans = 0;
    rep(i, 0, n) {
      rep(j, i + 1, n) {
        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 ll;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
#define all(v) v.begin(), v.end()
#define repi(i, n, init) for (ll i = init; i < (n); i++)
#define repd(i, n, init) for (ll i = (n); i >= init; i--)
#define repm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define repb(bit, n, init) for (ll bit = init; bit < (1 << n); ++bit)

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

int main() {
  ll n;
  cin >> n;
  map<ll, ll> mp1, mp2;
  repi(i, n, 0) {
    ll tmp;
    cin >> tmp;
    mp1[i + 1 + tmp]++;
    mp2[i + 1 - tmp]++;
  }
  ll ans = 0;
  repm(i, mp1) {
    // cout << i->second * mp2[i->first] << " ";
    ans += i->second * mp2[i->first];
  }
  // cout << endl;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9;
const ll MOD = 1e9 + 7;
#define all(v) v.begin(), v.end()
#define repi(i, n, init) for (ll i = init; i < (n); i++)
#define repd(i, n, init) for (ll i = (n); i >= init; i--)
#define repm(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define repb(bit, n, init) for (ll bit = init; bit < (1 << n); ++bit)

template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 50;
const int ININF = 1000000;
using pint = pair<int, int>;
using Edge = pair<long long, pint>; //<cost,<u,v>>の形
vector<Edge> edges;

int main() {
  int N;
  cin >> N;
  vector<long long> a(N);
  rep(i, 0, N) { cin >> a[i]; }
  map<long long, long long> ma;
  ma[-1LL - a[0]] = 1LL;
  long long res = 0LL;
  rep(i, 1, N) {
    long long ind = 1LL * i + 1;
    // cout << i << "," << ma[a[i] - ind]<<endl;
    if (ma[a[i] - ind] > 0) {
      res = 1LL * res + ma[1LL * a[i] - ind];
    }
    if (ma[-ind - a[i]] > 0) {
      ma[-ind - a[i]]++;
    } else {
      ma[-ind - a[i]] = 1LL;
    }
  }

  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i < n; i++)
using namespace std;
const long long INF = 1LL << 50;
const int ININF = 1000000;
using pint = pair<int, int>;
using Edge = pair<long long, pint>; //<cost,<u,v>>の形
vector<Edge> edges;

int main() {
  int N;
  cin >> N;
  vector<long long> a(N);
  rep(i, 0, N) { cin >> a[i]; }
  multiset<long long> se;
  se.insert(-1 - a[0]);
  long long res = 0LL;
  rep(i, 1, N) {
    int ind = i + 1;
    res += se.count(a[i] - ind);
    se.insert(-ind - a[i]);
  }

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

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

const ll mod = 1000000007;

int main() {
  ll n, count = 0;
  cin >> n;
  map<int, int> mp;
  r(i, n) {
    int a;
    cin >> a;
    count += mp[i - a];
    mp[a + i]++;
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

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

const ll mod = 1000000007;

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

#include <bits/stdc++.h>
using namespace std;
long long int b[200000], c[200000];
int main() {
  // freopen("input.txt","r",stdin);
  long long int n, i, j, s = 0;
  cin >> n;
  long long int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  // long long int a[n];
  for (i = 0; i < n; i++) {
    // cin>>a[i];
    b[i] = a[i] + i;
    if (i - a[i] > 0) {
      c[i - a[i]]++;
    }
  }
  // cout<<"Hello"<<endl;
  for (i = 0; i < n; i++) {
    if (b[i] < n)
      s = s + c[b[i]];
  }
  cout << s << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long int b[100000], c[100000];
int main() {
  long long int n, i, j, s = 0;
  cin >> n;
  long long int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    b[i] = a[i] + i;
    if (i - a[i] > 0) {
      c[i - a[i]]++;
    }
  }
  for (i = 0; i < n; i++) {
    if (b[i] < n)
      s = s + c[b[i]];
  }
  cout << s << endl;
}