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

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

int main(void) {
  int n;
  cin >> n;
  int a[n];
  map<long long, long long> mp;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  long long sum = 0;

  for (int i = 0; i < n; i++) {
    // mp[a[i]-(i+1)]++;
    mp[(-a[i] + (i + 1))]++;
  }

  for (int i = 0; i < n; i++) {
    mp[(-a[i] + (i + 1))]--;
    // mp[a[i]-(i+1)]--;

    if (mp[abs(a[i] + (i + 1))] > 0) {
      sum += mp[abs(a[i] + (i + 1))];
    }
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int n;
  cin >> n;
  int a[n];
  // long long num[200001] = {},num2[200001] = {};
  for (int i = 0; i < n; i++)
    cin >> a[i];
  long long sum = 0;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
ll a[maxn], b[maxn], k, n, m, sum, p;
double c, r;
string s, t;
map<int, int> ma, mp;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    mp[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    sum += mp[a[i] + i];
  }
  cout << sum;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
ll a[maxn], k, n, m, sum, p;
double c, r;
string s, t;

int main() {
  ios_base::sync_with_stdio(0);
  scanf("%ld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%ld", &a[i]);
    for (int j = 1; j < i; j++) {
      if (j + (a[i] + a[j]) == i)
        sum++;
    }
  }
  printf("%d", sum);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i <= n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
#define MAX 100000
#define inf 1000000007
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

int main() {
  // cin高速化
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, ans, tmp;
  cin >> n;
  ans = 0;
  vll a(n + 1, 0); // 初期化忘れない
  vll b(n + 1, 0);
  vll dp(n, 0);
  FOR(i, 1, n) {
    cin >> tmp;
    a[i] = i + tmp;
    b[i] = i - tmp;
    if (b[i] > 0)
      dp[b[i]] += 1;
  }

  FOR(i, 1, n) {
    if (a[i] < n)
      ans += dp[a[i]];
  }
  cout << ans << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i <= n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define SORT(v, n) sort(v, v + n)
#define MAX 100000
#define inf 1000000007
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

int main() {
  // cin高速化
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n, ans;
  cin >> n;
  ans = 0;
  vll a(n + 1, 0); // 初期化忘れない
  FOR(i, 1, n) cin >> a[i];
  FOR(i, 1, n - 1) {
    FOR(j, i + 1, n) {
      if (j - i == a[j] + a[i]) {
        ans++;
      }
    }
  }

  cout << ans << "\\n";
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

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

  vector<long long> h(n);
  vector<long long> hplus(n);
  vector<long long> hminuses;

  long long answer = 0;

  for (long long i = 0; i < n; i++) {
    cin >> h[i];
    hplus[i] = h[i] + i;
    hminuses.push_back(i - h[i]);
  }
  sort(hplus.begin(), hplus.end());
  sort(hminuses.begin(), hminuses.end());

  long long index = 0;
  for (long long i = 0; i < n;) {
    long long ans = 0;
    for (; index < hminuses.size(); index++) {
      if (hminuses[index] == hplus[i])
        ans++;
      if (hplus[i] < hminuses[index])
        break;
    }
    while (1) {
      answer += ans;
      i++;
      if (i >= n - 1)
        break;
      if (hplus[i] != hplus[i - 1])
        break;
    }

    if (index > hminuses.size() - 1)
      break;
  }

  cout << answer << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

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

  vector<long long> h(n);
  vector<long long> hplus(n);
  multiset<long long> hminuses;

  int answer = 0;

  for (int i = 0; i < n; i++) {
    cin >> h[i];
    hplus[i] = h[i] + i;
    hminuses.insert(i - h[i]);
  }

  for (int i = 0; i < n; i++) {
    answer += hminuses.count(hplus[i]);
  }

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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<ll> sum_cnt(n), diff_cnt(n);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (i + 1 + a < n)
      sum_cnt[i + 1 + a]++;
    if (i + 1 - a > 1)
      diff_cnt[i + 1 - a]++;
  }
  ll ans = 0;
  for (int i = 2; i < n; i++) {
    ans += sum_cnt[i] * diff_cnt[i];
  }
  cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  map<ll, int> sum_mp, diff_mp;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    sum_mp[i + 1 + a]++;
    diff_mp[i + 1 - a]++;
  }
  ll ans = 0;
  for (auto &&[sum, sum_cnt] : sum_mp) {
    for (auto &&[diff, diff_cnt] : diff_mp) {
      if (sum == diff)
        ans += sum_cnt * diff_cnt;
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {

  ll n, ans = 0, tmp = 0;
  ll a, b;
  map<ll, ll> m;

  cin >> n;

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

    cin >> a;
    b = a - i;
    tmp = b;

    if (m.count(tmp) != 0) {

      ans += m.at(tmp);
    }

    tmp = 0 - b - 2 * i;
    m[tmp] += 1;
  }

  cout << ans << endl;

  return 0;
}

Error: WA

Fixed submission:

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

int main(void) {

  int n, ans = 0, tmp = 0;
  int a[100009] = {};
  int b[100009] = {};
  map<int, int> m;

  cin >> n;

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

    cin >> a[i];
    b[i] = a[i] - i;
    tmp = b[i];

    if (m.count(tmp) != 0) {

      // ans += m.at(tmp);
    }

    tmp = 0 - b[i] - 2 * i;
    // m[tmp] += 1;
  }

  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 < n; i++)
#define All(V) V.begin(), V.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7, INF = 1e9;

int main() {
  int n;
  cin >> n;
  vector<ll> v(n);
  rep(i, n) cin >> v[i];
  map<ll, int> mp;
  ll ans = 0;
  vector<ll> r(n);
  rep(i, n) {
    mp[i + v[i]]++;
    r[i] = i - v[i];
  }
  rep(i, n) { ans += mp[r[i]]; }
  cout << ans << endl;
  system("pause");
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define All(V) V.begin(), V.end()
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD = 1e9 + 7, INF = 1e9;

int main() {
  ll n;
  cin >> n;
  vector<ll> v(n), t(n);
  rep(i, n) {
    cin >> v[i];
    t[i] = v[i];
  }
  sort(All(t));
  int mn = t[0] + t[1];
  int mx = t[n - 1] + t[n - 2];
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + mn; j <= min(n - 1, (ll)i + mx); j++) {
      if (j - i == v[i] + v[j])
        ans++;
    }
  }
  cout << ans << endl;

  system("pause");
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
using namespace std;
int mx4[] = {0, 1, 0, -1};
int my4[] = {1, 0, -1, 0};
int mx2[] = {1, 0};
int my2[] = {0, 1};

class EThisMessageWillSelfDestructIn5s {
public:
  void solve(istream &in, ostream &out) {
    int n;
    in >> n;
    vector<ll> a(n), l(n), r(n);
    map<ll, ll> mp;
    rep(i, n) {
      ll a;
      in >> a;
      l[i] = i + a;
      mp[l[i]]++;
      r[i] = i - a;
    }
    ll ans = 0;
    rep(i, n) { ans += mp[r[i]]; }
    out << ans << endl;
  }
};

int main() {
  EThisMessageWillSelfDestructIn5s solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (n); i++)
#define pb push_back
using namespace std;
int mx4[] = {0, 1, 0, -1};
int my4[] = {1, 0, -1, 0};
int mx2[] = {1, 0};
int my2[] = {0, 1};

class EThisMessageWillSelfDestructIn5s {
public:
  void solve(istream &in, ostream &out) {
    int n;
    in >> n;
    vector<ll> a(n), l(n), R(n);
    multiset<ll> r;
    rep(i, n) {
      ll a;
      in >> a;
      l[i] = i + a;
      R[i] = i - a;
      r.insert(R[i]);
    }
    ll ans = 0;
    rep(i, n) { ans += r.count(l[i]); }
    out << ans << endl;
  }
};

int main() {
  EThisMessageWillSelfDestructIn5s solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfenv>
#include <cmath>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#define ll long long
using namespace std;

typedef pair<int, int> pair;

int main() {
  ll N;
  ll ans = 0;
  cin >> N;
  vector<ll> A(N, 0);
  vector<ll> B(2, 0);
  vector<vector<ll>> V(N, B);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i + A[i] < N)
      V[i + A[i]][1] += 1;
    if (i - A[i] >= 0)
      V[i - A[i]][0] += 1;
  }
  for (int i = 0; i < N; i++) {
    // cout << i;
    if (V[i][0] > 0 && V[i][1] > 0) {
      ans += V[i][0] * V[i][1];
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfenv>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#define ll long long
using namespace std;

int main() {
  ll N;
  ll ans = 0;
  cin >> N;
  vector<ll> A(N, 0);
  vector<vector<ll>> V(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i + A[i] < N)
      V[i + A[i]].push_back(i + 1);
    if (i - A[i] >= 0)
      V[i - A[i]].push_back(i + 1);
  }
  for (int i = 0; i < N; i++) {
    // cout << i;
    if (V[i].size() > 1) {
      for (int j = 0; j < V[i].size() - 1; j++) {
        // cout << j;
        for (int k = j + 1; k < V[i].size(); k++) {
          // cout << k <<endl;
          if (V[i][j] < i + 1 && V[i][k] > i + 1)
            ans++;
        }
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll n;
  cin >> n;
  ll a[200005], sum[200005] = {0};
  rep(i, n) cin >> a[i + 1];
  ll ans = 0;

  for (ll i = 1; i <= n; i++) {
    if (a[i] + i < 200005) {
      sum[a[i] + i]++;
    }
  }

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

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

Error: 0

Fixed submission:

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

int main() {
  long int n;
  cin >> n;
  long int a[200005], sum[200005] = {0};
  rep(i, n) cin >> a[i + 1];
  long int ans = 0;

  for (long int i = 1; i <= n; i++) {
    sum[a[i] + i]++;
  }

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

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

#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
map<long long, long long> mp;
long long o;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> o;
    v.push_back(i - o);
    mp[i + o]++;
  }
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    sum += mp[v[i - 1]];
  }
  cout << sum << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> v;
map<int, int> mp;
int o[100005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> o[i];
    v.push_back(i - o[i]);
    mp[i + o[i]]++;
  }
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += mp[v[i - 1]];
  }
  cout << sum << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long;
using P = pair<int, int>;
#define reps(i, n) for (int i = 1; i <= n; i++)
#define zarray(x, n)                                                           \\
  x[n + 1];                                                                    \\
  reps(i, n) { x[i] = 0; }
#define zarray2(x, n, m)                                                       \\
  x[n + 1][m + 1];                                                             \\
  reps(i, n) {                                                                 \\
    reps(j, m) { x[i][j] = 0; }                                                \\
  }

int main() {
  int n;
  cin >> n;
  map<int, pair<ll, ll>> l;
  reps(i, n) {
    int height;
    cin >> height;
    l[i + height].first++;
    l[i - height].second++;
  }

  ll ans = 0;
  for (auto p : l) {
    ans += p.second.first * p.second.second;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

using ll = long long;
using P = pair<int, int>;
#define reps(i, n) for (int i = 1; i <= n; i++)
#define zarray(x, n)                                                           \\
  x[n + 1];                                                                    \\
  reps(i, n) { x[i] = 0; }
#define zarray2(x, n, m)                                                       \\
  x[n + 1][m + 1];                                                             \\
  reps(i, n) {                                                                 \\
    reps(j, m) { x[i][j] = 0; }                                                \\
  }

int main() {
  int n;
  cin >> n;
  int zarray(a, n);
  reps(i, n) { cin >> a[i]; }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define lowbit(i) (i & (-i))
typedef long long LL;
using namespace std;
const int maxn = 2e5 + 5;
int readint() {
  int x = 0, f = 1;
  char s = getchar();
#define sc (s = getchar())
  while (s < '0' || s > '9') {
    if (s == '-')
      f = -1;
    sc;
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    sc;
  }
#undef sc
  return x * f;
}
int a[maxn];
map<int, int> mp, mp1;
signed main() {
  int n = readint();
  LL ans = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = readint();
  }
  for (int i = n; i >= 1; i--) {
    mp[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    mp1[i + a[i]]++;
    if (mp[i + a[i]] > 0)
      ans += mp[i + a[i]];
  }
  for (int i = n; i >= 1; i--)
    if (mp1[i - a[i]] > 0)
      ans += mp1[i - a[i]];
  cout << ans / 2;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
#define lowbit(i) (i & (-i))
typedef long long LL;
using namespace std;
const int maxn = 1e5 + 5;
int readint() {
  int x = 0, f = 1;
  char s = getchar();
#define sc (s = getchar())
  while (s < '0' || s > '9') {
    if (s == '-')
      f = -1;
    sc;
  }
  while (s >= '0' && s <= '9') {
    x = (x << 3) + (x << 1) + (s ^ 48);
    sc;
  }
#undef sc
  return x * f;
}
int a[maxn];
map<int, int> mp, mp1;
signed main() {
  int n = readint();
  LL ans = 0;
  for (int i = 1; i <= n; i++) {
    a[i] = readint();
  }
  for (int i = n; i >= 1; i--) {
    mp[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    mp1[i + a[i]]++;
    if (mp[i + a[i]] > 0)
      ans += mp[i + a[i]];
  }
  for (int i = n; i >= 1; i--)
    if (mp1[i - a[i]] > 0)
      ans += mp1[i - a[i]];
  cout << ans / 2;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  vector<ll> D;
  ll N;
  cin >> N;
  ll A[N], B[N], C[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    B[i] = i - A[i];
    C[i] = i + A[i];
  }

  ll ans = 0;
  sort(B, B + N);
  sort(C, C + N);
  ll bi, ci;
  bi = ci = 0;
  while (bi < N and ci < N) {
    if (B[bi] < C[ci])
      bi++;
    else if (B[bi] > C[ci])
      ci++;
    else {
      ll ab, ac;
      ab = ac = 1;
      while (B[bi + 1] == B[bi] and bi < N - 1) {
        bi++;
        ab++;
      }
      while (C[ci + 1] == C[ci] and ci < N - 1) {
        ci++;
        ac++;
      }
      ans += ab * ac;
      bi++;
      ci++;
    }
  }

  cout << ans << endl;
}

Error: -11

Fixed submission:

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

using Graph = vector<vector<int>>;

int main() {
  int *D;
  D = (int *)malloc(1000000000 + 10000000 * sizeof(int));
  int N;
  cin >> N;
  int A[N], B[N], C[N];
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    B[i] = i - A[i];
    C[i] = i + A[i];
  }

  ll ans = 0;

  for (int i = 0; i < N; i++) {
    if (B[i] >= 0)
      D[B[i]]++;
  }
  for (int i = 0; i < N; i++) {
    ans += D[C[i]];
  }
  cout << ans << endl;
  free(D);
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
using namespace std;
typedef long long int lli;
const int MaxN = 200005;
map<lli, lli> mapi;
lli arr[MaxN];
int main() {
  lli N, M, i, j, p, q, res = 0;
  cin >> N;
  for (i = 1; i <= N; i++) {
    cin >> arr[i];
  }
  for (i = 1; i <= N; i++) {
    p = i - arr[i];
    q = i + arr[i];
    res += mapi[p];
    mapi[q] += 1;
  }
  cout << res;
}

Error: WA

Fixed submission:

#include <iostream>
#include <map>
using namespace std;
typedef long long int lli;
const int MaxN = 100005;
map<lli, lli> mapi;
lli arr[MaxN];
int main() {
  lli N, M, i, j, p, q, res = 0;
  cin >> N;
  for (i = 1; i <= N; i++) {
    cin >> arr[i];
  }
  for (i = 1; i <= N; i++) {
    p = i - arr[i];
    q = i + arr[i];
    res += mapi[p];
    mapi[q] += 1;
  }
  cout << res;
}
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++)
const int INF = 1 << 29;
const int MOD = 1000000007;

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

  // i + Ai = j - Aj となればよい
  // L = i + Ai, R = j - Aj とおいて{値:個数}のmapで管理

  long long count = 0;
  map<long long, long long> L, R;

  for (long long i = 1; i < n + 1; i++) {
    // それまでのiを見て今回のiとL=Rになっているものがあるかを見る(無いなら0が足される)
    // Lの値と一致するものがあるかRの値を見てRの個数分を足し、その逆も行う
    count += R[i + a.at(i - 1)];
    count += L[i - a.at(i - 1)];

    // 個数を1増やす
    L[i + a.at(i - 1)]++;
    R[i - a.at(i - 1)]++;
  }

  cout << count << endl;
}


Error: TLE

Fixed submission:

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

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

  long long count = 0;

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (abs(i - j) == a.at(i) + a.at(j)) {
        count++;
      }
    }
  }

  cout << count << endl;
}
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 PI 3.14159265358979323846
#define int long long
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long INFL = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  int A[N];
  int B[N];
  map<int, int> m;
  rep(i, N) {
    cin >> A[i];
    B[i] = i - A[i];
    m[B[i]]++;
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    int x = A[i] + i;
    ans += m[x];
  }
  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 PI 3.14159265358979323846
#define int long long
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr long long INFL = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  int A[N];
  rep(i, N) cin >> A[i];
  int ans = 0;
  for (int i = 0; i < N; i++) {
    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 rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
using namespace std;
using ll = long long;

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

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

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int a[n];
  rep(i, n) { cin >> a[i]; }
  int ans = 0;
  rep(i, n - 1) {
    for (int j = i + 1; j < n; j++) {
      if (a[j] + a[i] == abs(j - i)) {
        ans++;
        // cout << i << j <<endl;
      }
    }
  }
  cout << ans << endl;
  return 0;
}