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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n);
  for (auto &&e : a) {
    cin >> e;
  }
  map<ll, ll> minus, plus;
  for (ll i = 0; i < n; ++i) {
    minus[i - a[i]]++;
    plus[i + a[i]]++;
  }
  for (const auto &e : plus) {
    ans += minus[e.first] * e.second;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n);
  for (auto &&e : a) {
    cin >> e;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = i + a[i] + 1; j < n; ++j) {
      if (a[j] == j - a[i] - i) {
        ans++;
      }
    }
  }
  cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())

using namespace std;
typedef long long ll;

int main() {
#ifdef RUN_LOCAL
  ifstream in("../input.txt");
  cin.rdbuf(in.rdbuf());
#endif

  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  unordered_map<int, int> set;
  ll cnt = 0;
  for (int i = 1; i <= n; ++i) {
    int h;
    cin >> h;
    if (set.count(i - h) > 0) {
      cnt += set[i - h];
    }
    set[h + i]++;
  }

  cout << cnt << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())

using namespace std;
typedef long long ll;

int main() {
#ifdef RUN_LOCAL
  ifstream in("../input.txt");
  cin.rdbuf(in.rdbuf());
#endif

  cin.tie(0);
  ios::sync_with_stdio(false);

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

  //  sort(a.begin(), a.end());

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

  cout << cnt << endl;

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

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < (long long)n; i++)
// #define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vl = vector<ll>;
using vi = vector<int>;
using Graph = vector<vi>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15); //"for float-base
  // problem"
  ll N;
  map<ll, ll> L, R;
  cin >> N;
  REP(i, N) {
    ll tmp;
    cin >> tmp;
    if (L.count(i + tmp)) {
      L.at(i + tmp)++;
    } else {
      L[i + tmp] = 1;
    }
    if (R.count(i - tmp)) {
      R.at(i - tmp)++;
    } else {
      R[i - tmp] = 1;
    }
  }
  ll ans = 0;
  for (auto a : L) {
    if (R.count(a.first)) {
      ans += (a.second * R.at(a.first));
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < (long long)n; i++)
// #define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vl = vector<ll>;
using vi = vector<int>;
using Graph = vector<vi>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15); //"for float-base
  // problem"
  ll N;
  cin >> N;
  Vl A(N);
  ll ans = 0;
  set<ll> S;

  REP(i, N) {
    cin >> A.at(i);
    S.insert(A.at(i));
  }
  if (S.empty()) {
    cout << 0 << endl;
    return 0;
  }

  for (ll j = 0; j < N; j++) {
    auto itr = S.rbegin();
    while (itr != S.rend()) {
      if (*itr + A.at(j) + j >= N) {
        itr++;
        continue;
      } else if (A.at(j + A.at(j) + *itr) == *itr) {
        //        cout << j << " " << A.at(j) + *itr << endl;
        ans++;
        itr++;
      } else
        itr++;
    }
    //    if (A.at(j) + A.at(j + *itr) == *itr - j) ans++;
  }

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

#include <bits/stdc++.h>
#define ll long long
#define ii pair<int, int>
#define N 200123
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define pb push_back
#define fs first
#define sc second
#define sz(x) ((int)((x).size()))
#define oo 1000000123
#define mod 1000000007

using namespace std;

int n;
ll A[N];
map<ll, ll> fd, fs;

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
    fs[A[i] + i]++;
    fd[i - A[i]]++;
  }
  ll ans = 0;
  for (ll i = 1; i <= n; ++i) {
    if (fs.count(i - A[i]))
      ans += (fs[i - A[i]]);
    if (fd.count(A[i] + i))
      ans += (fd[A[i] + i]);
  }
  ans /= 2LL;
  cout << ans << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ii pair<int, int>
#define N 100123
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define pb push_back
#define fs first
#define sc second
#define sz(x) ((int)((x).size()))
#define oo 1000000123
#define mod 1000000007

using namespace std;

int n;
ll A[N];
map<ll, ll> fd, fs;

int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
    fs[A[i] + i]++;
    if (i > A[i])
      fd[i - A[i]]++;
  }
  ll ans = 0;
  for (ll i = 1; i <= n; ++i) {
    if (fs.count(i - A[i]))
      ans += (fs[i - A[i]]);
    if (fd.count(A[i] + i))
      ans += (fd[A[i] + i]);
  }
  ans /= 2LL;
  cout << ans << '\\n';
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

signed main() {
  int N;
  cin >> N;
  map<int, int> dic1, dic2, dic3, dic4;
  for (int i = 1; i <= N; i++) {
    int a;
    cin >> a;
    dic1[a - i]++;
    dic2[a + i]++;
    dic3[-a - i]++;
    dic4[-a + i]++;
  }
  int ans = 0;
  for (auto p : dic1) {
    ans += dic3[p.first] * p.second;
  }
  for (auto p : dic2) {
    ans += dic4[p.first] * p.second;
  }
  cout << ans / 2 << endl;
}

Error: TLE

Fixed submission:

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

signed main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] - 1 > max(i, N - i - 1))
      continue;
    else {
      int now = i + A[i] - 1;
      while (now <= N - 1) {
        if (A[i] + A[now] == abs(i - now))
          ans++;
        now++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
using P = pair<int, int>;

int main() {
  int n, a;
  ll o = 0;
  scanf("%d", &n);
  set<P> p;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    if (i) {
      auto it = p.lower_bound(P(i - a, 0));
      if (it != p.end())
        if ((*it).first == i - a)
          o += (*it).second;
    }
    auto it = p.lower_bound(P(i + a, 0));
    if (it == p.end())
      p.insert(P(a + i, 1));
    else if ((*it).first != i + a)
      p.insert(P(a + i, 1));
    else {
      int nw = (*it).second + 1;
      p.erase(it);
      p.insert(P(a + i, nw));
    }
  }
  printf("%lld\\n", o);
}

Error: TLE

Fixed submission:

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

int main() {
  int n, a;
  ll o = 0;
  scanf("%d", &n);
  multiset<int> p;
  for (int i = 0; i < n; i++) {
    scanf("%d", &a);
    if (i)
      o += p.count(i - a);
    p.insert(a + i);
  }
  printf("%lld\\n", o);
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a.at(i);
  map<ll, queue<int>> p;
  rep(i, n) { p[a.at(i) + (i + 1)].push(i); }

  ll count = 0;
  rep(i, n) {
    ll key = i + 1 - a.at(i);
    if (p.count(key)) {
      int size = p.at(key).size();
      count += size;
    }
  }

  cout << count << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  int count = 0;
  rep(i, n) cin >> a.at(i);
  rep(i, n - 1) {
    for (int j = i + a.at(i); j < n; j++) {
      if (n <= j)
        break;
      ll dif = (ll)abs(i - j);
      ll h = (ll)a.at(i) + a.at(j);
      if (dif == h)
        count++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

//
//
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define is_in(i, s) (s.find(i) != s.end())
#define all(x) (x).begin(), (x).end()
#define outl(x) cout << x << endl
#define outsp(x) cout << x << " "
typedef long long ll;
typedef pair<int, int> P;

#define MAX_N 200005

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

  ll left[MAX_N], right[MAX_N];
  memset(left, 0, sizeof(left));
  memset(right, 0, sizeof(right));
  rep(i, n) {
    if (i >= 2 && a[i] <= i - 1)
      right[i - 1 - a[i]]++;
    if (i <= n - 3 && a[i] <= n - 2 - i)
      left[a[i] + i - 1]++;
  }
  ll ans = 0;
  rep(i, n - 2) {
    ans += left[i] * right[i];
    // outsp(left[i]);outl(right[i]);
  }
  outl(ans);

  return 0;
}


Error: 0

Fixed submission:

//
//
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define is_in(i, s) (s.find(i) != s.end())
#define all(x) (x).begin(), (x).end()
#define outl(x) cout << x << endl
#define outsp(x) cout << x << " "
typedef long long ll;
typedef pair<int, int> P;

#define MAX_N 200005

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

  int left[MAX_N], right[MAX_N];
  memset(left, 0, sizeof(left));
  memset(right, 0, sizeof(right));
  rep(i, n) {
    if (i >= 2 && a[i] <= i - 1)
      right[i - 1 - a[i]]++;
    if (i <= n - 3 && a[i] <= n - 2 + i)
      left[a[i] + i - 1]++;
  }
  ll ans = 0;
  rep(i, n - 2) {
    ans += left[i] * right[i];
    // outsp(left[i]);outl(right[i]);
  }
  outl(ans);

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

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n)                                                          \\
  ;                                                                            \\
  for (int i = s; i < (int)(n); i++)
#define ALL(vec) (vec).begin(), (vec).end()
#define pi 3.1415926535897932384626433832795
#define MAX_INF 9223372036854775807
#define MIN_INF (922337203685477587 + 1)
#define sosuu 1000000007

int main() {
  lli N;
  cin >> N;
  vector<lli> A(N);

  std::map<std::string, int> one;
  std::map<std::string, int> two;
  lli ans = 0;
  rep(i, N) {
    cin >> A[i];
    one[to_string(A[i] + i)]++;
    two[to_string(i - A[i])]++;
  }

  rep(i, N) { ans += two[to_string(A[i] + i)]; }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n)                                                          \\
  ;                                                                            \\
  for (int i = s; i < (int)(n); i++)
#define ALL(vec) (vec).begin(), (vec).end()
#define pi 3.1415926535897932384626433832795
#define MAX_INF 9223372036854775807
#define MIN_INF (922337203685477587 + 1)
#define sosuu 1000000007

int main() {
  lli N;
  cin >> N;
  vector<lli> A(N);
  rep(i, N) { cin >> A[i]; }
  lli ans = 0;
  rep(i, N) {
    lli j = A[i] + i;
    while (j < N) {
      if (A[i] + A[j] == j - i)
        ans++;
      j++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#ifdef _PRINTDEBUG
#include "lib/printdebug.hpp"
#else
#define printdebug(...) 1
#endif
#define MOD_1_000_000_007 (1000000007LL)
#define LINF (1LL << 60)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep1(i, n) for (long long i = 1; i < (long long)(n); i++)
#define rep2(i, n) for (long long i = 2; i < (long long)(n); i++)
#define rep0c(i, n) for (long long i = 0; i <= (long long)(n); i++)
#define rep1c(i, n) for (long long i = 1; i <= (long long)(n); i++)
#define rep2c(i, n) for (long long i = 2; i <= (long long)(n); i++)
#define repc0(n, i) for (long long i = (long long)(n); i >= 0; i--)
#define repc1(n, i) for (long long i = (long long)(n); i >= 1; i--)
#define repc2(n, i) for (long long i = (long long)(n); i >= 2; i--)
#define REP(i, n, m) for (long long i = (long long)(n); i < (long long)(m); i++)

namespace solver {
using namespace std;
typedef long long ll;

static const ll MAX = 200020LL;
ll N, A[MAX];
map<ll, ll> L, R;

void init() {
  scanf("%lld", &N);
  rep(i, N) scanf("%lld", A + i);
}

void solve() {
  rep(i, N) {
    L[i + A[i]]++;
    R[i - A[i]]++;
  }
  ll ans = 0;
  for (const auto &l : L) {
    ans += l.second * R[l.first];
  }
  cout << ans << endl;
}
} // namespace solver

int main() {
  solver::init();
  solver::solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#ifdef _PRINTDEBUG
#include "lib/printdebug.hpp"
#else
#define printdebug(...) 1
#endif
#define MOD_1_000_000_007 (1000000007LL)
#define LINF (1LL << 60)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define rep1(i, n) for (long long i = 1; i < (long long)(n); i++)
#define rep2(i, n) for (long long i = 2; i < (long long)(n); i++)
#define rep0c(i, n) for (long long i = 0; i <= (long long)(n); i++)
#define rep1c(i, n) for (long long i = 1; i <= (long long)(n); i++)
#define rep2c(i, n) for (long long i = 2; i <= (long long)(n); i++)
#define repc0(n, i) for (long long i = (long long)(n); i >= 0; i--)
#define repc1(n, i) for (long long i = (long long)(n); i >= 1; i--)
#define repc2(n, i) for (long long i = (long long)(n); i >= 2; i--)
#define REP(i, n, m) for (long long i = (long long)(n); i < (long long)(m); i++)

namespace solver {
using namespace std;
typedef long long ll;

static const ll MAX = 200020LL;
ll N, A[MAX], L[MAX], R[MAX];

void init() {
  scanf("%lld", &N);
  rep(i, N) {
    scanf("%lld", A + i);
    L[i] = -1;
    R[i] = -1;
  }
}

void solve() {
  ll lmax = 0, rmax = 0;
  rep(i, N) {
    lmax = max(lmax, A[i]);
    L[i] = lmax;
    rmax = max(rmax, A[N - i - 1]);
    R[N - i - 1] = rmax;
  }
  ll ans = 0;
  rep(i, N) {
    for (ll j = i + 1; j < N; j++) {
      if (abs(i - j) == A[i] + A[j])
        ans++;
      if (R[j] < abs(i - j) - A[i])
        break;
    }
    // for(ll j = i-1; j>=0; j--) {
    //   if(abs(i-j) == A[i] + A[j]) ans++;
    //   if(L[j] < abs(i-j) - A[i]) break;
    // }
  }
  cout << ans << endl;
}
} // namespace solver

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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#define lli long long int
using namespace std;
#define S second
#define F first
#define pb push_back
#define M1 1000000007
#define M2 998244353
#define plli pair<lli, lli>
#define setit set<lli>::iterator seti
#define mapit map<lli, lli>::iterator mapi
#define MP make_pair
#define MT make_tuple
// map      iterator->first  iterator.first
// map      mp[key]=value
// set      *s.begin()
// vector   v[j]=value
// pair     p.first p.second
// vector of pairs v[j].first v[j].second
// tuple    get<index>(tuple_name)
// vector of tuples get<index>(v[j])  sort(v.begin(),v.end())
// priority queue .top() .pop() .push()
// __gcd(x,y)
#define fr(j, a, b) for (j = a; j < b; j++)

int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lli t, i, j, n, x, k;
  lli l = 0;
  cin >> n;
  lli a[n];
  map<lli, lli> mp;
  for (j = 0; j < n; j++) {
    cin >> a[j];
    l = l + mp[j + 1 - a[j]];
    mp[j + 1 + a[j]]++;
  }
  cout << l;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#define lli long long int
using namespace std;
#define S second
#define F first
#define pb push_back
#define M1 1000000007
#define M2 998244353
#define plli pair<lli, lli>
#define setit set<lli>::iterator seti
#define mapit map<lli, lli>::iterator mapi
#define MP make_pair
#define MT make_tuple
// map      iterator->first  iterator.first
// map      mp[key]=value
// set      *s.begin()
// vector   v[j]=value
// pair     p.first p.second
// vector of pairs v[j].first v[j].second
// tuple    get<index>(tuple_name)
// vector of tuples get<index>(v[j])  sort(v.begin(),v.end())
// priority queue .top() .pop() .push()
// __gcd(x,y)
#define fr(j, a, b) for (j = a; j < b; j++)

int power(int x, unsigned int y, int p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  lli t, i, j, n, x, k;
  t = 1;
  lli l = 0;
  cin >> n;
  lli a[n];
  x = 0;
  map<lli, lli> b;
  map<lli, lli> c;
  map<lli, lli> d;
  for (j = 0; j < n; j++) {
    cin >> a[j];
    b[a[j] + j + 1]++;

    c[j + 1 - a[j]]++;
    if (a[j] + j + 1 > x) {
      x = a[j] + j + 1;
    }
  }
  for (j = 0; j <= x; j++) {
    if (b[j] > 0 && c[j] > 0) {
      l = l + (b[j] * c[j]);
    }
  }
  cout << l;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  int a[n];
  ll ans = 0;
  rep(i, n) { cin >> a[i]; }
  for (int i = 0; i < n; i++) {
    for (int r = i + 1; r < n; r++) {
      if (r - i == a[i] + a[r])
        ans++;
    }
  }
  cout << ans << 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 < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  vector<int> B(N);
  vector<int> C(N);
  rep(i, N) {
    cin >> A.at(i);
    B.at(i) = A.at(i) + (i + 1);
    C.at(i) = -1 * (A.at(i) - (i + 1));
  }
  // Bi=Ai+i,Ci=-(Ai-i)を持っておき、Bi=Ciの個数をそれぞれカウントして
  // ⇒例えばB={1,1,0,-3,-2,-5},C={-3,-5,-6,-5,-8,-7}だったら
  //-3のところで1*1組、-5のところで1*2組、よって3組
  // B,Cをソート
  sort(all(B));
  sort(all(C));

  map<int, int> Cmap;
  rep(i, N) {
    if (Cmap.count(C.at(i))) {
      Cmap[C.at(i)]++;
    } else {
      Cmap[C.at(i)] = 1;
    }
  }
  long long ans = 0;
  rep(i, N) {
    if (Cmap.count(B.at(i))) {
      ans += Cmap[B.at(i)];
    } else {
      continue;
    }
  }
  cout << ans << endl;
  // int Bcnt=1;
  // int Ccnt=0;
  // int tmp=B.at(0);
  // int ans =0;
  // rep(i,N){
  //     // cout<<"B:"<<B.at(i)<<" C:"<<C.at(i)<<endl;
  // }
  // //探す
  // rep2(i,1,N){
  //     if(tmp==B.at(i)&&i!=N-1){
  //         Bcnt++;
  //         continue;
  //         //とりあえず同じ数だったらBを進め続ける
  //     }else{
  //         if(tmp==B.at(i)&&i==N-1){
  //             Bcnt++;
  //         }
  //         //違う数になったらtmpの数がCに何個あるか数える
  //         int lowind=lower_bound(C.begin(),C.end(),tmp)-C.begin();
  //         // cout << "low:"<<lowind;
  //         int uppind=upper_bound(C.begin(),C.end(),tmp)-C.begin();
  //         // cout << " upp:"<<uppind<<endl;
  //         Ccnt=uppind-lowind;
  //         // cout << "tmp:"<<tmp<<endl;
  //         // cout << "Bcnt:"<<Bcnt<<endl;
  //         // cout << "Ccnt:"<<Ccnt<<endl;

  //         ans=ans+Bcnt*Ccnt;
  //         tmp=B.at(i);//tmpを初期化
  //         Bcnt=1;//Bcntを初期化
  //     }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
#define all(n) begin(n), end(n)
struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} star;
const long long INF = numeric_limits<long long>::max();
int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) { cin >> A.at(i); }
  // TLE
  long long ans = 0;
  rep(i, N - 1) {
    // 左端固定
    rep2(j, i + A.at(i) + 1, N) {
      if (j - i == A.at(j) + A.at(i)) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  map<ll, ll> q;
  ll n;
  cin >> n;
  ll a[200010];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  q[1 + a[0]]++;
  ll ans = 0;
  for (ll i = 1; i < n; i++) {
    ll now = i + 1 - a[i];
    ans += q[now];
    q[i + 1 + a[i]]++;
  }
  cout << ans;
  // your code goes here
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n;
  cin >> n;
  vector<pair<ll, ll>> a;
  for (ll i = 0; i < n; i++) {
    ll now;
    cin >> now;
    if (now <= n) {
      a.push_back(make_pair(now, i + 1));
    }
  }
  sort(a.begin(), a.end());
  ll ans = 0;
  for (ll i = 0; i < a.size(); i++) {
    ll now = a[i].first;
    ll num = a[i].second;
    for (ll j = i + 1; j < a.size(); j++) {
      ll nex = a[j].first;
      ll nnum = a[j].second;
      if (now + nex > n) {
        break;
      }
      if (now + nex == abs(num - nnum)) {
        ans++;
      }
    }
  }
  cout << ans;
  // your code goes here
  return 0;
}