alexjercan/bugnet huggingface dataset:
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, (b), sizeof(a))
#define fore(i, a) for (int i = 0; i < a; i++)
#define fore1(i, j, a) for (int i = j; i < a; i++)
#define print(ar)                                                              \\
  for (int i = 0; i < ar.size(); i++)                                          \\
    cout << ar[i] << " ";
#define END cout << '\\n'
const double pi = acos(-1.0);

typedef pair<int, int> PII;

typedef vector<long long> VI;

typedef vector<string> VS;

typedef vector<PII> VII;

typedef vector<VI> VVI;

typedef map<int, int> MPII;

typedef set<int> SETI;

typedef multiset<int> MSETI;

typedef long int li;

typedef unsigned long int uli;

typedef long long int ll;

typedef unsigned long long int ull;
ll fastexp(ll a, ll b, ll n) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % n;
    a = a * a % n;
    b >>= 1;
  }
  return res;
}
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
// VI vect[2020202];
int main() {
  fast();
  int t, n, k;

  cin >> n;
  VI arr(n, 0);
  map<ll, ll> mp;
  fore(i, n) {
    cin >> arr[i];
    mp[i + 1 - arr[i]]++;
  }
  // vector<vector<ll>> vect(n,)
  // cout<<n<<endl;
  ll cnt = 0;
  fore(i, n) { cnt += mp[arr[i] + i + 1]; }

  cout << cnt << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define mem(a, b) memset(a, (b), sizeof(a))
#define fore(i, a) for (int i = 0; i < a; i++)
#define fore1(i, j, a) for (int i = j; i < a; i++)
#define print(ar)                                                              \\
  for (int i = 0; i < ar.size(); i++)                                          \\
    cout << ar[i] << " ";
#define END cout << '\\n'
const double pi = acos(-1.0);

typedef pair<int, int> PII;

typedef vector<long long> VI;

typedef vector<string> VS;

typedef vector<PII> VII;

typedef vector<VI> VVI;

typedef map<int, int> MPII;

typedef set<int> SETI;

typedef multiset<int> MSETI;

typedef long int li;

typedef unsigned long int uli;

typedef long long int ll;

typedef unsigned long long int ull;
ll fastexp(ll a, ll b, ll n) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = res * a % n;
    a = a * a % n;
    b >>= 1;
  }
  return res;
}
void fast() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
// VI vect[2020202];
int main() {
  fast();
  int t, n, k;

  cin >> n;
  VI arr(n, 0);
  fore(i, n) cin >> arr[i];
  // vector<vector<ll>> vect(n,)
  // cout<<n<<endl;
  int count = 0;
  int vect[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      vect[i][j] = arr[i] + arr[j];
      // cout<<vect[i][j]<<" ";
      if (vect[i][j] == j - i)
        count++;
    }
    // cout<<endl;
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
#pragma GCC optimize(2)
// #define int long long
using namespace std;
template <typename T> void read(T &x) {
  x = 0;
  char ch = getchar();
  int fh = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= fh;
}
template <typename T> void write(T x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
  write(x);
  puts("");
}
int n;
int a[200005];
unordered_map<int, vector<int>> mp;
signed main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  read(n);
  for (register int i = 1; i <= n; i++)
    read(a[i]);
  for (register int i = 1; i <= n; i++)
    mp[i - a[i]].push_back(i);
  long long ans = 0;
  for (register int i = 1; i <= n; i++) {
    vector<int> v = mp[i + a[i]];
    int id = upper_bound(v.begin(), v.end(), i) - v.begin();
    ans += (long long)(v.size() - id);
  }
  writeln(ans);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define inf 1000000007
#define mod 1000000007
#pragma GCC optimize(2)
#define int long long
using namespace std;
template <typename T> void read(T &x) {
  x = 0;
  char ch = getchar();
  int fh = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      fh = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  x *= fh;
}
template <typename T> void write(T x) {
  if (x < 0)
    x = -x, putchar('-');
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> void writeln(T x) {
  write(x);
  puts("");
}
int n;
int a[200005];
unordered_map<int, vector<int>> mp;
signed main() {
  // freopen(".in","r",stdin);
  // freopen(".out","w",stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  read(n);
  for (register int i = 1; i <= n; i++)
    read(a[i]);
  for (register int i = 1; i <= n; i++)
    mp[i - a[i]].push_back(i);
  int ans = 0;
  for (register int i = 1; i <= n; i++) {
    vector<int> v = mp[i + a[i]];
    int id = upper_bound(v.begin(), v.end(), i) - v.begin();
    ans += v.size() - id;
  }
  writeln(ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;

int main() {
  int n;
  cin >> n;
  map<int, int> mp, mm, m;
  rep(i, n) {
    int a;
    cin >> a;
    mp[i - a]++;
    mm[i + a]++;
  }
  ll ans = 0;
  for (auto x : mp) {
    for (auto y : mm) {
      if (x.first == y.first) {
        ans += x.second * y.second;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, d, n) for (int i = (d); i < (n); ++i)
#define all(v) v.begin(), v.end()
using P = pair<int, int>;

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  map<int, int> L;
  map<int, int> R;
  rep(i, n) {
    int l = i - a[i];
    int r = i + a[i];
    L[l]++;
    R[r]++;
  }

  int ans = 0;
  for (auto m : L) {
    ans += m.second * R[m.first];
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, d, n) for (int i = (d); i < (n); ++i)
#define all(v) v.begin(), v.end()
using P = pair<int, int>;

signed main() {

  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }

  int ans = 0;
  rep(i, n - 1) {
    int x = a[i];
    REP(j, i + x, n) {
      if (abs(i - j) == (a[i] + a[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 int ll;

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


Error: TLE

Fixed submission:

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

int main() {
  ll n, a, count = 0;
  cin >> n;
  multiset<ll> st;
  for (int i = 0; i < n; i++) {
    cin >> a;
    count += distance(st.lower_bound(i - a), st.upper_bound(i - a));
    st.insert(a + i);
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int N = 2e5 + 500;

int n;
int a[N];
map<ll, ll> mp;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  mp[a[0]]++;
  ll ans = 0;
  for (int i = 1; i < n; ++i) {
    ll x = i - a[i];
    ans += mp[x];
    mp[a[i] + i]++;
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long

using namespace std;

const int N = 2e5 + 500;

int n;
int a[N];
multiset<ll> s;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  s.insert(a[0]);
  ll ans = 0;
  for (int i = 1; i < n; ++i) {
    ll x = i - a[i];
    ans += s.count(x);
    s.insert(a[i] + i);
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5, M = 1e9 + 7, OO = 0x3f3f3f3f;
typedef long long ll;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const double Pii = 3.14159265359;
ll n;
ll a[N], d[N], fr[N];

int main() {
  FIO cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    d[i] = i - a[i];
  }
  for (int i = 0; i < n; ++i)
    if (d[i] < N && d[i] >= 0)
      fr[d[i]]++;
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int didx = a[i] + i;
    if (didx >= 0 && didx < N)
      ans += fr[didx];
    // cout << didx << " " << fr[didx] << endl;
  }
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5, M = 1e9 + 7, OO = 0x3f3f3f3f;
typedef long long ll;
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
const double Pii = 3.14159265359;
ll n;
ll a[N], d[N], fr[N];

int main() {
  FIO cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    d[i] = i - a[i];
  }
  for (int i = 0; i < n; ++i)
    if (d[i] < N && d[i] >= 0)
      fr[d[i]]++;
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int didx = a[i] + i;
    ans += fr[didx];
    // cout << didx << " " << fr[didx] << 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 (int i = 0; i < n; i++)
#define numrep(i, n) for (int i = 1; i <= n; i++)
#define bitrep(n) for (int bit = 0; bit < (1 << n); bit++)
#define ll int64_t
#define all(v) v.begin(), v.end()

const int MOD = 1000000007;

int main() {

  ll N;
  cin >> N;

  vector<ll> A(N);

  rep(i, N) cin >> A[i];

  map<ll, ll> L, R;

  rep(i, N) {
    ll x = i + A[i] + 1, y = i - A[i] + 1; // i+A(i),j-A(j)
    if (L[x] == 0)
      L[x] = 1;
    else
      L[x]++;

    if (R[y] == 0)
      R[y] = 1;
    else
      R[y]++;
  }

  ll ans = 0;

  for (auto p : R) {
    ll key = p.first;
    ll value = p.second;

    ans += value * L[key];
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define numrep(i, n) for (int i = 1; i <= n; i++)
#define bitrep(n) for (int bit = 0; bit < (1 << n); bit++)
#define ll int64_t
#define all(v) v.begin(), v.end()

const int MOD = 1000000007;

int main() {

  int N;
  cin >> N;

  vector<int> A(N);

  rep(i, N) cin >> A[i];

  ll ans = 0;

  for (int i = 1; i < N; i++) {
    for (int j = 0; j + i < N; j++) {
      if (A[j] + A[j + i] == i)
        ans++;
    }
  }

  cout << ans << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
#define PI 3.14159265359

int main() {
  ll N, A;
  cin >> N;
  map<ll, ll> count;
  ll ret = 0;
  for (auto i = 0; i < N; i++) {
    cin >> A;
    auto it = count.find(i - A);
    if (it != count.end()) {
      ret += it->second;
    }
    it = count.find(i + A);
    if (it != count.end()) {
      it->second++;
    } else {
      count[i + A] = 1;
    }
  }
  cout << ret << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
#define PI 3.14159265359

int main() {
  ll N, A;
  cin >> N;
  vector<ll> vec;
  vec.reserve(N);
  ll ret = 0;
  for (auto i = 0; i < N; i++) {
    cin >> A;
    for (auto k = 0; k < i; k++) {
      if (i - k == vec[k] + A) {
        ret++;
      }
    }
    vec.push_back(A);
  }
  cout << ret << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; }

std::istream &operator>>(std::istream &in, set<string> &o) {
  string a;
  in >> a;
  o.insert(a);
  return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
  ll a;
  in >> a;
  o.push(a);
  return in;
}

typedef pair<ll, ll> P;

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);
// const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;

int main() {

  int n;
  cin >> n;

  vector<ll> numbers(n);
  rep(i, n) cin >> numbers[i];

  multiset<ll> s;
  map<ll, int> m;
  rep(i, n) s.insert(numbers[i] - i);
  rep(i, n) m[numbers[i] - i]++;
  ll ans = 0;
  rep(i, n) {

    ll t = -(numbers[i] + i);
    ll now = m[t];
    ans += now;

    auto it = s.find(numbers[i] - i);
    s.erase(it);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const double PI = 3.14159265358979323846;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
// #define rep(i, n) for (ll i = 0; i < (n); ++i)
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

double equal(double a, double b) { return fabs(a - b) < DBL_EPSILON; }

std::istream &operator>>(std::istream &in, set<string> &o) {
  string a;
  in >> a;
  o.insert(a);
  return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
  ll a;
  in >> a;
  o.push(a);
  return in;
}

typedef pair<ll, ll> P;

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

// ofstream outfile("log.txt");
// outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0')
// << rank << endl;
//  std::cout << std::bitset<8>(9);
// const ll mod = 1e10;

typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;

int main() {

  int n;
  cin >> n;

  vector<ll> numbers(n);
  rep(i, n) cin >> numbers[i];

  multiset<ll> s;
  rep(i, n) s.insert(numbers[i] - i);
  ll ans = 0;
  rep(i, n) {

    ll t = -(numbers[i] + i);
    ll now = s.count(t);
    ans += now;

    auto it = s.find(numbers[i] - i);
    s.erase(it);
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, s, n) for (int i = s; i < n; i++)
#define fi first
#define se second
#define all(c) c.begin(), c.end()
typedef long long int ll;
typedef pair<int, int> P;
typedef long double ld;
void Ans(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

const ll INF = 1001001001;
const ll mod = 1000000007;

int main() {
  ll n;
  cin >> n;
  ll a[n];
  map<ll, ll> b, c;
  rep(i, n) {
    cin >> a[i];
    b[a[i] + i]++;
    if (i - a[i] >= 0)
      c[i - a[i]]++;
  }
  ll ans = 0;
  for (auto x : b) {
    ans += x.se * c[x.fi];
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, s, n) for (int i = s; i < n; i++)
#define fi first
#define se second
#define all(c) c.begin(), c.end()
typedef long long int ll;
typedef pair<int, int> P;
typedef long double ld;
void Ans(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

const ll INF = 1001001001;
const ll mod = 1000000007;

int main() {
  int n;
  cin >> n;
  ll a[n];
  int b[200005], c[200005];
  rep(i, 200005) {
    b[i] = 0;
    c[i] = 0;
  }
  rep(i, n) {
    cin >> a[i];
    b[a[i] + i]++;
    if (i - a[i] >= 0)
      c[i - a[i]]++;
  }
  int ans = 0;
  rep(i, 200005) { ans += b[i] * c[i]; }
  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 < (int)(n); i++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

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


Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vi a(n + 1);
  rep(i, n) cin >> a[i + 1];
  int ans = 0;
  for (int i = 1; i <= n - 1; i++) {
    for (int j = i + a[i]; j <= n; j++) {
      if (j - i == a[i] + a[j])
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sc(x) scanf("%lld", &x);
#define int long long
#define fi first
#define se second
#define endl '\\n'
#define P pair<int, int>
#define fi first
#define se second

int A[200004];
int B[200005];
map<int, int> mp;
signed main() {
  int n;
  sc(n) for (int i = 1; i <= n; i++) {
    sc(A[i]) B[i] = A[i] - i;
    mp[B[i]]++;
  }
  int a = 0;

  for (int i = 1; i <= n; i++) {
    a += mp[-(A[i] + i)];
  }
  cout << a;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sc(x) scanf("%lld", &x);
#define int long long
#define fi first
#define se second
#define endl '\\n'
#define P pair<int, int>
#define fi first
#define se second

int A[100004];
int B[100005];
map<int, int> mp;
signed main() {
  int n;
  sc(n) for (int i = 1; i <= n; i++) {
    sc(A[i]) B[i] = A[i] - i;
    mp[B[i]]++;
  }
  int a = 0;

  for (int i = 1; i <= n; i++) {
    a += mp[-(A[i] + i)];
  }
  cout << a;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  // 入力
  int n;
  cin >> n;
  long long a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  // Ai+Aj=i-jより、i-Ai=j+Aj。左辺と右辺それぞれの値を全ての参加者についてもとめその値をkeyとした連想配列で求める

  // 和と差を保管する連想配列
  map<long long, long long> plus;
  map<long long, long long> minus;

  for (int i = 0; i < n; i++) {
    plus[i + 1 + a[i]] += 1;
    minus[i + 1 - a[i]] += 1;
  }

  // ペアの数カウント変数
  long long count = 0;

  // i-Ai=j+Ajが成り立つ時の両辺の値の範囲は大体0〜nである
  for (int i = 0; i <= n; i++) {
    count += plus[i] * minus[i];
  }

  cout << count << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  long a[n];
  long long count = 0;

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

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

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

#include <bits/stdc++.h>
const int M = 1e9 + 7; // M=1e9+7;//M=300000;//
const int N = 2e6 + 5;
typedef long long int ll;
using namespace std;

ll cnt(0), sum(0), n, x, y, t, i, j, temp, k, ans(0), mn(M), mx(-9e9);
map<ll, ll> m;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  ll a[n + 1];
  for (i = 0; i < n; i++) {
    cin >> x;
    ans += m[i - x];
    m[i + x]++;
  }

  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
const int M = 1e9 + 7; // M=1e9+7;//M=300000;//
const int N = 2e6 + 5;
typedef long long int ll;
using namespace std;

ll cnt(0), sum(0), n, m, x, y, t, i, j, temp, k, ans(0), mn(M), mx(-9e9);

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

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

#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  // j - i = a[i] + a[j]
  // => i + a[i] = j - a[j]
  map<ll, ll> l, r;
  rep(i, n) {
    l[i + a[i]]++;
    r[i - a[i]]++;
  }
  ll res = 0;
  for (auto ll : l) {
    res += ll.second * r[ll.first];
  }
  cout << res << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int n;
  cin >> n;
  vector<P> a(n);
  rep(i, n) {
    int aa;
    cin >> aa;
    a[i] = make_pair(aa, i);
  }
  sort(a.begin(), a.end()); // 昇順
  ll res = 0;
  rep(i, a.size()) {
    int j = i + 1;
    while (j < n && a[i].first + a[j].first <= n) {
      if (a[i].first + a[j].first == abs(a[i].second - a[j].second)) {
        res++;
        //        cout << i << ", " << j << endl;
      }
      j++;
    }
  }
  cout << res << endl;
  return 0;
}