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

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

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define ALL(a) (a).begin(), (a).end()
// constant
//--------------------------------------------
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

const int MAX_N = 2e5 + 3;

const unsigned long long B = 1e12 + 7;

int main() {
  int N;
  cin >> N;
  vector<int> Ai, Aj;
  REP(i, N) {
    int a;
    cin >> a;
    Ai.PB(a + i);
    Aj.PB(i - a);
  }
  SORT(Aj);
  LL res = 0;
  REP(n, N) {
    LL cnt = upper_bound(Aj.begin(), Aj.end(), Ai[n]) -
             lower_bound(Aj.begin(), Aj.end(), Ai[n]);
    res += cnt;
  }
  cout << res << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

// container util
//------------------------------------------
#define PB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
// repetition
//------------------------------------------
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())
#define ALL(a) (a).begin(), (a).end()
// constant
//--------------------------------------------
// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

const int MAX_N = 2e5 + 3;

const unsigned long long B = 1e12 + 7;

int main() {
  int N;
  cin >> N;
  vector<PLL> A;
  REP(i, N) {
    int a;
    cin >> a;
    A.PB(MP(a, i));
  }
  LL res = 0;
  for (LL n = 2; n < N; n++) {
    for (int i = 0; i < N - n; i++) {
      if (A[i].first + A[i + n].first == n) {
        res++;
      }
    }
  }
  cout << res << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>

#define int long long
#define rr register

#define MAXN 1000010
#define inf 1e18

using namespace std;

inline int read() {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + (ch ^ 48);
    ch = getchar();
  }
  return f ? -s : s;
}
int n, ans;

int a[MAXN];

map<int, int> v, m;

signed main() {
  n = read();
  for (rr int i = 1; i <= n; i++) {
    a[i] = read();
    m[i + a[i]]++;
    // cout << i + a[i] << "\\n";
  }
  for (rr int i = 1; i <= n; i++) {
    ans += m[i - a[i]];
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>

#define int long long
#define rr register

#define MAXN 1000010
#define inf 1e18

using namespace std;

inline int read() {
  int s = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + (ch ^ 48);
    ch = getchar();
  }
  return f ? -s : s;
}

// int x;

// signed main() {
//     x = read();
//     for (rr int i = -100; i <= 100;i++)
//         for (rr int j = -100; j <= 100; j++)
//             if (i * i * i * i * i - j * j * j * j * j == x) {
//                 cout << i << " " << j << "\\n";
//                 return 0;
//             }
// }
int n, ans;

int a[MAXN];

signed main() {
  n = read();
  for (rr int i = 1; i <= n; i++) {
    a[i] = read();
    for (rr int j = 1; j < i; j++)
      if ((i - j) == (a[i] + a[j]))
        ans++;
  }
  cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N), v(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    v[i] = i + 1 - A[i];
  }
  ll cnt = 0;
  sort(v.begin(), v.end());
  for (int i = 0; i < N; i++) {
    auto th1 = lower_bound(v.begin(), v.end(), A[i] + (i + 1));
    auto th2 = upper_bound(v.begin(), v.end(), A[i] + (i + 1));
    cnt += th2 - th1;
    // cout << *th << endl;
    // for (auto iter = th;  iter!= v.end(); ++iter)
    //{
    //     if(*iter==A[i]+(i+1)) cnt++;
    //     else break;
    // }
  }
  cout << cnt << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N), v(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    v[i] = i + 1 - A[i];
  }
  ll cnt = 0;
  sort(v.begin(), v.end());
  for (int i = 0; i < N; i++) {
    auto th = lower_bound(v.begin(), v.end(), A[i] + (i + 1));
    // cout << *th << endl;
    for (auto iter = th; iter != v.end(); ++iter) {
      if (*iter == A[i] + (i + 1))
        cnt++;
      else
        break;
    }
  }
  cout << cnt << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N = 2e5 + 20;

int n, a[N];
long long ans;
map<int, int> m;

int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    ans += m[a[i] - i];
    m[-a[i] - i]++;
  }

  cout << ans;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> heights(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> heights[i];
  }
  int count = 0;
  int a;
  for (int i = 1; i < n; i++) {
    a = heights[i];

    for (int j = i + 1; j <= n; j++) {
      if (heights[j] >= j || a >= j) {
        continue;
      }
      if (heights[j] + heights[i] == j - i) {
        count++;
      }
    }
  }
  cout << count;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded


// E - This Message Will Self-Destruct in 5s

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

int A[200000];
unordered_map<int, int> cnt;

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

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

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

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:


//

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// const int INF = 2147483647;
// const ll INF = 9223372036854775807;
// const ll MOD = 1e9 + 7;

int A[200000];
int height[200000];

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

  set<int> height_set;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    height_set.insert(A[i]);
  }

  int i = 0;
  for (auto itr = height_set.begin(); itr != height_set.end(); itr++) {
    // cout << "  " << *itr << endl; // **** debug ****
    height[i] = *itr;
    i++;
  }

  unordered_set<int> sum_set;
  for (int i = 0; i < height_set.size(); i++) {
    for (int j = i; j < height_set.size(); j++) {
      sum_set.insert(height[i] + height[j]);
    }
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
      int diff = j - i;
      if (sum_set.find(diff) == sum_set.end())
        continue;

      if (A[i] + A[j] == diff)
        ans++;
    }
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>

#define endl '\\n'
#define fi first
#define se second
#define pb push_back
#define debug(x) cout << "[debug] " << #x << ": " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
const int MOD = 1e9 + 7;
const int maxn = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1.0);

void header() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll a[maxn], b[maxn];

void solve() {
  ll n, res = 0;
  cin >> n;
  map<ll, ll> mp;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[i] = a[i] - i;
  }

  for (int i = n; i >= 1; i--)
    mp[-(a[i] + i)]++;

  for (int i = 1; i <= n; i++)
    res += mp[b[i]];

  cout << res << endl;
}

int main() {
  header();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define endl '\\n'
#define fi first
#define se second
#define pb push_back
#define debug(x) cout << "[debug] " << #x << ": " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
const int MOD = 1e9 + 7;
const int maxn = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1.0);

void header() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll a[maxn];

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

  ll res = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] >= n)
      continue;
    for (int j = max(a[i], 1ll * i) + 1; j <= n; j++) {
      if (a[i] + a[j] == j - i)
        res++;
    }
  }
  cout << res << endl;
}

int main() {
  header();
  solve();
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  scanf("%d", &N);

  vector<long long> plus(N), minus(N);
  for (int i = 0; i < N; i++) {
    long long A;
    scanf("%lld", &A);
    plus[i] = i + 1 + A;
    minus[i] = i + 1 - A;
  }
  sort(plus.begin(), plus.end());
  sort(minus.begin(), minus.end());
  map<int, long long> minus_map;
  for (int i = 0; i < N; i++) {
    minus_map[minus[i]]++;
  }

  long long p = 0;

  for (int i = 0; i < N; i++) {
    p += minus_map[plus[i]];
  }

  cout << p << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  vector<int> plus(N), minus(N);
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    plus[i] = i + 1 + A;
    minus[i] = i + 1 - A;
  }
  sort(plus.begin(), plus.end());
  sort(minus.begin(), minus.end());
  multiset<long long> minus_set;
  for (int i = 0; i < N; i++) {
    minus_set.insert(minus[i]);
  }

  long long p = 0;

  for (int i = 0; i < N; i++) {
    p += minus_set.count(plus[i]);
  }

  cout << p << 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;
  cin >> n;
  vector<ll> a(n);
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }

  map<ll, ll> m1, m2;
  for (ll i = 1; i <= n; i++) {
    m1[i - a[i - 1]]++;
    m2[i + a[i - 1]]++;
  }

  ll count = 0;
  for (map<ll, ll>::iterator it = m1.begin(); it != m1.end(); it++) {
    count += it->second * m2[it->first];
  }

  cout << count;

  return 0;
}

Error: TLE

Fixed submission:

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

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

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

  cout << count;

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

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

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  ll N;
  cin >> N;
  vector<ll> pl(N);
  vector<ll> mi(N);
  for (ll i = 0LL; i < N; i++) {
    ll a;
    cin >> a;
    pl[i] = a + i + 1LL;
    mi[i] = i + 1LL - a;
  }
  sort(pl.begin(), pl.end());
  sort(mi.begin(), mi.end());
  vector<vector<ll>> po, ne;
  po.push_back({pl[0], 1LL});
  ne.push_back({mi[0], 1LL});
  ll k = 0LL;
  ll l = 0LL;
  for (ll i = 0LL; i < N - 1; i++) {
    if (pl[i] == pl[i + 1]) {
      po[k][1]++;
    }
    if (mi[i] == mi[i + 1]) {
      ne[l][1]++;
    }
    if (pl[i] != pl[i + 1]) {
      po.push_back({pl[i + 1], 1LL});
      k++;
    }
    if (mi[i] != mi[i + 1]) {
      ne.push_back({mi[i + 1], 1LL});
      l++;
    }
  }
  ll ans = 0LL;
  vector<vector<ll>> plmi;
  for (ll i = 0LL; i < (ll)po.size(); i++) {
    plmi.push_back(po[i]);
  }
  for (ll i = 0LL; i < (ll)ne.size(); i++) {
    plmi.push_back(ne[i]);
  }
  sort(plmi.begin(), plmi.end());
  for (ll i = 0LL; i < (ll)plmi.size() - 1; i++) {
    if (plmi[i][0] == plmi[i + 1][0]) {
      ans += plmi[i][1] * plmi[i + 1][1];
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

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

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

int main(void) {
  ll n;
  cin >> n;
  ll a[200000] = {0};

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  // Ai + Aj == j - i (j>i) <=> Ai + i == j - Aj
  // Li = Ai + i, Ri = i - Ai
  // Li == X && Rj == X
  map<ll, ll> L;
  map<ll, ll> R;
  ll ans = 0;

  for (ll i = 0; i < n; i++) {
    L[a[i] + i]++;
    R[i - a[i]]++;
  }

  for (ll i = 0; i < n; i++) {
    // cout << L[i] << " " << R[i] << endl;
    // cout << L[a[i] + i] << " " << R[i - a[i]] << endl;
    ans += L[i] * R[i];
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  ll a[200000] = {0};

  for (ll i = 0; i < n; i++) {
    cin >> a[i];
  }
  // Ai + Aj == j - i (j>i) <=> Ai + i == j - Aj
  // Li = Ai + i, Ri = i - Ai
  // Li == X && Rj == X
  map<ll, ll> L;
  map<ll, ll> R;
  ll ans = 0;

  for (ll i = 0; i < n; i++) {
    L[i] = a[i] + i;
    for (ll j = i + 1; j < n; j++) {
      R[j] = j - a[j];
      if (L[i] == R[j])
        ans++;
    }
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1e9;
const long long LINF = 1e18;

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

  vector<int> A(N);
  map<int, int> setA;

  long long ans = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    setA[i + 1 - A[i]]++;

    // cout << "setA[ " << i + 1 - A[i] << " ] = " << setA[i + 1 - A[i]] <<
    // endl;
  }

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

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

const int INF = 1e9;
const long long LINF = 1e18;

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

  vector<int> A(N);
  multiset<int> setA;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    setA.insert(i + 1 - A[i]);
  }

  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += setA.count(i + 1 + A[i]);
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG // GCC5.4.1で配列の範囲外アクセスをエラーにする

// cmdで g++ test.cpp とするとコンパイルできる
// 制限1秒なら10^6は余裕 10^7は多分間に合う 10^8はよほど単純でないと厳しい

#define ALL(x) x.begin(), x.end()
typedef long long ll;            // long long
typedef pair<int, int> pii;      // pair<int,int>
typedef vector<int> vi;          // vector<int>
typedef vector<vector<int>> vvi; // vector<vector<int>>
typedef vector<ll> vll;          // vector<long long>
typedef vector<vector<ll>> vvll; // vector<vector<long long>>
typedef priority_queue<int, vector<int>, greater<int>> gpq; // 逆順priority_queue
typedef priority_queue<int> pq;

// const int MOD=998244353;
const int MOD = 1000000007; // 10^9+7
const double PI = 3.1415926535;

int main() {
  int N;
  cin >> N;
  vi A(N);
  vector<pii> P(N);
  vector<pii> M(N);

  for (int i = 0; i < N; i++) {
    cin >> A[i];
    P[i] = make_pair(A[i] + i, i);
    M[i] = make_pair(i - A[i], i);
  }
  // sort(ALL(P));
  sort(ALL(M));
  vi Mf(N);
  vi Ms(N);
  for (int i = 0; i < N; i++) {
    Mf[i] = M[i].first;
    Ms[i] = M[i].second;
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int val = P[i].first;
    int start = lower_bound(ALL(Mf), val) - Mf.begin();
    int end = upper_bound(ALL(Mf), val) - Mf.begin();
    int ans_i =
        (upper_bound(Ms.begin() + start, Ms.begin() + end, P[i].second) -
         Ms.begin()) -
        start;
    ans += (end - start) - ans_i;
    // for(int j=start;j<end;j++){
    //     if(P[i].second<Ms[j])ans++;
    // }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG // GCC5.4.1で配列の範囲外アクセスをエラーにする

// cmdで g++ test.cpp とするとコンパイルできる
// 制限1秒なら10^6は余裕 10^7は多分間に合う 10^8はよほど単純でないと厳しい

#define ALL(x) x.begin(), x.end()
typedef long long ll;            // long long
typedef pair<int, int> pii;      // pair<int,int>
typedef vector<int> vi;          // vector<int>
typedef vector<vector<int>> vvi; // vector<vector<int>>
typedef vector<ll> vll;          // vector<long long>
typedef vector<vector<ll>> vvll; // vector<vector<long long>>
typedef priority_queue<int, vector<int>, greater<int>> gpq; // 逆順priority_queue
typedef priority_queue<int> pq;

// const int MOD=998244353;
const int MOD = 1000000007; // 10^9+7
const double PI = 3.1415926535;

int main() {
  int N;
  cin >> N;
  vi A(N);
  vector<pii> P(N);
  vector<pii> M(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    P[i] = make_pair(A[i] + i, i);
    M[i] = make_pair(i - A[i], i);
  }
  sort(ALL(P));
  sort(ALL(M));
  vi Mf(N);
  for (int i = 0; i < N; i++) {
    Mf[i] = M[i].first;
  }

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    int val = P[i].first;
    int start = lower_bound(ALL(Mf), val) - Mf.begin();
    int end = upper_bound(ALL(Mf), val) - Mf.begin();
    for (int j = start; j < end; j++) {
      if (P[i].second < M[j].second)
        ans++;
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) {
    long long x;
    cin >> x;
    A[i] = x;
  }
  vector<long long> B(N);
  rep(i, N) { B[i] = A[i] - i - 1; }
  map<long long, int> C;
  rep(i, N) { C[A[i] + i + 1]++; }
  // sort(B.begin(),B.end());
  // sort(C.begin(),C.end());
  long long c = 0;
  for (int i = 0; i < N; i++) {
    // count+=upper_bound(C.begin(),C.end(),-B[i])-lower_bound(C.begin(),C.end(),-B[i])+1;
    c += C[-B[i]];
  }
  // rep(i,N)cout << B[i] << endl;
  cout << c << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long N;
  cin >> N;
  vector<long long> A(N);
  rep(i, N) {
    int x;
    cin >> x;
    A[i] = min(x, (300000));
  }
  vector<long long> B(N);
  rep(i, N) { B[i] = A[i] - i - 1; }
  multiset<long long> C;
  rep(i, N) { C.insert(A[i] + i + 1); }
  // sort(B.begin(),B.end());
  // sort(C.begin(),C.end());
  int c = 0;
  for (int i = 0; i < N; i++) {
    // count+=upper_bound(C.begin(),C.end(),-B[i])-lower_bound(C.begin(),C.end(),-B[i])+1;
    c += C.count(-B[i]);
  }
  // rep(i,N)cout << B[i] << endl;
  cout << c << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  map<long long, int> M;
  vector<long long> a(n + 1, 0), L(n + 1, 0), R(n + 1, 0);

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

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

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

#include <bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);

  freopen("output.txt", "w", stdout);
#endif

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

  ll n;
  cin >> n;
  vector<ll> l;
  unordered_map<ll, ll> cntr;
  unordered_map<ll, vector<ll>> r;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    l.pb(i + x);
    r[i - x].pb(i);
    cntr[i - x]++;
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (cntr[l[i]]) {
      auto it = upper_bound(r[l[i]].begin(), r[l[i]].end(), l[i]);
      ans += r[l[i]].end() - it;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int ll;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);

  freopen("output.txt", "w", stdout);
#endif

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

  ll n;
  cin >> n;
  vector<ll> l;
  unordered_map<ll, ll> cntr;
  unordered_map<ll, set<ll>> r;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    l.pb(i + x);
    r[i - x].insert(i);
    cntr[i - x]++;
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (cntr[l[i]]) {
      for (auto x : r[l[i]]) {
        if (x > i) {
          ans++;
        } else {
          break;
        }
      }
    }
  }
  cout << ans << endl;
}