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 MOD 1000000007
ll fact[200005];
#define eps 1e-9
#define INF 1e17
#define N 200005
inline ll mul(ll a, ll b) {
  a = (a + MOD) % MOD;
  b = (b + MOD) % MOD;
  ll c = a * b;
  c = (c + MOD) % MOD;
  return c;
}
ll power(ll a, ll b, ll c) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = mul(ans, a);
    }
    a = mul(a, a);
    b = b / 2;
  }
  return ans;
}
ll min(ll a, ll b) { return a < b ? a : b; }
ll max(ll a, ll b) { return a > b ? a : b; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll a[n + 1];
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  map<ll, ll> m1, m2;
  m2[1 + a[1]]++;
  ll cnt = 0;
  for (int i = 2; i <= n; ++i) {
    ll x = i - a[i];
    cnt = cnt + m2[x];
    m2[i + a[i]]++;
  }
  cout << cnt;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
ll fact[200005];
#define eps 1e-9
#define INF 1e17
#define N 200005
inline ll mul(ll a, ll b) {
  a = (a + MOD) % MOD;
  b = (b + MOD) % MOD;
  ll c = a * b;
  c = (c + MOD) % MOD;
  return c;
}
ll power(ll a, ll b, ll c) {
  ll ans = 1;
  while (b) {
    if (b & 1) {
      ans = mul(ans, a);
    }
    a = mul(a, a);
    b = b / 2;
  }
  return ans;
}
ll min(ll a, ll b) { return a < b ? a : b; }
ll max(ll a, ll b) { return a > b ? a : b; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n;
  cin >> n;
  ll a[n + 1];
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  vector<set<int>> v(200004);
  ll cnt = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] >= 200001)
      continue;
    v[a[i]].insert(i);
  }
  for (int i = 2; i <= n; ++i) {
    if (a[i] >= i)
      continue;
    for (int j = 1; j <= i - a[i]; j++) {
      int k = i - j - a[i];
      if (v[j].count(k))
        cnt++;
    }
  }
  cout << cnt;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

/*BISMILLAH
THE WHITE WOLF
NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef vector<int> vi;

#define io ios_base::sync_with_stdio(false)
#define pb push_back
#define eb emplace_back
#define mod 998244353
#define PI 2 * acos(0.0)
int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1};

ll bigmod(ll x, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    p >>= 1;
  }
  return res;
}

//=============================================ASIFAZAD==============================================//

int32_t main() {
  io;
  int n;
  cin >> n;
  multiset<ll> left;
  map<ll, int> rit;
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    left.insert(x + i);
    rit[i - x]++;
  }
  ll ans = 0;
  for (auto it = left.begin(); it != left.end(); it++)
    ans += rit[*it];
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

/*BISMILLAH
THE WHITE WOLF
NO DREAM IS TOO BIG AND NO DREAMER IS TOO SMALL*/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef vector<int> vi;

#define io ios_base::sync_with_stdio(false)
#define pb push_back
#define eb emplace_back
#define mod 998244353
#define PI 2 * acos(0.0)
int dirx[] = {1, -1, 0, 0}, diry[] = {0, 0, 1, -1};

ll bigmod(ll x, ll p) {
  ll res = 1;
  while (p) {
    if (p & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    p >>= 1;
  }
  return res;
}

//=============================================ASIFAZAD==============================================//

int32_t main() {
  io;
  int n;
  cin >> n;
  multiset<ll> left, rit;
  for (int i = 1; i <= n; i++) {
    ll x;
    cin >> x;
    left.insert(x + i);
    rit.insert(i - x);
  }
  int ans = 0;
  for (auto it = left.begin(); it != left.end(); it++)
    ans += rit.count(*it);
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (int)1e9 + 7
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define vi vector<int>
int main() {

#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  fio;
  int n;
  cin >> n;
  vector<int> v(n + 1);
  map<int, int> m;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    ans += m[-i + v[i]];
    m[-i - v[i]]++;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (int)1e9 + 7
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define vi vector<int>
int main() {

#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  fio;
  int n;
  cin >> n;
  vector<int> v(n);
  map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    m[v[i]] = i;
  }
  int count = 0;
  int l = 0, r;
  for (r = 1; r < n; r++) {
    for (l = 0; l < r; l++) {
      if (v[l] + v[r] == abs(l - r))
        count++;
    }
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

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

int main() {
  int n;
  cin >> n;
  map<int, int> s;
  ll cur = n - 1, ans = 0;
  FOR(i, 1, n) {
    int tt;
    cin >> tt;
    auto e = s.find(tt + cur);
    if (e != s.end())
      ans += e->se;
    s[n - tt - i]++;
    cur--;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define ROF(i, a, b) for (int i = a; i >= (b); i--)

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

int main() {
  int n;
  cin >> n;
  multiset<int> s;
  ll cur = n - 1, ans = 0;
  FOR(i, 1, n) {
    int tt;
    cin >> tt;
    ll tar = tt + cur;
    int ub = distance(s.begin(), s.upper_bound(tar));
    int lb = distance(s.begin(), s.lower_bound(tar));
    ans += ub - lb;
    s.insert(n - tt - i);
    cur--;
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <map>
using namespace std;

map<long long, int> m1, m2;

int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    long long c;
    scanf("%lld", &c);
    ++m1[i + c];
    ++m2[i - c];
  }
  long long ans = 0;
  for (map<long long, int>::iterator it = m1.begin(); it != m1.end(); ++it) {
    long long v = it->first;
    ans += (long long)m1[v] * m2[v];
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#define MAXN 200005

long long a[MAXN];

int main(void) {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%lld", &a[i]);
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int l = a[i] + 1; i + l <= n; ++l) {
      if (a[i] + a[i + l] == l)
        ++ans;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

#define EPS (1e-7)
#define INF 1e18
#define max(p, q) ((p) > (q) ? (p) : (q))
#define min(p, q) ((p) < (q) ? (p) : (q))
#define PI (acos(-1))

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

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  map<ll, ll> p, n;
  REP(i, N) {
    p[i + A[i]]++;
    if (i - A[i] >= 0)
      n[i - A[i]]++;
  }
  ll ans = 0;
  for (auto iter = p.begin(); iter != p.end(); iter++) {
    ll i = iter->first;
    ans += p[i] * n[i];
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

#define EPS (1e-7)
#define INF 1e18
#define max(p, q) ((p) > (q) ? (p) : (q))
#define min(p, q) ((p) < (q) ? (p) : (q))
#define PI (acos(-1))

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

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> p((1e5) * 2 + 1, 0), n((1e5) * 2 + 1, 0);
  REP(i, N) {
    p[i + A[i]]++;
    if (i - A[i] >= 0)
      n[i - A[i]]++;
  }
  ll ans = 0;
  REP(i, (1e5) * 2 + 1) { ans += p[i] * n[i]; }
  cout << ans << endl;

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


#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <ios>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <numeric>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
const long long LINF = 1e18;
const int INF = 1e9;

int main() {
  ll ans = 0;
  int n;
  cin >> n;
  map<int, ll> mp_l;
  map<int, ll> mp_r;
  for (int i = 1; i <= n; ++i) {
    int temp;
    cin >> temp;
    mp_l[i + temp]++;
    mp_r[i - temp]++;
  }

  for (auto L : mp_l) {
    ans += L.second * mp_r[L.first];
  }

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


Error: TLE

Fixed submission:

//
// Created on 2020/05/03.
//

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <ios>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <numeric>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define all(x) (x).begin(), (x).end()
typedef long long ll;
using namespace std;
const long long LINF = 1e18;
const int INF = 1e9;

int main() {
  int ans = 0;
  int n;
  cin >> n;
  vector<int> high(n + 1);
  high[0] = -1;
  for (int i = 1; i <= n; ++i) {
    cin >> high[i];
  }
  for (int j = 2; j <= n - 1; ++j) {
    for (int i = 1; i + j <= n; ++i) {
      if (high[i] + high[i + j] == j) {
        ans++;
      }
    }
  }

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

// https://atcoder.jp/contests/abc166/tasks/abc166_e

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <vector>

using uint = unsigned int;
signed main() {
  uint n;
  std::cin >> n;
  std::vector<uint> a;
  a.reserve(n);
  for (uint i = 0; i < n; ++i) {
    uint t;
    std::cin >> t;
    a.emplace_back(t);
  }

  std::vector<uint> ansv(8);

  const uint n8 = n / 8;
  uint now = 0;
  for (int k = 0; k < 8; ++k) {
    const uint nown8 = now + n8;
    for (uint i = now; i < nown8; ++i) {
      for (uint j = i + 1; j < n; ++j) {
        ansv[k] += (j - i) == a[j] + a[i];
      }
    }
    now += n8;
  }
  for (uint i = now; i < n; ++i) {
    for (uint j = i + 1; j < n; ++j) {
      ansv[0] += (j - i) == a[j] + a[i];
    }
  }

  long long ans = 0;
  for (const auto &x : ansv) {
    ans += x;
  }
  std::cout << ans << std::endl;

  return 0;
}

Error: TLE

Fixed submission:

// https://atcoder.jp/contests/abc166/tasks/abc166_e

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,avx512f")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <vector>

using uint = unsigned int;
signed main() {
  uint n;
  std::cin >> n;
  std::vector<uint> a;
  a.reserve(n);
  for (uint i = 0; i < n; ++i) {
    uint t;
    std::cin >> t;
    a.emplace_back(t);
  }

  std::vector<uint> ansv(8);

  for (uint i = 0; i < n; ++i) {
    const uint n8 = (n - i - 1) / 8;
    for (uint j = 0; j < n8; ++j) {
      uint j8 = i + 1 + 8 * j;
      for (uint k = 0; k < 8; ++k) {
        ansv[k] += (k + j8 - i) == a[k + j8] + a[i];
      }
    }
    uint s = i + 1 + n8 * 8;
    for (uint k = s; k < n; ++k) {
      ansv[k - s] += (k - i) == a[k] + a[i];
    }
  }
  long long ans = 0;
  for (const auto &x : ansv) {
    ans += x;
  }
  std::cout << ans << std::endl;

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

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

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define all(n) n.begin(), n.end()
#define print(n) cout << n << endl

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

  ll n;
  cin >> n;
  vector<ll> a(n);
  ll bmin = 1 << 29, cmax = 0;
  rep(i, n) {
    cin >> a[i];
    if (i + a[i] < bmin)
      bmin = i + a[i];
    if (i - a[i] > cmax)
      cmax = i - a[i];
  }

  if (cmax - bmin < 0) {
    print(0);
    return 0;
  }

  vector<pair<ll, ll>> x(cmax - bmin + 1);
  rep(i, n) {
    if (i + a[i] <= cmax) {
      x[i + a[i] - bmin].first++;
    }
    if (i - a[i] >= bmin) {
      x[i - a[i] - bmin].second++;
    }
  }
  ll ans = 0;
  for (auto i : x) {
    if (i.first != 0 && i.second != 0)
      ans += i.first * i.second;
  }
  print(ans);
  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++)
#define rrep(i, n) for (int i = (n); i > 0; i--)
#define all(n) n.begin(), n.end()
#define print(n) cout << n << endl

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

  ll n;
  cin >> n;
  vector<ll> a(n), min(n), max(n);
  rep(i, n) { cin >> a[i]; }
  rep(i, n) {
    if (i == 0) {
      min[n - 1] = a[n - 1];
      max[n - 1] = a[n - 1];
      continue;
    }
    if (a[n - 1 - i] < min[n - i])
      min[n - 1 - i] = a[n - 1 - i];
    else
      min[n - i - 1] = min[n - i];
    if (a[n - 1 - i] > max[n - i])
      max[n - 1 - i] = a[n - 1 - i];
    else
      max[n - i - 1] = max[n - i];
  }

  ll ans = 0;
  rep(i, n) {
    for (int j = i + a[i] + min[i]; j < n; j++) {
      if (j - i > a[i] + max[j])
        break;
      if (j - i == a[i] + a[j])
        ans++;
    }
  }
  print(ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int64_t n;
  cin >> n;
  vector<int64_t> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  vector<int64_t> l(n);
  vector<int64_t> r(n);
  for (int64_t i = 0; i < n; i++) {
    int64_t l_idx = i - a.at(i);
    int64_t r_idx = i + a.at(i);
    if (l_idx > 0) {
      l.at(l_idx)++;
    }
    if (r_idx < n - 1) {
      r.at(r_idx)++;
    }
  }
  int64_t cnt = 0;
  for (int i = 1; i < n - 1; i++) {
    cnt += l.at(i) * r.at(i);
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int64_t n;
  cin >> n;
  vector<int64_t> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int cnt = 0;
  for (int64_t i = 0; i < n; i++) {
    if (i + a.at(i) + 1 < n) {
      for (int64_t j = i + a.at(i) + 1; j < n; j++) {
        if (j - i == a.at(i) + a.at(j)) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

long long a[200005];
long long plusv[200005], subv[200005];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    plusv[i] = i + a[i];
    subv[i] = i - a[i];
  }

  // sort(plusv+1,plusv+n+1);
  sort(subv + 1, subv + n + 1);
  /*for(int i=1;i<=n;i++) {
        cout<<plusv[i]<<' ';
  }
  cout<<endl;
  for(int i=1;i<=n;i++) {
        cout<<subv[i]<<' ';
  }
  cout<<endl;*/
  // i+a[i]=j-a[j]
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += upper_bound(subv + i + 1, subv + n + 1, plusv[i]) -
           lower_bound(subv + 1, subv + n + 1, plusv[i]);
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

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

int a[100005];
int plusv[100005], subv[100005];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    plusv[i] = i + a[i];
    subv[i] = i - a[i];
  }

  sort(plusv + 1, plusv + n + 1);
  sort(subv + 1, subv + n + 1);
  /*for(int i=1;i<=n;i++) {
        cout<<plusv[i]<<' ';
  }
  cout<<endl;
  for(int i=1;i<=n;i++) {
        cout<<subv[i]<<' ';
  }
  cout<<endl;*/
  // i+a[i]=j-a[j]
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += upper_bound(subv + 1, subv + n + 1, plusv[i]) -
           lower_bound(subv + 1, subv + n + 1, plusv[i]);
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repc(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ll long long
#define vec vector
#define ft first
#define sd second
#define all(vec) vec.begin(), vec.end()
typedef pair<int, int> P;
const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  vec<int> a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  ll ans = 0;
  rep(i, n) {
    int sa = i - a[i];
    ans += mp[sa];
    int wa = a[i] + i;
    mp[wa]++;
  }
  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 < (int)(n); i++)
#define repc(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ll long long
#define vec vector
#define ft first
#define sd second
#define all(vec) vec.begin(), vec.end()
typedef pair<int, int> P;
const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  vec<ll> a(n + 1);
  vec<ll> ai(n + 1);
  repc(i, 1, n) cin >> a[i];
  repc(i, 1, n) { ai[i] = a[i] + i; }

  int ans = 0;
  repc(j, 2, n) {
    int aj = j - a[j];
    repc(i, 1, j - 1) {
      if (ai[i] == aj)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>

int main() {
  long long N;
  std::cin >> N;

  std::map<long long, long long> B_map, C_map;
  for (long long i = 0; i < N; ++i) {
    long long a;
    std::cin >> a;
    B_map[i + a] += 1;
    C_map[i - a] += 1;
  }

  unsigned long ans = 0;
  for (auto x : B_map) {
    const long long B_num = x.second;
    const long long C_num = C_map[x.first];
    ans += B_num * C_num;
  }

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>

int main() {
  long long N;
  std::cin >> N;

  std::vector<long long> B_array(N);
  std::vector<long long> C_array(N);
  for (long long i = 0; i < N; ++i) {
    long long a;
    std::cin >> a;
    B_array[i] = i + a;
    C_array[i] = i - a;
  }

  std::vector<long long> indices(N);
  std::iota(indices.begin(), indices.end(), 0);
  std::sort(indices.begin(), indices.end(),
            [&C_array](long long i1, long long i2) {
              return C_array[i1] < C_array[i2];
            });

  std::sort(C_array.begin(), C_array.end());

  unsigned long ans = 0;
  for (long long i = 0; i < N; ++i) {
    auto result =
        std::equal_range(C_array.begin(), C_array.end(), B_array.at(i));
    const long long saisyo = std::distance(C_array.begin(), result.first);
    const long long saigo = std::distance(C_array.begin(), result.second);
    for (long long j = saigo - 1; j >= saisyo; --j) {
      const long long index = indices[j];
      if (index > i) {
        ans += 1;
      } else {
        break;
      }
    }
  }

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

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

typedef long long ll;

int main() {
  ll n, x, c = 0;
  cin >> n;
  map<ll, ll> m1, m2;
  for (ll i = 1; i <= n; i++) {
    cin >> x;
    m1[i + x]++;
    m2[i - x]++;
  }
  for (auto i : m1)
    c += i.second * m2[i.first];
  cout << c << endl;
}

Error: TLE

Fixed submission:

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

typedef long long ll;

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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

ll N;
vector<ll> v;
unordered_map<ll, ll> um;

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    ll a;
    cin >> a;
    v.push_back(a - i);
    if (um.count(a - i) == 0)
      um[a - i] = 1;
    else
      um[a - i] = um[a - i] + 1;
  }
  ll count = 0;
  for (int i = 0; i < N - 1; i++) {
    ll s = 0 - i * 2 - v[i];
    if (um.count(s) == 0) {
      um[v[i]] = um[v[i]] - 1;
      continue;
    }
    if (um[s] <= 0) {
      um[v[i]] = um[v[i]] - 1;
      continue;
    }
    count += um[s];
    um[v[i]] = um[v[i]] - 1;
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int N;
int A[200000];

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int C = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      if (A[i] + A[j] == j - i)
        C++;
    }
  }
  cout << C << endl;
  return 0;
}