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 pb push_back
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i) >= min((a), (b))) && ((i) <= max((a), (b))))
#define show(x) cout << #x << " is " << x << "\\n";
typedef vector<ll> vi;
#define fi first
#define se second
const ll inf = 9e18;
const ll mod = 1e9 + 7;

void print(ll a[], ll n) {
  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\\n";
}
ll power(ll x, ll y, ll M = inf) {
  ll ans = 1;
  x %= M;
  while (y) {
    if (y & 1)
      ans = (x * ans) % M;

    x = (x * x) % M;
    y >>= 1;
  }
  return ans;
}
ll modInverse(ll n) { return power(n, mod - 2, mod); }
inline ll mul(ll a, ll b) { return (a * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c > mod)
    c -= mod;
  return c;
}
inline ll divi(ll a, ll b) { return mul(a, modInverse(b)); }

// const ll N = 1e5 + 1;
// vector<ll> adj[N];
// bool visited[N];

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

  ll n;
  cin >> n;
  ll a[n];
  ll mina[n];
  ll x;

  for (int i = 0; i < n; i++) {
    cin >> x;

    a[i] = i + x;
    mina[i] = i - x;
  }
  ll ans = 0;
  map<ll, ll> mp;
  for (int i = 0; i < n; i++) {
    mp[a[i]]++;
  }
  for (int i = 0; i < n; i++) {
    ans += mp[mina[i]];
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define all(t) t.begin(), t.end()
#define inrange(i, a, b) (((i) >= min((a), (b))) && ((i) <= max((a), (b))))
#define show(x) cout << #x << " is " << x << "\\n";
typedef vector<ll> vi;
#define fi first
#define se second
const ll inf = 9e18;
const ll mod = 1e9 + 7;

void print(ll a[], ll n) {
  for (ll i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << "\\n";
}
ll power(ll x, ll y, ll M = inf) {
  ll ans = 1;
  x %= M;
  while (y) {
    if (y & 1)
      ans = (x * ans) % M;

    x = (x * x) % M;
    y >>= 1;
  }
  return ans;
}
ll modInverse(ll n) { return power(n, mod - 2, mod); }
inline ll mul(ll a, ll b) { return (a * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c > mod)
    c -= mod;
  return c;
}
inline ll divi(ll a, ll b) { return mul(a, modInverse(b)); }

// const ll N = 1e5 + 1;
// vector<ll> adj[N];
// bool visited[N];

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

  ll n;
  cin >> n;
  pair<ll, ll> a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  ll ans = 0;
  sort(a, a + n);
  int i;
  for (i = n - 1; i >= 0; i--) {
    if (i != 0 && a[i].fi + a[0].fi > n - 1)
      continue;
    break;
  }
  for (int j = 0; j <= i; j++) {
    for (int k = 0; k < j; k++) {
      if (abs(a[j].se - a[k].se) == a[j].fi + a[k].fi)
        ans++;
    }
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;
map<ll, ll> mp;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += i;
    mp[a[i]]++;
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += mp[i * 2 - a[i]];
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ll ans = 0;
  for (ll i = 1; i <= n - 1; i++) {
    ll temp = 0;
    for (ll j = i + a[i]; j <= n; j++) {
      if (temp == a[j]) {
        ans++;
      }
      temp++;
    }
  }
  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++)
typedef long long ll;

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


Error: TLE

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;
  int A[n];
  rep(i, n) cin >> A[i];
  ll count = 0;
  rep(i, n) {
    for (int j = i + A[i]; j < n; j++) {
      if (A[i] + A[j] == j - i)
        count++;
    }
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, start, end) for (int i = start; i <= end; i++)
const int INF = 1001001001;
typedef long long ll;
const ll MOD = 1000000007;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> auto MAX(const T &a) {
  return *max_element(a.begin(), a.end());
}
template <class T> auto MIN(const T &a) {
  return *min_element(a.begin(), a.end());
}
template <class T, class U> U SUM(const T &a, const U &v) {
  return accumulate(a.begin(), a.end(), v);
}
template <class T, class U> U COUNT(const T &a, const U &v) {
  return count(a.begin(), a.end(), v);
}
template <class T, class U> int LOWER(const T &a, const U &v) {
  return lower_bound(a.begin(), a.end(), v) - a.begin();
}
template <class T, class U> int UPPER(const T &a, const U &v) {
  return upper_bound(a.begin(), a.end(), v) - a.begin();
}
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int LCM(int a, int b) {
  int g = GCD(a, b);
  return a / g * b;
}

int main(void) {
  // Your code here!
  int n;
  cin >> n;
  map<ll, ll> z;
  vector<ll> w(n), s(n);
  rep(i, n) {
    ll a;
    cin >> a;
    w[i] = a + i + 1;
    s[i] = i + 1 - a;
    z[s[i]]++;
  }
  ll ans = 0;
  rep(i, n) { ans += z[w[i]]; }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, start, end) for (int i = start; i <= end; i++)
const int INF = 1001001001;
typedef long long ll;
const ll MOD = 1000000007;
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> auto MAX(const T &a) {
  return *max_element(a.begin(), a.end());
}
template <class T> auto MIN(const T &a) {
  return *min_element(a.begin(), a.end());
}
template <class T, class U> U SUM(const T &a, const U &v) {
  return accumulate(a.begin(), a.end(), v);
}
template <class T, class U> U COUNT(const T &a, const U &v) {
  return count(a.begin(), a.end(), v);
}
template <class T, class U> int LOWER(const T &a, const U &v) {
  return lower_bound(a.begin(), a.end(), v) - a.begin();
}
template <class T, class U> int UPPER(const T &a, const U &v) {
  return upper_bound(a.begin(), a.end(), v) - a.begin();
}
int GCD(int a, int b) { return b ? GCD(b, a % b) : a; }
int LCM(int a, int b) {
  int g = GCD(a, b);
  return a / g * b;
}

int main(void) {
  // Your code here!
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    for (ll j = i + a[i]; a[i] <= n - i && j < n; j++) {
      if (a[i] + a[j] == j - i) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\\n";
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define cr7                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (((a) * (b)) / gcd((a), (b)))
int main() {
  cr7
#ifndef ONLINE_JUDGE
      freopen("input2.txt", "r", stdin);
  freopen("output2.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  ll arr[n + 1];
  unordered_map<ll, ll> mp;
  ll pos[n + 1], neg[n + 1];
  set<ll> vec;
  for (ll i = 1; i <= n; ++i) {
    cin >> arr[i];
    pos[i] = i + arr[i]; // mp[pos[i]]++;
    neg[i] = i - arr[i];
    if (neg[i] > 1) {
      vec.insert(neg[i]);
      mp[neg[i]]++;
    }
  }
  ll cnt = 0;
  for (ll i = 1; i <= n; ++i) {
    if (mp[pos[i]] > 0) {
      cnt += mp[pos[i]];
    }
  }
  /*for (auto it : mp) {
          if (it.second > 0) {
                  cnt += ((it.second) * (it.second - 1)) / 2;
          }
  }*/
  cout << cnt << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define deb(x) cout << #x << " " << x << "\\n";
#define pi 3.1415926535897932384626433832795
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define cr7                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (((a) * (b)) / gcd((a), (b)))

// POWER FUNCTION

/*
ll power(ll x, ll y)
{
    ll temp;
    if ( y == 0)
        return 1;
    temp = power(x, y / 2);
    if (y % 2 == 0)
        return ((temp) * (temp));
    else
        return (x * (temp) * (temp));
}
*/
int main() {
  cr7
#ifndef ONLINE_JUDGE
      freopen("input2.txt", "r", stdin);
  freopen("output2.txt", "w", stdout);
#endif
  ll n;
  cin >> n;
  ll arr[n];
  ll cnt = 0;
  for (ll i = 0; i < n; ++i)
    cin >> arr[i];
  for (ll i = 0; i < n; ++i) {
    for (ll j = i + 1; j < n; ++j) {
      if (j - i == arr[i] + arr[j]) {
        cnt++;
      }
    }
  }
  cout << cnt << "\\n";
  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++)
using namespace std;

signed main(void) {
  int n;
  long long cnt;
  map<long long, long long> mp, sp;

  cin >> n;
  rep(i, n) {
    int v;
    cin >> v;
    mp[(i + 1) + v]++;
    sp[(i + 1) - v]++;
  }
  cnt = 0;
  for (auto m : mp) {
    auto k = m.first;
    auto v = m.second;
    if (sp[k] > 0)
      cnt += v * sp[k];
  }
  cout << cnt << 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;

signed main(void) {
  int n;
  long long cnt;

  cin >> n;
  vector<int> v(n);
  rep(i, n) cin >> v[i];
  cnt = 0;
  rep(i, n - 1) {
    for (int j = i + 1; j < n; j++) {
      if (abs((j + 1) - (i + 1)) == (v[i] + v[j]))
        cnt++;
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using Pair = pair<int, int>;
using Vec = vector<int>;
using Graph = vector<Vec>;
#define INF 1000000007
#define MOD 1000000007
#define SEP " "

int main(void) {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<int> add(n);
  vector<int> sub(n);
  for (int i = 0; i < n; i++) {
    add[i] = i + a[i];
    sub[i] = i - a[i];
  }
  sort(add.begin(), add.end());
  sort(sub.begin(), sub.end());
  ll count = 0;
  auto it = add.begin();
  while (it != add.end()) {
    ll ci, cj;
    auto tmp = upper_bound(add.begin(), add.end(), *it);
    ci = tmp - it;
    cj = upper_bound(sub.begin(), sub.end(), *it) -
         lower_bound(sub.begin(), sub.end(), *it);
    count += ci * cj;
    it = tmp;
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using Pair = pair<int, int>;
using Vec = vector<int>;
using Graph = vector<Vec>;

int main(void) {
  int n;
  cin >> n;
  Vec a(n);
  for (auto i = a.begin(); i != a.end(); i++)
    cin >> *i;
  int c = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 1; i + j < n; j++) {
      if (a.at(i) + a.at(i + j) == j)
        c++;
    }
  }
  cout << c << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef unsigned long long ull;

ull A[200002];
unordered_map<ull, ull> I, J;
set<ull> X;

int main() {
  ull N;
  cin >> N;
  for (ull i = 1; i <= N; i++) {
    cin >> A[i];
    I[A[i] + i] += 1;
    J[i - A[i]] += 1;
    X.insert(A[i] + i);
  }

  ull cnt = 0;
  for (auto itr = X.begin(); itr != X.end(); itr++) {
    ull x = *itr;
    // cout << x << " " << I[x] << " "<< J[x]<< endl;
    cnt += I[x] * J[x];
  }

  cout << cnt << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int A[200002];

int main() {
  int N;
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
  }

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

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

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

int main(void) {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<int> d(N, 0);
  ll count = 0ll;
  for (int i = 0; i < N; i++) {
    if (A[i] >= N)
      continue;
    int ind = i - A[i];
    if (0 <= ind && ind < N)
      count += ll(d[ind]);

    int ind2 = A[i] + i;
    if (0 <= ind2 && ind2 < N)
      d[ind2]++;
  }
  cout << count << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main(void) {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  vector<ll> d(2 * N + 3, 0); // [-N, N], i = i+N
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (A[i] > N)
      continue;
    int ind = A[i] - i + 1;
    count += d[ind + N];
    // if(d[ind+N]>0){
    //     cout << "i=" << i << ", ind=" << ind << ", add=" << d[ind+N] << endl;
    // }

    ind = A[i] * -1 + 1 - i;
    d[ind + N]++;
    // for(int j=i+1;j<N;j++){
    //     if(j-i == A[i]+A[j]){
    //         cout << i << ", " << j << endl;
    //         count++;
    //     }
    // }
  }
  cout << count << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define lli long long int
#define rep(i, n) for (lli i = 0; i < n; i++)
#define loop(i, j, n) for (lli i = j; i < n; i++)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
// Function
lli combi(lli n, lli r) {
  if (r == 0 || r == n)
    return 1;
  else
    return combi(n - 1, r) + combi(n - 1, r - 1);
}
lli sigma1(lli n) { return n * (n + 1) / 2; }
lli sigma2(lli s, lli n) { return sigma1(n) - sigma1(s - 1); }
using namespace std;
// }}} End Header

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  lli n, ans = 0;
  cin >> n;
  vector<lli> a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  rep(i, n) {
    int sa = i - a[i];
    int wa = i + a[i];
    ans += mp[sa];
    mp[wa]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define lli long long int
#define rep(i, n) for (lli i = 0; i < n; i++)
#define loop(i, j, n) for (lli i = j; i < n; i++)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
// Function
lli combi(lli n, lli r) {
  if (r == 0 || r == n)
    return 1;
  else
    return combi(n - 1, r) + combi(n - 1, r - 1);
}
lli sigma1(lli n) { return n * (n + 1) / 2; }
lli sigma2(lli s, lli n) { return sigma1(n) - sigma1(s - 1); }
using namespace std;
// }}} End Header

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  lli n, ans = 0;
  cin >> n;
  vector<lli> a(n + 1);
  loop(i, 1, n + 1) { cin >> a[i]; }
  loop(i, 1, n) {
    loop(j, i + 1, n + 1) {
      if (j - i == a[i] + a[j])
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;

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

  long long ans = 0;
  map<int, int> map;
  for (int i = 0; i < n; i++) {
    int s = i - A[i];
    ans += map[s];
    int w = i + A[i];
    map[w]++;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, const char *argv[]) {
  long long n;
  long long ans = 0;

  cin >> n;

  vector<long long> A(n);

  for (long long i = 0; i < n; i++) {
    cin >> A[i];
  }

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

  cout << ans << endl;

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

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N_MAX 200000
#define MODVAL 1000000007

int main(void) {
  int n;
  int a[200000];
  int i, j;
  long long int ret = 0;
  int b[2000000] = {};
  int bias = 1000000;
  int diff;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] <= 200000) {
      diff = i - a[i] + bias;
      b[diff]++;
    }
  }

  if (a[0] <= 200000) {
    ret += b[a[0] + bias];
  }

  for (i = 1; i < n; i++) {
    if (a[i] <= 200000) {
      ret += (long long int)(b[a[i] + bias + i]);
    }
  }

  printf("%lld\\n", ret);

  return 0;
}


Error: -11

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N_MAX 200000
#define MODVAL 1000000007

int main(void) {
  int n;
  int a[200000];
  int i, j;
  long long int ret = 0;
  int b[2000000] = {};
  int bias = 1000000;
  int diff;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] <= 200000) {
      diff = i - a[i] + bias;
      b[diff]++;
    }
  }

  ret += b[a[0] + bias];

  for (i = 1; i < n; i++) {
    if (a[i] <= 200000) {
      ret += (long long int)(b[a[i] + bias + i]);
    }
  }

  printf("%lld\\n", ret);

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

#include <bits/stdc++.h>
#define rep(i, e, n) for (int i = e; i < (n); ++i)
using namespace std;
typedef long long ll;
using P = pair<int, int>;
const int inf = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<int> val(n + 10, 0);
  ll ans = 0;

  rep(i, 1, n + 1) {
    ll a;
    cin >> a;
    if (a + i <= n)
      val[a + i]++;
    if (i - a > 0)
      ans += val[i - a];
    //    cout << i+a << ' ' << val[i-a] << endl;
  }
  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 namespace std;
using ll = long long;
int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll mx = 0;
  rep(i, n) {
    cin >> a[i];
    mx = max(mx, a[i]);
  }
  int cnt = 0;
  rep(i, n) {
    int upper_limit = min(n, i + a[i] + mx + 1);
    // cout << upper_limit << endl;
    for (int j = a[i] + i; j < upper_limit; j++) {
      if (a[i] + a[j] == j - i) {
        cnt += 1;
        // cout << i << j << endl;
      }
    }
  }
  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;
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ff(i, c, n) for (ll i = c; i < n; i++)
#define fr(i, c, n) for (ll i = c; i >= n; --i)
#define INF 1e9 + 7

ll modpow(ll a, ll b, ll c) { // a^b%c in O(logb)
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % c;
    }
    a = (a * a) % c;
    b >>= 1;
  }
  return res;
}
ll modinv(ll m, ll n) { // modular inverse of n w.r.t m provided 1<=n<=(m-1) in
                        // O(logn) and m is prime
  ll inv[n + 1];
  inv[1] = 1;
  for (int i = 2; i < n + 1; ++i)
    inv[i] = (m - (m / i) * inv[m % i] % m) % m;
  return inv[n];
}

// a+b=a^b+2*(a&b)
int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  map<ll, ll> mp;
  ll s = 0;
  ff(i, 0, n) {
    cin >> a[i];
    mp[i - a[i]]++;
  }
  ff(i, 0, n) { s += mp[i + a[i]]; }
  cout << s;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define MP make_pair
#define F first
#define S second
#define ff(i, c, n) for (ll i = c; i < n; i++)
#define fr(i, c, n) for (ll i = c; i >= n; --i)
#define INF 1e9 + 7

ll modpow(ll a, ll b, ll c) { // a^b%c in O(logb)
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % c;
    }
    a = (a * a) % c;
    b >>= 1;
  }
  return res;
}
ll modinv(ll m, ll n) { // modular inverse of n w.r.t m provided 1<=n<=(m-1) in
                        // O(logn) and m is prime
  ll inv[n + 1];
  inv[1] = 1;
  for (int i = 2; i < n + 1; ++i)
    inv[i] = (m - (m / i) * inv[m % i] % m) % m;
  return inv[n];
}

// a+b=a^b+2*(a&b)
int main() {
  // your code goes here
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll n;
  cin >> n;
  ll a[n];
  multiset<ll> st;
  ll s = 0;
  ff(i, 0, n) {
    cin >> a[i];
    st.insert(i - a[i]);
  }
  ff(i, 0, n) { s += st.count(i + a[i]); }
  cout << s;

  return 0;
}