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

#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>*/

#define pll pair<long long, long long>
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define D(x) cerr << #x " = " << (x) << endl
#define vll vector<ll>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);
#define endl '\\n'
using namespace std;

int main() {
  IOS
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);
      // freopen("error.txt","w",stderr);
      ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  unordered_map<ll, ll> mp;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll val = i - a[i];
    ans += mp[val];
    mp[i + a[i]]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>*/

#define pll pair<long long, long long>
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define D(x) cerr << #x " = " << (x) << endl
#define vll vector<ll>
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);
#define endl '\\n'
using namespace std;

int main() {
  IOS
      // freopen("input.txt","r",stdin);
      // freopen("output.txt","w",stdout);
      // freopen("error.txt","w",stderr);
      ll n;
  cin >> n;
  ll a[n];
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  multiset<ll> st;
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll val = i - a[i];
    ans += st.count(val);
    st.insert(i + a[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i > b; i--)
#define vint vector<int>
#define vvint vector<vint>
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;

ll A[200002], B[200002], C[200002];
ll ans;
ll N;

bool isOK(ll index, ll key) {
  if (C[index] >= key)
    return true; // ここの配列名を適宜変えよう！
  else
    return false;
}

ll binary_search(ll key) {

  ll left = 0, right = N; // ここの配列名を適宜変えよう！
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;

    if (isOK(mid, key))
      right = mid;
    else
      left = mid;
  }
  return right;
}

map<int, int> mp;

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

  cin >> N;

  FOR(i, 1, N + 1) {
    cin >> A[i];
    B[i] = i - A[i];
    mp[B[i]]++;
    C[i] = A[i] + i;
  }

  FOR(i, 1, N + 1) { ans += mp[C[i]]; }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, a, b) for (int i = a; i > b; i--)
#define vint vector<int>
#define vvint vector<vint>
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;

ll A[200002], B[200002], C[200002];
ll ans;
ll N;

bool isOK(ll index, ll key) {
  if (C[index] >= key)
    return true; // ここの配列名を適宜変えよう！
  else
    return false;
}

ll binary_search(ll key) {

  ll left = 0, right = N; // ここの配列名を適宜変えよう！
  while (right - left > 1) {
    ll mid = left + (right - left) / 2;

    if (isOK(mid, key))
      right = mid;
    else
      left = mid;
  }
  return right;
}

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

  cin >> N;

  FOR(i, 1, N + 1) {
    cin >> A[i];
    B[i] = i - A[i];
    C[i] = A[i] + i;
  }
  B[0] = -1e5;
  C[0] = -1e5;
  sort(C, C + (N + 1));

  ll cur = 1;
  FOR(i, 1, N + 1) {
    ll cur = binary_search(B[i]);
    while (B[i] == C[cur]) {
      ans++;
      if (cur != N)
        cur++;
      else
        break;
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  map<long long int, long long int> m;
  long long int n;
  cin >> n;
  long long int c = 0;
  vector<long long int> a(n + 1);
  for (long long int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[0] = 0;
  for (long long int i = 1; i <= n; i++) {
    if (m.count(i - a[i]) > 0) {
      c += m[i - a[i]];
    }
    m[i + a[i]]++;
  }
  cout << c;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  map<long long int, long long int> m;
  long long int n;
  cin >> n;
  long long int c = 0;
  vector<long long int> a(n);
  for (long long int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[0] = 0;
  for (long long int i = 1; i <= n; i++) {
    if (m.count(i - a[i]) > 0) {
      c += m[i - a[i]];
    }
    m[i + a[i]]++;
  }
  cout << c;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 5;
map<int, int> mp;
int a[maxn];
int n;
ll ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    if (i - a[i] > 0)
      mp[i - a[i]]++;
  for (int i = 1; i <= n; i++)
    if (mp[i + a[i]])
      ans += mp[i + a[i]];
  cout << ans;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5 + 5;
map<int, int> mp;
int a[maxn];
int n;
ll ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    if (i - a[i] > 0)
      mp[i - a[i]]++;
  for (int i = 1; i <= n; i++)
    if (mp[i + a[i]])
      ans += mp[i + a[i]];
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  long long int n, i, j, ans = 0, x;
  cin >> n;
  vector<int> vec(n, 0);
  for (i = 0; i < n; i++) {
    cin >> x;
    if (i - x >= 0)
      ans += vec[i - x];
    if (i + x < n)
      vec[i + x]++;
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
void A(void);
void B(void);
void C(void);
void D(void);
void E(void);
void F(void);

int main(void) { E(); }

void F() {
  int n, a, b, c, i;
  cin >> n >> a >> b >> c;
  vector<string> vec(n);
  string s;
  for (i = 0; i < n; i++) {
    cin >> s;
    if (s == "AB") {
      if (a == 0 && b == 0) {
        cout << "No" << endl;
        break;
      } else if (a < b) {
        a++;
        b--;
        vec.at(i) = "A";
      } else if (a > b) {
        a--;
        b++;
        vec.at(i) = "B";
      }
    }
    if (s == "BC") {
      if (c == 0 && b == 0) {
        cout << "No" << endl;
        break;
      } else if (c < b) {
        c++;
        b--;
        vec.at(i) = "C";
      } else if (c > b) {
        c--;
        b++;
        vec.at(i) = "B";
      }
    }
    if (s == "AC") {
      if (a == 0 && c == 0) {
        cout << "No" << endl;
        break;
      } else if (c < a) {
        c++;
        a--;
        vec.at(i) = "C";
      } else if (c > a) {
        c--;
        a++;
        vec.at(i) = "A";
      }
    }
  }
  if (i == n) {
    cout << "Yes" << endl;
    for (i = 0; i < n; i++) {
      cout << vec.at(i) << endl;
    }
  }
}

void D() {
  long long int x, f, i, j;
  int k = 0;
  cin >> x;
  for (i = -200; i <= 200; i++) {
    for (j = -200; j <= 200; j++) {
      f = i * i * i * i * i - j * j * j * j * j;
      if (f == x) {
        cout << i << " " << j << endl;
        k = 1;
        break;
      }
    }
    if (k == 1)
      break;
  }
}

void E() {
  int n, i, j, ans = 0, x;
  cin >> n;
  vector<int> vec;
  for (i = 0; i < n; i++) {
    cin >> x;
    if (i >= 1) {
      for (j = 0; vec.at(j) <= (i - x); j++) {
        if ((i - x) == vec[j])
          ans++;
      }
    }
    vec.push_back(i + x);
    sort(vec.begin(), vec.end());
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
2020-05-03
atcoder ABC
*/

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

const ll mod = 1000000007;
int main() {
  int N;
  ll ans = 0;
  cin >> N;
  map<ll, int> L_map;

  rep(i, N) {
    ll A;
    cin >> A;
    L_map[i + A]++;
    ans += L_map[i - A];
  }

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


Error: TLE

Fixed submission:

/*
2020-05-03
atcoder ABC
*/

#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

const ll mod = 1000000007;
int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int N;
  ll ans = 0;
  cin >> N;
  vector<ll> data(N);
  rep(i, N) {
    ll A;
    cin >> A;
    data[i] = A - i;
  }

  rep(i, N) {
    for (int j = i; j < N; j++) {
      if ((data[i] + data[j] + 2 * i) == 0) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

ll N;
ll A[200010];
map<ll, ll> mp;

int main() {
  cin >> N;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i] - i]++;
  }

  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    ll num = -A[i] - i;
    mp[A[i] - i]--;
    ans += mp[num];
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

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

typedef long long ll;

ll N;
ll A[100010];
map<ll, ll> mp;

int main() {
  cin >> N;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i] - i]++;
  }

  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    ll num = -A[i] - i;
    mp[A[i] - i]--;
    ans += mp[num];
  }

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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define mt(a, b, c) make_tuple((a), (b), (c))

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  long long int ans = 0;
  mp[a[0] + 1] = 1;
  for (int i = 1; i < n; i++) {
    int is = a[i] + i + 1;
    int rd = i + 1 - a[i];
    if (mp.count(rd) == 0)
      ans = ans;
    else
      ans = ans + mp[rd];

    if (mp.count(is) == 0)
      mp[is] = 1;
    else
      mp[is] = mp[is] + 1;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define pb(e) push_back(e)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define mt(a, b, c) make_tuple((a), (b), (c))

typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  multiset<int> ms;
  long long int ans = 0;
  ms.insert(a[0] + 1);
  for (int i = 1; i < n; i++) {
    int is = a[i] + i + 1;
    int rd = i + 1 - a[i];
    ans = ans + ms.count(rd);
    ms.insert(is);
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[]) {
  ll n, i, j;
  cin >> n;
  vector<ll> a(n);
  ll answer = 0;
  map<ll, ll> mpi, mpj;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (mpi.find(i + a[i]) == mpi.end())
      mpi[i + a[i]] = 1;
    else
      mpi[i + a[i]]++;
    if (mpj.find(i - a[i]) == mpj.end())
      mpj[i - a[i]] = 1;
    else
      mpj[i - a[i]]++;
  }

  for (auto itr = mpi.begin(); itr != mpi.end(); itr++) {
    if (mpj.find(itr->first) != mpj.end())
      answer += itr->second * mpj[itr->first];
  }

  cout << answer << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;

int main(int argc, char *argv[]) {
  int n, i, j;
  cin >> n;
  vector<ll> a(n);
  ll answer = 0;
  for (i = 0; i < n; i++)
    cin >> a[i];

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

  cout << answer << endl;

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

#include <algorithm>
#include <iostream>
using namespace std;
int n, pp, sign, a[200005], g, h;
long long ans = 0;
struct tt {
  int id, num;
} b[200005];
bool pd(tt x, tt y) {
  if ((x.num < y.num) || (x.num == y.num && x.id < y.id))
    return true;
  return false;
}
int search1(int t, int i, int j) {
  int mid = (i + j) / 2;
  if (j < i)
    return -1;
  if (b[mid].num == t && b[mid - 1].num != t)
    return mid;
  if ((b[mid].num > t) || (b[mid].num == t && b[mid - 1].num == t))
    return search1(t, i, mid - 1);
  if (b[mid].num < t)
    return search1(t, mid + 1, j);
}

int search2(int t, int i, int j) {
  int mid = (i + j) / 2;
  if (j < i)
    return -1;
  if (b[mid].num == t && b[mid + 1].num != t)
    return mid;
  if (b[mid].num > t)
    return search2(t, i, mid - 1);
  if ((b[mid].num < t) || (b[mid].num == t && b[mid + 1].num == t))
    return search2(t, mid + 1, j);
}

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i].num = a[i] + i;
    b[i].id = i;
  }
  b[0].num = -1;
  b[0].id = 0;
  b[n + 1].num = -1;
  b[n + 1].id = 0;
  sort(b, b + n, pd);
  for (int i = 2; i <= n; i++) {
    pp = i - a[i];
    g = search1(pp, 1, n);
    if (g != -1) {
      h = search2(pp, 1, n);
      while ((b[h].id > i) && h > 0)
        h--;
      if (h < g)
        continue;
      ans += h - g + 1;
    }
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n, ans = 0, pp, sign, a[200005], g, h;
struct tt {
  int id, num;
} b[200005];
bool pd(tt x, tt y) {
  if (x.num < y.num)
    return true;
  return false;
}
int search(int t, int i, int j) {
  int mid = (i + j) / 2;
  if (j < i)
    return -1;
  if (b[mid].num == t)
    return mid;
  if (b[mid].num > t)
    return search(t, i, mid - 1);
  if (b[mid].num < t)
    return search(t, mid + 1, j);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i].num = a[i] + i;
    b[i].id = i;
  }
  sort(b, b + n, pd);
  for (int i = 1; i <= n; i++) {
    pp = i - a[i];
    sign = search(pp, 1, n);
    if (sign != -1) {
      g = sign;
      h = sign - 1;
      while (b[g].num == pp && g <= n) {
        if (b[g].id <= i)
          ans++;
        g++;
      }
      while (b[h].num == pp && h > 0) {
        if (b[h].id <= i)
          ans++;
        h--;
      }
    }
  }
  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 endl "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const int MOD = 1e9 + 7;
typedef unsigned long long u64;

int main() {
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int diff[n + 1];
    unordered_map<long, long> mp;
    for (int i = 1; i <= n; i++) {
      int x;
      cin >> x;
      diff[i] = i - x;
      mp[i + x] += 1;
    }
    ll ct = 0;
    for (int i = 1; i <= n; i++) {
      if (diff[i] > 0) {
        ct += mp[diff[i]];
      }
    }
    cout << ct << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

#define ll long long int
#define endl "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

const int MOD = 1e9 + 7;
typedef unsigned long long u64;

int main() {
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int ht[n];
    for (int i = 0; i < n; i++)
      cin >> ht[i];
    ll ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (abs(j - i) == ht[i] + ht[j])
          ans += 1;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int64;
int P[400000];
int M[200000];

int main() {
  memset(P, 0, sizeof(P));
  memset(P, 0, sizeof(M));
  int N;
  cin >> N;
  int tmp;
  int64 ans = 0;
  for (int i = 0; i < N; ++i) {
    cin >> tmp;
    if (tmp < 200000) {
      ++P[i + tmp];
      if (i - tmp > 0)
        ++M[i - tmp];
    }
  }
  for (int i = 1; i < N; ++i) {
    ans += (int64)P[i] * M[i];
  }
  cout << ans << "\\n";
  return 0;
}


Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int64;
int P[200000];
int M[200000];

int main() {
  memset(P, 0, sizeof(P));
  memset(P, 0, sizeof(M));
  int N;
  cin >> N;
  int tmp;
  int64 ans = 0;
  for (int i = 0; i < N; ++i) {
    cin >> tmp;
    if (tmp < 200000) {
      ++P[i + tmp];
      if (i - tmp > 0)
        ++M[i - tmp];
    }
  }
  for (int i = 1; i < N; ++i) {
    ans += (int64)P[i] * M[i];
  }
  cout << ans << "\\n";
  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

int main() {
  ll n;
  cin >> n;
  vector<ll> arr(n);
  map<ll, ll> mp;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    mp[arr[i] + i]++;
  }
  ll ans = 0;
  for (int j = 0; j < n; j++) {
    ans += mp[j - arr[j]];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

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

  //	multiset<long long> st;
  unordered_map<long long, int> mp;

  long long cnt = 0;

  for (int i = 0; i < n; i++) {
    long long key = (a[i] * (-1) + i);
    // cnt+=(upper_bound(st.begin(),st.end(),key)-lower_bound(st.begin(),st.end(),key));
    // cnt+=st.count(key);
    // st.insert(a[i]+i);
    cnt += mp[key];
    mp[a[i] + i] += 1;
  }

  cout << cnt << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;

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

  multiset<long long> st;

  long long cnt = 0;

  for (int i = 0; i < n; i++) {
    cnt += st.count(a[i] * (-1) + i);
    st.insert(a[i] + i);
  }

  cout << cnt << endl;
}