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 dump(x) cerr << #x << "=" << x << endl

#define rep(i, n) for (int i = 0; i < n; i++)

ll N;
vector<ll> A;

void solve() {
  ll result = 0;
  map<int, int> mp;

  rep(i, N) {
    result += mp[i - A.at(i)];
    mp[i + A.at(i)]++;
  }

  cout << result << endl;
}

int main() {
  cin >> N;
  A.resize(N);

  rep(i, N) { cin >> A.at(i); }

  solve();

  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 < n; i++)

ll N;
vector<ll> A;
ll highest = 0;

void solve() {
  ll result = 0;

  for (int i = 0; i < N - 1; i++) {
    int jmin = i + A.at(i);
    int jmax = min(N, A.at(i) + highest + i + 1);
    for (int j = jmin; j < jmax; j++) {
      if (abs(j - i) == A.at(i) + A.at(j)) {
        result++;
      }
    }
  }

  cout << result << endl;
}

int main() {
  cin >> N;
  A.resize(N);

  rep(i, N) {
    cin >> A.at(i);
    highest = max(highest, A.at(i));
  }

  solve();

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

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

void solve() {
  ll n;
  cin >> n;
  ll a[n + 1];
  map<ll, ll> m;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    m[i + a[i]]++;
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += m[i - a[i]];
  }
  cout << ans;
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

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

void solve() {
  ll n;
  cin >> n;
  ll a[n + 1];
  map<ll, unordered_set<ll>> m;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    m[a[i]].emplace(i);
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    for (auto j : m) {
      if (i + a[i] + j.first > 0 &&
          j.second.find(i + a[i] + j.first) != j.second.end())
        ans++;
      if (i - a[i] - j.first > 0 &&
          j.second.find(i - a[i] - j.first) != j.second.end())
        ans++;
    }
  }
  cout << ans / 2;
}

int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<pair<ll, ll>> v;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    v.push_back(make_pair(a, i + 1));
  }
  vector<ll> count;
  vector<ll> u;
  map<ll, ll> mp;
  for (int i = 0; i < n; i++) {
    count.push_back(v[i].second - v[i].first);
    u.push_back(v[i].first + v[i].second);
    mp[v[i].first + v[i].second] = 0;
  }
  for (int i = 0; i < n; i++) {
    mp[u[i]]++;
  }
  ll counter = 0;
  for (int i = 0; i < n; i++) {
    counter += mp[count[i]];
  }
  cout << counter << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<pair<ll, ll>> v;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    v.push_back(make_pair(a, i + 1));
  }
  multiset<ll> s1;
  multiset<ll> s2;
  for (int i = 0; i < n; i++) {
    s1.insert(v[i].second - v[i].first);
    s2.insert(v[i].second + v[i].first);
  }
  ll ans = 0;
  multiset<ll>::iterator itr;
  for (itr = s1.begin(); itr != s1.end(); itr++) {
    ans += s2.count(*itr);
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  map<int, pair<int, int>> m;
  for (int i = 1; i <= n; i++) {
    int p1 = i + (a[i] - 1);
    int p2 = i - (a[i] - 1);
    if (p1 >= 1 and p1 <= n)
      m[p1].first += 1;
    if (p2 >= 1 and p2 <= n)
      m[p2].second += 1;
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int p1 = i + (a[i] - 1) + 2;
    int p2 = i - (a[i] - 1) - 2;
    ans += m[p1].second + m[p2].first;
  }
  cout << ans / 2 << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;
  int h[n];
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    for (int j = h[i] + i; j < n; j++) {
      if (1LL * h[i] + 1LL * h[j] == j - i)
        cnt++;
    }
  }
  cout << cnt;
  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 rep1(i, n) for (int i = 1; i <= (n); ++i)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;

int main() {
  map<int, int> l, r;
  int n;
  cin >> n;

  ll ans = 0;

  rep(i, n) {
    int a;
    cin >> a;
    l[i + 1 - a]++;
    r[i + 1 + a]++;

    // cout<<ans<<endl;
    if (r.find(i + 1 - a) == r.end()) {
      continue;
    } else {
      ans += r[i + 1 - a];
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

using namespace std;
using ll = long long;
using P = pair<int, int>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;

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

int n;
long long ans;
map<int, int> a;

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


Error: TLE

Fixed submission:

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

int n;
int h[200005];
// int a[200005];

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> h[i];
    //		a[i] = i;
  }

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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e7 + 10;
long long n, ans;
map<long long, long long> a;
int main() {
  scanf("%lld", &n);
  for (int i = 1, x; i <= n; i++) {
    scanf("%lld", &x);
    a[i + x]++;
    ans += a[i - x];
  }
  printf("%lld", ans);
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e7 + 10;
int n, a[maxn], ans;
int main() {
  scanf("%d", &n);
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    a[i + x + 400000]++;
    ans += a[i - x + 400000];
  }
  printf("%d", ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  unordered_map<int, int> L, R;
  REP(i, 0, N) {
    cin >> A[i];
    L[i + A[i]]++;
    R[i - A[i]]++;
  }

  ll ans = 0;
  for (auto l : L) {
    ans += (ll)l.second * R[l.first];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> A(N), idxs(N);
  REP(i, 0, N) { cin >> A[i]; }
  iota(ALL(idxs), 0);
  sort(ALL(idxs), [&](auto l, auto r) { return A[l] < A[r]; });

  ll count = 0;
  ll ans = 0;
  REP(i, 0, N) {
    for (auto j : idxs) {
      if (A[j] > N - i - 1 - A[i])
        break;
      if (j <= i)
        continue;
      if (abs(i - j) == A[i] + A[j]) {
        // cout << "i : " << i << " j : " << j << endl;
        ans++;
      }
      count++;
    }
  }
  // cout << "count : " << count << endl;
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pi;

// #define int ll
#define forl(i, a, b) for (int i = a; i < b; i++)
#define rfor(i, b, a) for (int i = b; i >= a; i--)
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define srt(x) sort(x.begin(), x.end())
#define sumup(x) accumulate(x.begin(), x.end(), 0)
#define PI 3.141592653589793238
#define MOD 1000000007
#define loop(n) for (int i = 0; i < n; i++)
#define rloop(n) for (int i = n - 1; i >= 0; i--)

void io() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void solve() {
  int n;
  cin >> n;
  vl a(n);
  loop(n) cin >> a[i];
  map<int, int> mk;
  ll ans = 0;
  loop(n) {

    if (mk[i + 1 - a[i]])
      ans += mk[i + 1 - a[i]];
    else
      mk[i + 1 - a[i]] = 0;

    mk[i + 1 + a[i]]++;
  }
  cout << ans << "\\n";
  return;
}
int32_t main() {
  io();
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

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

typedef long long ll;

const int MX = 200006;
int a[MX];

int main() {
  int t = 1;
  // scanf("%d",&t);
  while (t--) {
    int n;
    ll ans = 0;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }
    for (int i = 1; i <= n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (a[i] + a[j] == abs(i - j))
          ans++;
      }
    }
    printf("%lld\\n", ans);
  }
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  map<ll, ll> l, r;

  rep(i, n) {
    ++l[i + a[i]];
    ++r[i - a[i]];
  }

  ll ans = 0;
  for (auto &p : l) {
    ans += p.second * r[p.first];
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

  ll ans = 0;
  ma[N - 1] = A[N - 1];
  for (int i = N - 2; i >= 0; --i) {
    ma[i] = max(ma[i + 1], A[i]);
  }

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

  cout << ans << endl;

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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 2e5 + 5;
const ll maxm = 1e3 + 5;
template <class T> inline T qread(T &IEE) {
  register T x = 0, f = 1;
  register char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') {
      f = -f;
    }
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return IEE = x * f;
}
template <class T> inline void qwrite(T a) {
  if (!a) {
    putchar('0');
    return;
  }
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a > 9)
    qwrite(a / 10);
  putchar(a % 10 + 48);
  return;
}
template <class T> inline T ab(T a) { return (a > 0) ? a : -a; }
template <class T> inline void swa(T &a, T &b) {
  a ^= b ^= a ^= b;
  return;
}
ll cnt, n;
ll a[maxn];
map<ll, ll> miu;
int main() {
  //	freopen("1.in", "r", stdin);
  //	freopen("1.out", "w", stdout);
  qread(n);
  for (int i = 1; i <= n; i++) {
    qread(a[i]);
    miu[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    miu[i - a[i]]--;
    cnt += miu[a[i] + i];
  }
  cout << cnt << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 1e5 + 5;
const ll maxm = 1e3 + 5;
template <class T> inline T qread(T &IEE) {
  register T x = 0, f = 1;
  register char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') {
      f = -f;
    }
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return IEE = x * f;
}
template <class T> inline void qwrite(T a) {
  if (!a) {
    putchar('0');
    return;
  }
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a > 9)
    qwrite(a / 10);
  putchar(a % 10 + 48);
  return;
}
template <class T> inline T ab(T a) { return (a > 0) ? a : -a; }
template <class T> inline void swa(T &a, T &b) {
  a ^= b ^= a ^= b;
  return;
}
ll cnt, n;
ll a[maxn];
map<ll, ll> miu;
int main() {
  //	freopen("1.in", "r", stdin);
  //	freopen("1.out", "w", stdout);
  qread(n);
  for (int i = 1; i <= n; i++) {
    qread(a[i]);
    miu[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    miu[i - a[i]]--;
    cnt += miu[a[i] + i];
  }
  cout << cnt << 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 zero = 0;
const ll INF = 10000000000000000; // 10^16
const ll MOD = 1000000007;        // 10^9+7
const ll MOD2 = 998244353;

int main(void) {
  ll N;
  cin >> N;
  vector<ll> a(N + 1);
  for (ll i = 1; i <= N; i++) {
    cin >> a.at(i);
  }

  vector<ll> R(N + 1), L(N + 1);
  for (ll i = 1; i <= N; i++) {
    R.at(i) = i - a.at(i);
    L.at(i) = i + a.at(i);
  }

  map<ll, ll> test;
  ll ans = 0;
  for (ll i = 1; i <= N; i++) {
    test[L.at(i)]++;

    ans += test[R.at(i)];
  }

  printf("%lld\\n", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll zero = 0;
const ll INF = 10000000000000000; // 10^16
const ll MOD = 1000000007;        // 10^9+7
const ll MOD2 = 998244353;

int main(void) {
  ll N;
  cin >> N;
  vector<ll> a(N);
  for (ll i = 0; i < N; i++) {
    cin >> a.at(i);
  }
  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    for (ll j = i + a.at(i); j < N; j++) {
      if (j - i == a.at(i) + a.at(j))
        ans++;
    }
  }
  printf("%lld", ans);
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;

// --------------------------------------
int n;
int a[200005];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  vector<int> sum(n, 0);

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

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;

// --------------------------------------
int n;
int a[100005];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  vector<int> sum(n, 0);

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

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

#include <iostream>
#include <map>
/*
 j>iとする
 j - i   = A_j + A_i
 j - A_j = i + A_i
*/

int main() {
  int N, sa, wa;
  long long ans = 0;
  std::map<int, int> mp;
  //<i+A_i, i+A_iの個数>

  std::cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }

  // jを固定して、条件式を満たすi(<j)を探す
  for (int j = 0; j < N; j++) {
    sa = j - A[j];
    ans += mp[sa];

    // 終了したjは、以降のjの候補(i+A_i)になる。
    wa = j + A[j];
    mp[wa]++;
  }

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

Error: TLE

Fixed submission:

#include <iostream>

int main() {
  int N;
  int ans = 0;

  std::cin >> N;
  int A[N];
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }

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

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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

Int A[200000 + 10];
int main(void) {
  int N;
  cin >> N;

  map<Int, Int> dic;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    dic[i + A[i]]++;
  }

  //|i-j| == Ai+Aj
  //--> j-Aj, i+Ai
  Int ans = 0;
  for (Int i = 1; i <= N; i++) {
    ans += dic[i - A[i]];
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD (1000000007)

using namespace std;

typedef long long int Int;

constexpr Int TEN(int n) { return n == 0 ? 1 : 10 * TEN(n - 1); }

int A[200000 + 10];
int main(void) {
  int N;
  cin >> N;

  vector<pair<int, int>> P;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    P.push_back({A[i], i});
  }

  sort(P.begin(), P.end());
  int ans = 0;
  for (int i = 0; i < P.size(); i++) {
    if (P[i].first >= N - 1)
      break;
    for (int j = i + 1; j < P.size(); j++) {
      if (P[i].first + P[j].first >= N)
        break;

      if (P[i].first + P[j].first == abs(P[i].second - P[j].second)) {
        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;

#define INF (int)1e9 + 7
#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 vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define N 100005
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795

ll binPowM(ll a, ll b, ll m) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return ans % m;
}
ll f(ll x, ll y, ll a) { return (a & x) * (a & y); }

int main() {
  ll n, cnt = 0;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += i;
  }
  map<ll, ll> m;
  for (int i = 1; i <= n; i++) {
    m[2 * i - a[i]]++;
  }

  for (int i = 1; i <= n; i++) {
    if (m.count(a[i]))
      cnt += m[a[i]];
  }
  cout << cnt << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

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

#define INF (int)1e9 + 7
#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 vi vector<int>
#define vl vector<ll>
#define vii vector<vector<int>>
#define vll vector<vector<long long>>
#define pii pair<int, int>
#define pll pair<long, long>
#define vpii vector<pii>
#define vpll vector<pll>
#define N 100005
#define pb push_back
#define mp make_pair
#define PI 3.1415926535897932384626433832795

ll binPowM(ll a, ll b, ll m) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1)
      ans = ans * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return ans % m;
}
ll f(ll x, ll y, ll a) { return (a & x) * (a & y); }

int main() {
  ll n, cnt = 0;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++) {
    ll p = a[i] + i;
    for (int j = p + 1; j <= n; j++) {
      if (p + a[j] == j)
        cnt++;
    }
  }
  cout << cnt << "\\n";

  return 0;
}