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

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

#define ll long long
#define ld long double
#define pb push_back

#define mo 1000000007
#define inf 1e18
#define rep(i, s, n) for (ll i = s; i < n; i = i + 1)
#define rrep(i, s, n) for (ll i = s; i >= n; i--)
#define adiii                                                                  \\
  \\                               
  ios_base::sync_with_stdio(false);                                            \\
  \\                                      
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

ll mod(ll n) { return (n % (ll)mo + (ll)mo) % (ll)mo; }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

int main() {
  adiii ll n;
  cin >> n;
  vector<ll> a(n + 1);
  map<ll, ll> m;
  rep(i, 0, n) cin >> a[i];
  vector<ll> temp;
  ll ans = 0;
  rep(i, 0, n) {

    ans += m[i - a[i]];
    // cout<<i<<" "<<" "<<a[i]<<" "<<a[idx]<<" "<<ans<<endl;

    m[a[i] + i]++;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

#define ll long long
#define ld long double
#define pb push_back

#define mo 1000000007
#define inf 1e18
#define rep(i, s, n) for (ll i = s; i < n; i = i + 1)
#define rrep(i, s, n) for (ll i = s; i >= n; i--)
#define adiii                                                                  \\
  \\                               
  ios_base::sync_with_stdio(false);                                            \\
  \\                                      
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

ll mod(ll n) { return (n % (ll)mo + (ll)mo) % (ll)mo; }
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}

int main() {
  adiii ll n;
  cin >> n;
  vector<ll> a(n + 1);
  map<ll, ll> m;
  rep(i, 0, n) cin >> a[i];
  vector<ll> temp;
  ll ans = 0;
  rep(i, 0, n) {
    if (i > 0) {
      ll idx = lower_bound(temp.begin(), temp.end(), i - a[i]) - temp.begin();
      if (idx == temp.size())
        ;
      else {
        if (temp[idx] == i - a[i]) {

          ans += m[i - a[i]];
          // cout<<i<<" "<<" "<<a[i]<<" "<<a[idx]<<" "<<ans<<endl;
        }
      }
    }

    temp.pb(a[i] + i);
    sort(temp.begin(), temp.end());
    m[a[i] + i]++;
  }

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

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

using namespace std;

int main() {

  int n;
  cin >> n;

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

  long long int s[n], t[n];

  for (int i = 0; i < n; i++) {
    s[i] = i + a[i];
    t[i] = i - a[i];
  }

  sort(s, s + n);
  sort(t, t + n);

  long long int c = 0;

  for (int i = 0; i < n; i++) {
    c += upper_bound(t, t + n, s[i]) - lower_bound(t, t + n, s[i]);
  }

  cout << c << endl;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {

  int n;
  cin >> n;

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

  vector<long long int> A;

  for (int i = 0; i < n; i++) {
    if (a[i] < n)
      A.push_back(i);
  }

  long long int c = 0;

  for (int i = 0; i < A.size() - 1; i++) {
    for (int j = i + 1; j < A.size(); j++) {
      if (abs(A[i] - A[j]) == a[A[i]] + a[A[j]])
        c++;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define pow(x, y) modpow(x, y)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> B(N);
  vector<ll> C(N);
  REP(i, N) {
    cin >> A[i];
    B[i] = -A[i] + i;
    C[i] = -A[i] - i;
  }
  sort(ALL(B));
  sort(ALL(C));
  ll count = 0;
  REP(i, N) {
    count += upper_bound(ALL(B), A[i] + i) - lower_bound(ALL(B), A[i] + i);
    count += upper_bound(ALL(C), A[i] - i) - lower_bound(ALL(C), A[i] - i);
  }
  cout << count / 2 << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define pow(x, y) modpow(x, y)
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> B(N);
  REP(i, N) {
    cin >> A[i];
    B[i] = -A[i] + i;
  }
  sort(ALL(B));
  ll count = 0;
  REP(i, N) {
    count += upper_bound(ALL(B), A[i] + i) - lower_bound(ALL(B), A[i] + i);
    *lower_bound(ALL(B), -A[i] + i) = 1e15;
    sort(ALL(B));
  }
  cout << count << "\\n";
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> C(N);
  vector<ll> D(N);
  vector<ll> E(1000000);
  REPS(i, N) {
    C[i - 1] = A[i - 1] + i;
    D[i - 1] = i - A[i - 1];
  }
  REP(i, N) {
    if (D[i] >= 0) {
      E[D[i]]++;
    }
  }
  ll answer = 0;
  REP(i, N) {
    if (C[i] >= 0 && C[i] <= 800000) {
      answer += E[C[i]];
    }
    if (D[i] >= 0) {
      E[D[i]]--;
    }
  }
  cout << answer << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using P = pair<ll, ll>;
const ll mod = 1e9 + 7;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define REPS(i, n) for (ll(i) = 1; (i) <= (n); (i)++)
#define RREP(i, n) for (ll(i) = (n - 1); (i) >= 0; (i)--)
#define RREPS(i, n) for (ll(i) = (n); (i) > 0; (i)--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end());

template <class T> inline void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

template <class T> inline void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> C(N);
  vector<ll> D(N);
  vector<ll> E(1000000);
  REPS(i, N) {
    C[i - 1] = A[i - 1] + i;
    D[i - 1] = i - A[i - 1];
  }
  REP(i, N) {
    if (D[i] >= 0) {
      E[D[i]]++;
    }
  }
  ll answer = 0;
  REP(i, N) {
    if (C[i] >= 0) {
      answer += E[C[i]];
    }
    if (D[i] >= 0) {
      E[D[i]]--;
    }
  }
  cout << answer << "\\n";
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const ll INF = 1<<21;
// static const ll MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

  vector<llint> a(n);
  for (llint in = 0; in < n; in++) {
    cin >> a.at(in);
  }

  llint ans = 0;
  vector<llint> l(n), r(n);
  llint ltmp, rtmp;
  for (llint in = 0; in < n; in++) {
    ltmp = in + a.at(in);
    rtmp = in - a.at(in);
    if (ltmp < n)
      l.at(ltmp)++;
    if (rtmp >= 0)
      r.at(rtmp)++;
  }

  for (llint in = 0; in < n; in++) {
    ans += l.at(in) * r.at(in);
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

typedef long lint;
typedef long long llint;
typedef pair<int, int> pint;
typedef pair<long long, long long> pllint;

// static const int MAX = 1e6;
// static const int NIL = -1;
// static const ll INF = 1<<21;
// static const ll MOD = 1e9 + 7;

bool compPair(const pint &arg1, const pint &arg2) {
  return arg1.first > arg2.first;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}

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

  vector<int> a(n);
  for (int in = 0; in < n; in++) {
    cin >> a.at(in);
  }

  llint ans = 0;
  for (int id = 1; id <= n - 1; id++) {
    for (int ip = 0; ip < n; ip++) {
      if (ip + id >= n)
        break;
      if (abs(a.at(ip) + a.at(ip + id)) == id) {
        ans++;
        // cout << ip+1 << ip+id+1 << endl;
      }
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;

int main() {
  ll n;
  cin >> n;
  vll a(n);
  rep(i, n) cin >> a[i];
  map<ll, vll> mpp;
  map<ll, vll> mpm;
  rep(i, n) {
    ll p = i + a[i];
    ll m = i - a[i];
    mpp[p].push_back(i);
    mpm[m].push_back(i);
  }
  ll ans = 0;
  for (auto q : mpp) {
    ll k = q.first;
    vll vp = q.second;
    vll vm = mpm[k];
    ll sp = vp.size();
    ll sm = vm.size();
    ans += sp * sm;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;

int main() {
  ll n;
  cin >> n;
  vll a(n);
  rep(i, n) cin >> a[i];
  map<ll, vll> mpp;
  map<ll, vll> mpm;
  rep(i, n) {
    ll p = i + a[i];
    ll m = i - a[i];
    mpp[p].push_back(i);
    mpm[m].push_back(i);
  }
  ll ans = 0;
  for (auto q : mpp) {
    ll k = q.first;
    vll vp = q.second;
    vll vm = mpm[k];
    rep(i, vp.size()) rep(j, vm.size()) {
      if (vp[i] < vm[j])
        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;

int n;
ll ans;
map<int, int> s;

int main() {
  scanf("%d", &n);
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    ans += s[-x + i];
    s[x + i]++;
  }
  printf("%lld", ans);
  return 0;
}


Error: TLE

Fixed submission:

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

int n;
ll ans;
multiset<int> s;

int main() {
  scanf("%d", &n);
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    ans += s.count(-x + i);
    s.insert(x + i);
  }
  printf("%lld", ans);
  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 < (n); i++)
typedef long long ll;

int main() {
  int N;
  cin >> N;
  vector<int> plus(N), minus(300000, 0);
  rep(i, N) {
    int A;
    cin >> A;
    plus[i] = i + 1 + A;
    if (i + 1 - A > 0) {
      minus.at(i + 1 - A)++;
    }
  }
  ll ans = 0;
  rep(i, N) {
    if (plus[i] < 300000) {
      ans += minus[plus[i]];
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> plus(N), minus(300000, 0);
  rep(i, N) {
    int A;
    cin >> A;
    plus[i] = i + 1 + A;
    if (i + 1 - A > 0) {
      minus.at(i + 1 - A)++;
    }
  }
  ll ans = 0;
  rep(i, N) { ans += minus[plus[i]]; }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define debug(s) cout << s << endl
#define debug1(s, t) cout << s << " " << t << endl
#define debug2(s, t, u) cout << s << " " << t << " " << u << endl
#define printvec(v)                                                            \\
  for (auto &&i : v)                                                           \\
    cout << i << " ";                                                          \\
  cout << endl
#define rep(n, i) for (int i = 0; i < n; i++)

using namespace std;
using i64 = long long;

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

  vector<i64> l(n), r(n);
  map<i64, i64> fvl, fvr;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    l[i] = i + a;
    r[i] = i - a;
    fvl[l[i]]++;
    fvr[r[i]]++;
  }

  i64 ct = 0LL;
  for (auto &&kv : fvl) {
    if (fvr.find(kv.first) != fvr.end()) {
      ct += kv.second * fvr[kv.first];
    }
  }

  cout << ct << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define debug(s) cout << s << endl
#define debug1(s, t) cout << s << " " << t << endl
#define debug2(s, t, u) cout << s << " " << t << " " << u << endl
#define printvec(v)                                                            \\
  for (auto &&i : v)                                                           \\
    cout << i << " ";                                                          \\
  cout << endl
#define rep(n, i) for (int i = 0; i < n; i++)

using namespace std;
using i64 = long long;

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

  vector<int> a(n);
  vector<int> c(n);

  // ci := ai - iとおく。
  // c(i+l) == -ci - 2i となるl>0が存在するか？
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    c[i] = a[i] - i;
  }

  int ct = 0;
  for (int i = 0; i < n; i++) {
    int searchfor = -c[i] - 2 * i;
    // ソートして、二分探索する
    vector<int> cp = c;
    sort(cp.begin() + i, cp.end());
    // printvec(cp);
    auto lb = lower_bound(cp.begin() + i, cp.end(), searchfor);
    auto ub = lower_bound(cp.begin() + i, cp.end(), searchfor + 1);
    if (*lb == searchfor)
      ct += ub - lb;
  }

  cout << ct << endl;

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

#include <bits/stdc++.h>
using namespace std;
long long N, ans = 0LL;
int main() {
  cin >> N;

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

  for (int i = 0; i < N - 1; i++) {
    m[i - A[i]]--;
    ans += m[i + A[i]];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1, 0);
  vector<pair<int, int>> ret(n + 1, {0, 0});
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
    if (i + v[i] <= n) {
      ++ret[i + v[i]].second;
    }
    if (1 <= i - v[i]) {
      ++ret[i - v[i]].first;
    }
  }

  long long sum = 0;
  for (int i = 1; i <= n; ++i) {
    long long a = ret[i].first;
    long long b = ret[i].second;
    sum += a * b;
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> v(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
  }

  long long ret = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = v[i] + i + 1; j <= n; ++j) {
      if (v[i] + v[j] == j - i) {
        ++ret;
      }
    }
  }
  cout << ret << endl;

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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <unordered_map>

using namespace std;

// int mod_num_counter[2030];

int abs(int n) { return n < 0 ? -n : n; }

int main() {
  // fill(mod_num_counter,mod_num_counter+2030,0);

  int n;
  cin >> n;

  int a[n + 1];

  for (int i = 1; i <= n; i++)
    cin >> a[i];

  unordered_map<int, int> m;
  for (int i = 1; i <= n; i++) {
    int v = a[i] - i;
    auto itr = m.find(v);
    if (itr == m.end()) {
      m[v] = 1;
    } else {
      m[v]++;
    }
  }
  /*
  for(auto itr = m.begin(); itr != m.end(); ++itr) {
        std::cout << "key = " << itr->first           // キーを表示
                        << ", val = " << itr->second << "\\n";    // 値を表示
    }
  */
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    int v = -a[i] - i;
    auto itr = m.find(v);
    if (itr != m.end()) {
      ans += m[v];
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <unordered_map>

using namespace std;

// int mod_num_counter[2030];

int abs(int n) { return n < 0 ? -n : n; }

int main() {
  // fill(mod_num_counter,mod_num_counter+2030,0);

  int n;
  cin >> n;

  int a[n + 1];

  for (int i = 1; i <= n; i++)
    cin >> a[i];

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

  cout << ans << endl;

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

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

#define ll long long

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  unordered_map<ll, ll> l, r;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    ll tmpl, tmpr;
    tmpl = i - a[i];
    tmpr = i + a[i];
    l[tmpl]++;
    r[tmpr]++;
  }

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

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

Error: TLE

Fixed submission:

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

#define mod 1000000007
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define uset unordered_set
#define umap unordered_map
#define db(x) cout << #x << " == " << x << ";\\n"
#define a(x) x.begin(), x.end()
#define input(v, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> v[i]
typedef vector<int> vi;
typedef pair<int, int> pi;

const int inf = 1e5 + 7;
// ===================== MANASH =================== //

void test() {

  int n;
  cin >> n;
  int cnt = 0;

  umap<int, int> m;
  for (int i = 0; i < n; i++) {
    int t;
    cin >> t;
    cnt += m[i + 1 - t]; // INCREASING ORDER. SO ONLY BEFORE REQ.
    m[i + 1 + t]++;
  }

  cout << cnt;
}

int32_t main() {

  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  int T = 1;
  while (T--) {
    test();
    cout << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define uset unordered_set
#define umap unordered_map
#define db(x) cout << #x << " == " << x << ";\\n"
#define a(x) x.begin(), x.end()
#define input(v, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> v[i]
typedef vector<int> vi;
typedef pair<int, int> pi;

const int inf = 1e5 + 7;
// ===================== MANASH =================== //

void test() {

  int n;
  cin >> n;
  int cnt = 0;
  unordered_multiset<int> L, R;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    L.insert(i + t);
    R.insert(i - t);
  }

  for (int x : L) {
    if (R.find(x) != R.end())
      cnt += R.count(x);
  }
  cout << cnt;
}

int32_t main() {

  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);

#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  int T = 1;
  while (T--) {
    test();
    cout << "\\n";
  }
  return 0;
}