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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>

using namespace std;

typedef long long ll;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)

int main(int argc, char **argv) {
  int N;
  cin >> N;
  ll A[N];
  REP(i, N) cin >> A[i];

  map<ll, int> m;

  REP(i, N) {
    ll s = i - A[i];
    if (m.find(s) != m.end())
      m[s]++;
    else
      m[s] = 1;
  }

  ll count = 0;
  REP(i, N) {
    ll s = A[i] + i;
    if (m.find(s) != m.end()) {
      count += m[s];
    }
  }
  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>

using namespace std;

typedef long long ll;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)

int main(int argc, char **argv) {
  int N;
  cin >> N;
  ll A[N];
  REP(i, N) cin >> A[i];

  pair<ll, int> p[N];
  REP(i, N) p[i] = make_pair(A[i], i);
  sort(p, p + N);

  REP(i, N) A[i] = p[i].first;

  ll count = 0;
  for (int i = N - 1; i >= 0; --i) {
    int mid = max(N - 1 - p[i].second, p[i].second);
    if (mid >= A[i]) { // p[i] is small enough
      auto it = upper_bound(A, A + N, mid - A[i]);
      int target = it - (&A[0]) - 1;
      if (target >= i)
        target = i - 1;
      for (int j = target; j >= 0; --j) {
        if (p[i].first + p[j].first == abs(p[i].second - p[j].second))
          count++;
      }
    }
  }
  cout << count << endl;
  return 0;
}
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 (int i = 0; i < (int)(n); i++)
#define inf 1001001001
#define mod 1000000007
#define pi 3.141592653589793
#define all(v) v.begin(), v.end()

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> mp;
  ll ans = 0;
  rep(j, n) {
    ll sa = j + 1 - a[j];
    ans += mp[sa];
    ll wa = j + 1 + a[j];
    mp[wa]++;
  }
  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 (int i = 0; i < (int)(n); i++)
#define inf 1001001001
#define mod 1000000007
#define pi 3.141592653589793
#define all(v) v.begin(), v.end()

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n) {
    int left = i - (a[i] + 1);
    int l_num = 1;
    int r_num = 1;
    int right = i + (a[i] + 1);
    while (left >= 0) {
      if (a[left] == l_num)
        ans++;
      left--;
      l_num++;
    }
    while (right < n) {
      if (a[right] == r_num)
        ans++;
      right++;
      r_num++;
    }
  }
  cout << ans / 2 << 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) < (n); (i)++)
#define FILL0(x) memset(x, 0, sizeof(x))

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

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;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;

  vector<ll> l(N);
  vector<ll> r(N);
  fill(l.begin(), l.end(), 0);
  fill(r.begin(), r.end(), 0);

  vector<ll> A(N);
  rep(i, N) {
    ll a;
    cin >> a;
    A[i] = a;
    if (i - A[i] > 0) {
      l[i - A[i]]++;
    }
    if (i + A[i] < N) {
      r[i + A[i]]++;
    }
  }

  ll ans = 0;
  rep(i, N) { ans += l[i] * r[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 FILL0(x) memset(x, 0, sizeof(x))

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

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;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) { cin >> A[i]; }

  ll ans = 0;

  rep(i, N) {
    for (int j = 0; i + j < N; j++) {
      if (A[j + i] + A[j] == i) {
        ans++;
      }
    }
  }

  cout << ans << endl;

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

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

int combi(int a) {
  int ans = 0;
  if (a == 1) {
    return 0;
  }

  ans = a * (a - 1) / 2;
  return ans;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<int> cop = A;
  sort(cop.begin(), cop.end());

  int64_t ans = 0;
  map<int, int> hozon;
  vector<int> jb(N), ib(N);
  // vector<int> han(2*abs(N-cop.at(0))+1, 1);
  for (int i = 0; i < N; i++) {
    jb.at(i) = A.at(i) - (i + 1);
    if (hozon.count(jb.at(i))) {
      ans += hozon.at(jb.at(i));
      // ans += han.at(abs(jb.at(i)));
    }

    ib.at(i) = -A.at(i) - (i + 1);
    if (hozon.count(ib.at(i)) && i != N - 1) {
      hozon.at(ib.at(i))++;
      // han.at(abs(ib.at(i))) ++;
      continue;
    } else {
      hozon[ib.at(i)] = 1;
    }
  }

  cout << ans;
}

Error: WA

Fixed submission:

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

int combi(int a) {
  int ans = 0;
  if (a == 1) {
    return 0;
  }

  ans = a * (a - 1) / 2;
  return ans;
}

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<int> cop = A;
  sort(cop.begin(), cop.end());

  int64_t ans = 0;
  map<int, int> hozon;
  vector<int> jb(N), ib(N);
  vector<int> han(2 * abs(N - cop.at(0)) + 1, 1);
  for (int i = 0; i < N; i++) {
    jb.at(i) = A.at(i) - (i + 1);
    if (hozon.count(jb.at(i))) {
      // ans += hozon.at(ib.at(i));
      ans += han.at(abs(jb.at(i)));
    }

    ib.at(i) = -A.at(i) - (i + 1);
    if (hozon.count(ib.at(i)) && i != N - 1) {
      // hozon.at(ib.at(i)) ++;
      han.at(abs(ib.at(i)))++;
      continue;
    } else {
      hozon[ib.at(i)] = 1;
    }
  }

  cout << ans;
}
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 REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

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

  map<ll, ll> A;
  map<ll, ll> B;
  REP(i, N) {
    ll a;
    cin >> a;
    A[i + a]++;
    B[i - a]++;
  }
  ll ans = 0;
  for (auto x : A) {
    ans += x.second * B[x.first];
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

long long modinv(long long a, long long m) {
  long long b = m, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}

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

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

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

#include <bits/stdc++.h>

#define scd second
#define fst first
#define mkp make_pair
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vlong;
typedef vector<vint> vvint;
typedef vector<vlong> vvlong;
typedef vector<bool> vbool;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> arr(n);
  for (auto &x : arr)
    cin >> x;
  unordered_map<int, int> cnt;
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    auto itr = cnt.find(arr[i] + i + 1);
    if (itr != cnt.end()) {
      ans += itr->second;
    }
    cnt[i + 1 - arr[i]]++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define scd second
#define fst first
#define mkp make_pair
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vlong;
typedef vector<vint> vvint;
typedef vector<vlong> vvlong;
typedef vector<bool> vbool;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int n;
  cin >> n;
  vector<int> arr(n);
  for (auto &x : arr)
    cin >> x;
  unordered_map<int, int> cnt;
  ll ans = 0;
  for (int i = n - 1; i >= 0; i++) {
    auto itr = cnt.find(arr[i] + i + 1);
    if (itr != cnt.end()) {
      ans += itr->second;
    }
    cnt[i + 1 - arr[i]]++;
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

int main(void) {
  int n;

  while (scanf("%d", &n) == 1) {
    vi a(n + 1);
    for (int i = 1; i <= n; i++)
      scanf("%d", &a[i]);

    map<int, int> m;
    ll ans = 0;
    for (int i = n; i >= 1; i--) {
      ans += m[-(a[i] + i)];
      m[a[i] - i]++;
    }
    printf("%lld\\n", ans);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vpii = vector<pii>;

int main(void) {
  int n;

  while (scanf("%d", &n) == 1) {
    vi a(n + 1);
    for (int i = 1; i <= n; i++)
      scanf("%d", &a[i]);

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
inline int gi() {
  int data = 0, w = 1;
  char ch = 0;
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (isdigit(ch))
    data = 10 * data + ch - '0', ch = getchar();
  return w * data;
}
const int MAX_N = 2e5 + 5;
int N, a[MAX_N];
map<int, int> mp;
long long ans = 0;
int main() {
  N = gi();
  for (int i = 1; i <= N; i++)
    a[i] = gi();
  for (int i = 1; i <= N; i++) {
    int x = a[i] - i;
    if (mp.find(x) != mp.end())
      ans += mp[x];
    mp[-a[i] - i]++;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
inline int gi() {
  int data = 0, w = 1;
  char ch = 0;
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    w = -1, ch = getchar();
  while (isdigit(ch))
    data = 10 * data + ch - '0', ch = getchar();
  return w * data;
}
const int MAX_N = 1e5 + 5;
int N, a[MAX_N];
map<int, int> mp;
long long ans = 0;
int main() {
  N = gi();
  for (int i = 1; i <= N; i++)
    a[i] = gi();
  for (int i = 1; i <= N; i++) {
    int x = a[i] - i;
    ans += mp[x];
    mp[-a[i] - i]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
// namespace mp = boost::multiprecision;
// using llll = mp::cpp_int;
//  仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
// using Real = mp::number<mp::cpp_dec_float<1024>>;
using ll = long long;
const double EPS = 1e-10;
const ll MOD = 7 + (1e+9);

int main() {
  // cout<<fixed<<setprecision(10);
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n, 0);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  map<ll, ll> s1;
  for (ll i = 0; i < n; i++) {
    s1[i - a[i]]++;
  }
  for (ll i = 0; i < n; i++) {
    ans += s1[a[i] + i];
    if (s1[i - a[i]] > 0)
      s1[i - a[i]]--;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>
#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
// namespace mp = boost::multiprecision;
// using llll = mp::cpp_int;
//  仮数部が1024ビットの浮動小数点数型(TLEしたら小さくする)
// using Real = mp::number<mp::cpp_dec_float<1024>>;
using ll = long long;
const double EPS = 1e-10;
const ll MOD = 7 + (1e+9);

int main() {
  // cout<<fixed<<setprecision(10);
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n, 0);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  multiset<ll> s1, s2;
  for (ll i = 0; i < n; i++) {
    s1.insert(i - a[i]);
  }
  for (ll i = 0; i < n; i++) {
    ll ans1 = s1.count(a[i] + i);
    // distance(s1.lower_bound(a[i]+i),s1.lower_bound(a[i]+i+1));
    ans += ans1;
    s1.erase(s1.lower_bound(i - a[i]));
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); };
const int MOD = 1000000007;

ll N;
ll A[200010];

signed main() {
  cin >> N;
  REP(i, N) cin >> A[i];
  map<ll, ll> mp;
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    ans += mp[i - A[i]];
    mp[i + A[i]]++;
  }
  PRINT(ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define FOR(i, b, e) for (int(i) = (b); (i) < (e); ++(i))
#define ALL(c) (c).begin(), (c).end()
#define PRINT(x) cout << (x) << "\\n"
using namespace std;
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
template <typename T> auto compare = [](T x, T y) -> bool { return (x < y); };
const int MOD = 1000000007;

int N;
ll A[100010];

signed main() {
  cin >> N;
  REP(i, N) cin >> A[i];
  map<ll, ll> mp;
  ll ans = 0;
  REP(i, N) {
    ans += mp[i - A[i]];
    mp[i + A[i]]++;
  }
  PRINT(ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
long mod = (long)1e9 + 7;

int main() {

  long n;
  cin >> n;

  vector<long> a(n, 0);
  map<int, int> m;

  rep(i, n) {
    cin >> a[i];
    m[i - a[i]]++;
  }

  long ans = 0;

  rep(i, n) ans += m[i + a[i]];

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

  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
long mod = (int)1e9 + 7;

int main() {

  int n;
  cin >> n;

  multiset<int> wa, sa;

  rep(i, n) {
    int x;
    cin >> x;
    wa.emplace(i + x);
    sa.emplace(i - x);
  }

  int count = 0;

  for (int i : wa)
    count += sa.count(i);

  cout << count << "\\n";

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

#include <bits/stdc++.h>

#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \\
  })(x)

using namespace std;
using ll = long long;

void solve() {
  int n;
  cin >> n;
  unordered_map<ll, int> ad, su;

  ll cnt = 0;

  for (int i = 1; i <= n; i++) {
    ll h;
    cin >> h;

    cnt += ad[i - h];
    ++ad[h + i];
  }

  cout << cnt << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);

  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define whole(f, x, ...)                                                       \\
  ([&](decltype((x)) whole) {                                                  \\
    return (f)(begin(whole), end(whole), ##__VA_ARGS__);                       \\
  })(x)

using namespace std;
using ll = long long;

void solve() {
  int n;
  cin >> n;
  multimap<ll, int> a;

  for (int i = 0; i < n; i++) {
    ll in;
    cin >> in;
    a.insert({in, i});
  }

  int cnt = 0;
  for (auto it = a.begin(); it != a.end(); it = next(it)) {
    for (auto itt = it; itt != a.end(); itt = next(itt)) {
      if (abs(itt->second - it->second) == itt->first + it->first) {
        ++cnt;
      }
      if (itt->first + it->first > n) {
        break;
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);

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

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))

const long long INF = 1LL << 60;

typedef long long ll;

const long long MOD = 1000000000 + 7;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int n;
  cin >> n;
  map<ll, ll> L{};
  map<ll, ll> R{};

  ll h;
  rep(i, n) {
    cin >> h;
    ll LL, RR;
    LL = L[i + 1 + h];
    RR = R[i + 1 - h];
    L[i + 1 + h] = LL + 1;
    R[i + 1 - h] = RR + 1;
  }
  ll cnt = 0;
  for (auto itr = L.begin(); itr != L.end(); ++itr) {
    cnt += itr->second * R[itr->first];
  }
  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define MIN(a, b) ((a) > (b) ? (b) : (a))
#define MAX(a, b) ((a) < (b) ? (b) : (a))

const long long INF = 1LL << 60;

typedef unsigned long long ll;

const long long MOD = 1000000000 + 7;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// ll d[2*100001][2*10001];

int main() {
  int n;
  cin >> n;
  ll a;
  map<ll, ll> h;
  map<ll, ll> sub;
  ll cnt = 0;
  rep(i, n) {
    cin >> h[i];
    rep(j, i) {
      if ((i - j) == h[i] + h[j])
        cnt += 1;
    }
  }
  cout << cnt << endl;

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

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

int32_t main() {
  int n;
  cin >> n;
  vector<int> h(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
  }
  map<int, int> hash;
  int res = 0;
  for (int j = 1; j <= n; j++) {
    if (hash.find(j - h[j]) != hash.end()) {
      res += hash[j - h[j]];
    }
    hash[j + h[j]]++;
  }
  cout << res << "\\n";
}

Error: TLE

Fixed submission:

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

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

/**
 *    created: 04.05.2020 02:49:20
 **/

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

int main() {
  int n;
  cin >> n;
  ll a[n];
  rep(i, n) cin >> a[i];
  ll x = 0;
  map<ll, int> m;
  rep(i, n) {
    x += m[i - a[i]];
    m[i + a[i]]++;
  }

  cout << x << endl;
}

Error: TLE

Fixed submission:

/**
 *    created: 04.05.2020 02:49:20
 **/

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

int main() {
  int n;
  cin >> n;
  ll a[n];
  rep(i, n) cin >> a[i];
  ll d;
  // a[i],a[j]
  // a[i]+a[j]==max(i-j,j-i)(=s)
  // i<jとする。
  // j=i+a[i]+a[j]>=i+a[i]
  // 計算時間。。わかめ。とりまつくるか03:02
  int c = 0;
  rep(i, n) {
    for (int j = i + a[i]; j < n; j++) {
      if (a[j] + a[i] == j - i) {
        c++;
      }
    }
  }
  cout << c << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream>
#include <string>

#include <map>
#include <vector>

using namespace std;

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

  int N;
  cin >> N;

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

  map<long long, int> count;
  for (int i = 0; i < N; i++) {
    count[i + A[i]]++;
  }

  long long result = 0;
  for (int i = 0; i < N; i++) {
    long long val = i - A[i];
    if (count.find(val) != count.end()) {
      result += count[val];
    }
  }

  cout << result;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <sstream>
#include <string>

#include <vector>

using namespace std;

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

  int N;
  cin >> N;

  vector<int> H(N, 0);
  for (int i = 0; i < N; i++) {
    cin >> H[i];
  }

  long long result = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + H[i]; j < N; j++) {
      if (abs(i - j) == H[i] + H[j]) {
        result++;
      }
    }
  }

  cout << result;

  return 0;
}