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

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

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 < m.size(); ++i) {
    ans += s[m.at(i)] * t[m.at(i)];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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;
  for (ll i = 0; i < s.size(); ++i) {
    ans += count(t.begin(), t.end(), s.at(i));
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INF INT_MAX     // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define per(i, n) for (ll i = ((ll)(n)) - 1; i >= 0; i--)
#define perf(i, n) for (ll i = ((ll)(n)); i > 0; i--)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  ll n, ans = 0;
  cin >> n;
  map<ll, ll> small;
  map<ll, ll> tall;
  repf(i, n) {
    ll a;
    cin >> a;
    small[i + a]++;
    tall[i - a]++;
  }
  for (auto p : small)
    ans += p.second * tall[p.first];
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lli long long int
#define MOD 1000000007
#define INF INT_MAX     // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for (lli i = 0; i < (lli)(n); i++)
#define repf(i, n) for (lli i = 1; i < (lli)(n + 1); i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"
#define vi vector<lli>
#define vvi vector<vector<lli>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  lli n;
  cin >> n;
  vector<pair<int, int>> a(n + 1);
  rep(i, n) {
    cin >> a[i + 1].first;
    a[i + 1].second = i + 1;
  }
  sort(all(a));

  lli cnt = 0;
  for (lli i = 1; i <= n - 1; i++) {
    for (lli j = i + 1; j <= n; j++) {
      if (a[i].first + a[j].first > 200000)
        break;
      if (a[i].first + a[j].first == abs(a[i].second - a[j].second))
        cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

struct node {
  int a;
  int b;
  int id;
};

int main() {
  int n;
  cin >> n;
  vector<node> vec(n);
  for (int i = 0; i < n; ++i) {
    int tmp;
    cin >> tmp;
    vec[i] = {tmp, -tmp + i, i};
  }

  sort(vec.begin(), vec.end(),
       [](auto &lhs, auto &rhs) { return lhs.b < rhs.b; });

  long long cnt = 0;
  for (auto itr = vec.begin(); itr != vec.end(); ++itr) {
    auto c1 = [](auto &lhs, int rhs) { return lhs.b < rhs; };
    auto c2 = [](int lhs, auto &rhs) { return lhs < rhs.b; };
    auto itr1 = lower_bound(itr, vec.end(), itr->a + itr->id, c1);
    auto itr2 = upper_bound(itr, vec.end(), itr->a + itr->id, c2);
    cnt += itr2 - itr1;
  }

  cout << cnt << endl;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> vec(n);
  for (auto &v : vec)
    cin >> v;

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

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

using namespace std;
typedef long long ll;

const int MAXN = 200010;

ll n;
ll a[MAXN];

void solve() {
  vector<ll> v;
  for (int i = 0; i < n; i++) {
    v.push_back(i - a[i]);
  }
  sort(v.begin(), v.end());

  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    auto lower = lower_bound(v.begin(), v.end(), a[i] + i);
    auto upper = upper_bound(v.begin(), v.end(), a[i] + i);
    cnt += upper - lower;
  }
  cout << cnt << endl;
}

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


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;
typedef long long ll;

const int MAXN = 200010;

ll n;
ll a[MAXN];

void solve() {
  ll cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 1; i + j < n; j++) {
      if (a[i] + a[i + j] == j) {
        cnt++;
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  solve();
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

// int _;

long long n, a[200020], ans = 0;
map<int, int> mp;

int main() {
  // for(scanf("%d",&_);_;_--)
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += mp[i - a[i]];
    mp[i + a[i]]++;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

// int _;

int n, a[200020], ans = 0, sum[200020];

int main() {
  // for(scanf("%d",&_);_;_--)
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ans += sum[i - a[i]];
    sum[i + a[i]]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;

/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define REP(i, n) REPS(i, 0, n)

using ll = long long;

signed main() {
  // my-code
  int N;
  cin >> N;
  vector<ll> a(N);
  REP(i, N) cin >> a[i];
  map<ll, ll> mp1, mp2;
  REP(i, N) {
    mp1[i + 1 + a[i]]++;
    mp2[i + 1 - a[i]]++;
  }
  ll ans = 0;
  REPS(x, 2, N) { ans += mp1[x] * mp2[x]; }
  cout << ans << endl;

  return 0;
}

// https://github.com/kurokoji/.cpp-Template/wiki テンプレートについて
// http://www.creativ.xyz/dump-cpp-652 dump()について
// https://gist.github.com/rigibun/7905920 色々

Error: TLE

Fixed submission:

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

struct Fast {
  Fast() {
    std::cin.tie(0);
    ios::sync_with_stdio(false);
  }
} fast;

/* REPmacro */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define REP(i, n) REPS(i, 0, n)

using ll = long long;

signed main() {
  // my-code
  int N;
  cin >> N;
  vector<ll> a(N);
  REP(i, N) cin >> a[i];
  ll ans = 0;
  REP(i, N - 1) {
    if (a[i] >= max(i, N - 1 - i))
      continue;
    REPS(j, i + a[i] + 1, N) {
      if (j - i == a[i] + a[j])
        ans++;
    }
  }
  cout << ans << endl;

  return 0;
}

// https://github.com/kurokoji/.cpp-Template/wiki テンプレートについて
// http://www.creativ.xyz/dump-cpp-652 dump()について
// https://gist.github.com/rigibun/7905920 色々
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &i : a)
    cin >> i;
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    m[i + 1 - a[i]]++;
  }

  // for (auto const& pair: m) {
  //         std::cout << "{" << pair.first << ": " << pair.second << "}\\n";
  // }
  long long int count = 0;
  for (int i = 0; i < n; i++) {
    m[i + 1 - a[i]]--;
    count += m[a[i] + i + 1];
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  int count = 0;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (a[i] + a[j] == abs(i - j))
        count++;
    }
  }
  cout << count << 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, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1 << 29);

int main() {

  int n;
  cin >> n;
  vector<int> a(n);
  map<ll, int> mp;

  ll res = 0;

  REP(i, n) {
    cin >> a[i];
    res += mp[i - a[i]];
    mp[i + a[i]]++;
  }

  cout << res << 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, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1 << 29);

int main() {

  int n;
  cin >> n;
  vector<int> a(n);
  REP(i, n) cin >> a[i];

  int res = 0;

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (abs(i - j) == a[i] + a[j])
        res++;
    }
  }

  cout << res << endl;

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Cr7                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define N 200200
ll n, a[N];
map<ll, ll> mp;
int main() {
  Cr7 cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  for (ll i = 2; i <= n; i++)
    mp[a[i] + a[1] - i + 1]++;
  ll need = 0, Res = mp[0];
  for (ll i = 2; i <= n; i++) {
    need--;
    need -= (a[i] - a[i - 1]);
    Res += mp[need];
  }
  cout << Res << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Cr7                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define N 100100
ll n, a[N];
map<ll, ll> mp;
int main() {
  Cr7 cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  for (ll i = 2; i <= n; i++)
    mp[a[i] + a[1] - i + 1]++;
  ll need = 0, Res = mp[0];
  for (ll i = 2; i <= n; i++) {
    need--;
    need -= (a[i] - a[i - 1]);
    Res += mp[need];
  }
  cout << Res << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

#define Maxn 200010
#define LL long long

using namespace std;

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

LL n, a[Maxn];

map<LL, LL> vis;

int main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  LL ans = 0;
  for (int i = n; i; --i) {
    ans += vis[i + a[i]];
    ++vis[i - a[i]];
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

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

#define Maxn 100010

using namespace std;

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

int n, a[Maxn];

map<int, int> vis;

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

#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e9
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

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

int main() {
  int n;
  cin >> n;
  vector<ll> A(n), b(n), v;
  rep(i, n) {
    cin >> b[i];
    A[i] = i + b[i];
    v.push_back(A[i]);
  }
  v.push_back(INF * INF);
  sort(ALL(v));

  ll res = 0;
  REP(i, 1, n - 1) {
    ll now = i - b[i];
    res += upper_bound(ALL(v), now) - lower_bound(ALL(v), now);
  }
  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
// #include <bits/stdc++.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <float.h>
#include <iomanip>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define INF 1e9
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define REP(i, a, b) for (int i = (int)(a); (i) <= (int)(b); i++)
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define vec(type, n) vector<type>(n)
#define vvec(m, n) vector<vector<int>>(int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

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

int main() {
  int n;
  cin >> n;
  vector<ll> A(n), b(n);
  rep(i, n) {
    cin >> b[i];
    A[i] = i + b[i];
  }
  vector<ll> v;
  v.push_back(A[0]);
  v.push_back(INF * INF);

  int res = 0;
  REP(i, 1, n - 1) {
    ll now = i - b[i];
    res += upper_bound(ALL(v), now) - lower_bound(ALL(v), now);
    v.push_back(A[i]);
    sort(ALL(v));
  }
  cout << res << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <set>

using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\\n";
}

signed main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  map<int, int> map;
  int ans = 0;
  rep(i, n) {
    ans += map[i - a[i]];
    map[i + a[i]]++;
  }
  print(ans);
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <set>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define all(i) (i).begin(), (i).end()
#define rall(i) (i).begin(), (i).end(), greater<int>()
#define int long long
#define elif else if
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define fst first
#define sec second
#define mod 1000000007
#define INF 10e10
template <typename T>

void print(T x) {
  std::cout << x << " ";
}
template <typename T>

void println(T x) {
  std::cout << x << "\\n";
}

signed main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  int ans = 0;
  rep(i, n) {
    FOR(j, i + 1, n) {
      if ((j - i) == a[j] + a[i])
        ans++;
    }
  }
  print(ans);
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

ll n, a[200001], ans;
map<ll, ll> mp;

int main() {

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


Error: WA

Fixed submission:

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

ll n, a[100001], ans;
map<ll, ll> mp;

int main() {

  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    mp[i + a[i]]++;
    ans += mp[i - a[i]];
  }
  cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long a[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  long ans = 0;
  map<long, long> numU;
  map<long, long> numD;
  set<long> numbers;
  for (int i = 1; i <= n; i++) {
    numU[a[i] + i]++;
    numD[i - a[i]]++;
    numbers.insert(a[i] + i);
    numbers.insert(i - a[i]);
  }
  for (int j : numbers) {
    ans += numU[j] * numD[j];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a, ap, am;
  int max_n = 200000;
  std::vector<long> p(max_n + 1, 0);
  std::vector<long> m(max_n + 1, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a;
    ap = a + i;
    am = i - a;
    if (ap >= 0 && ap <= max_n)
      p[ap]++;
    if (am >= 0 && am <= max_n)
      m[am]++;
  }
  long c = 0;
  for (int i = 0; i <= max_n; ++i) {
    c += p[i] * m[i];
  }
  cout << c << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  int c = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (j - i == (a[i] + a[j]))
        c++;
    }
  }

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

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

using ll = long long;

int main() {
  ll N;
  cin >> N;
  vector<ll> a(N + 1);
  map<ll, ll> m;

  for (int i = 1; i <= N; i++) {
    cin >> a.at(i);
    m[i - a.at(i)]++;
  }

  ll sum = 0;
  for (int i = 1; i <= N; i++) {
    sum += m[i + a.at(i)];
  }

  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

using ll = long long;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N + 1);
  map<ll, vector<ll>> kei;
  for (ll i = 1; i <= N; i++) {
    cin >> A.at(i);
    if (i - A.at(i) >= 0) {
      ll buf = i - A.at(i);
      if (kei.find(buf) == kei.end()) {
        vector<ll> bb;
        bb.push_back(i);
        kei.insert(make_pair(buf, bb));
      } else {
        kei.at(buf).push_back(i);
      }
    }
  }

  ll count = 0;

  for (int i = 1; i <= N; i++) {
    ll keisan = i + A.at(i);
    if (kei.find(keisan) != kei.end()) {
      vector<ll> koho = kei.at(keisan);
      for (ll k : koho) {
        if (i < k) {
          // cout << i << " " << k << endl;
          count++;
        }
      }
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  int *arr = new int[N];
  int *look = new int[N];

  for (int i = 0; i < N; ++i) {
    cin >> arr[i];
    look[i] = 0;
  }
  long long ans = 0;
  for (int i = 0; i < N - 1; ++i) {
    if (i + arr[i] < N)
      look[i + arr[i]]++;
  }
  for (int i = N - 1; i > 0; --i) {
    if (i - arr[i] >= 0) {
      ans += look[i - arr[i]];
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  int *arr = new int[N];
  for (int i = 0; i < N; ++i) {
    cin >> arr[i];
  }
  long long ans = 0;
  for (int i = 0; i < N - 1; ++i) {
    for (int j = i + arr[i]; j < N; ++j) {
      if (abs(j - i) == arr[i] + arr[j])
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}