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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
const ll MOD = 1000000007;
const long long INF = 1LL << 60;
const double pi = acos(-1.0);

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;
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];

  vector<ll> vec(0);
  rep(i, N) vec.push_back(i - A[i]);
  sort(vec.begin(), vec.end());

  ll ans = 0;
  rep(i, N) {

    ll ok = N, ng = -1, low, up;
    while (abs(ok - ng) > 1) {
      ll mid = (ok + ng) / 2;
      if (A[i] + i <= vec[mid])
        ok = mid;
      else
        ng = mid;
    }
    low = ok;

    ok = N, ng = -1;
    while (abs(ok - ng) > 1) {
      ll mid = (ok + ng) / 2;
      if (A[i] + i < vec[mid])
        ok = mid;
      else
        ng = mid;
    }
    up = ok;

    ans += up - low;
  }

  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++)
using ll = long long;
const ll MOD = 1000000007;

ll my_max(ll a, ll b) {
  if (a >= b) {
    return a;
  } else {
    return b;
  }
}

int main() {
  ll N;
  cin >> N;
  vector<vector<ll>> vec(N, vector<ll>(2));
  rep(i, N) {
    vec.at(i).at(0) = i;
    cin >> vec.at(i).at(1);
  }
  ll rej = 0;
  rep(i, N) {
    if (my_max(i, N - i - 1) <= vec.at(i - rej).at(1)) {
      vec.erase(vec.begin() + i - rej);
      rej++;
    }
  }
  ll num = N - rej, ans = 0;
  rep(i, num) {
    rep(j, num - i - 1) {
      ll sa = abs(vec.at(i).at(0) - vec.at(i + j + 1).at(0));
      ll wa = vec.at(i).at(1) + vec.at(i + j + 1).at(1);
      if (sa == wa) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

const int INF = 1e9;
const int MOD = 1000000007;

int main() {
  size_t N, A, count = 0;
  int val1, val2;
  unordered_map<int, size_t> map1, map2;
  cin >> N;
  for (size_t i = 0; i < N; ++i) {
    cin >> A;
    val1 = A + i;
    val2 = i - A;
    count += map1[val2] + map2[val1];
    map1[val1]++;
    map2[val2]++;
  }

  cout << count << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

const int INF = 1e9;
const int MOD = 1000000007;

int main() {
  size_t N, count = 0;
  cin >> N;
  vector<size_t> A(N);
  for (size_t i = 0; i < N; ++i) {
    cin >> A[i];
  }

  for (size_t i = 0; i < N - 1; ++i) {
    for (size_t j = i + A[i]; j < N; ++j) {
      if (j - i == A[i] + A[j]) {
        count++;
      }
    }
  }

  cout << count << endl;

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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
// #define num 1000000007
#define pi acos(-1.0)
// cout << fixed << setprecision (20);   小数点以下2０桁まで
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'0'+=16;        で大文字に
//'大文字'+=32;    で小文字に
// s[i]-'0'でchar文字→int数に;
// string s = to_string(int数);
// int n = stoi(string文字列)
// 実行時間制約2秒では２×10^8回くらいまで計算できる

int main() {

  ll n;
  cin >> n;
  ll ans = 0;

  map<ll, ll> p;
  map<ll, ll> q;

  rep(i, n) {
    ll x;
    cin >> x;
    if (i + 1 + x < 200000) {
      if (p.count(i + 1 + x))
        p.at(i + 1 + x)++;
      else
        p[i + 1 + x] = 1;
    }
    if (0 < i + 1 - x) {
      if (q.count(i + 1 - x))
        q.at(i + 1 - x)++;
      else
        q[i + 1 - x] = 1;
    }
  }

  rep(i, 200000) {
    if (p.count(i) && q.count(i))
      ans += p.at(i) * q.at(i);
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
// #define num 1000000007
#define pi acos(-1.0)
// cout << fixed << setprecision (20);   小数点以下2０桁まで
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'0'+=16;        で大文字に
//'大文字'+=32;    で小文字に
// s[i]-'0'でchar文字→int数に;
// string s = to_string(int数);
// int n = stoi(string文字列)
// 実行時間制約2秒では２×10^8回くらいまで計算できる

int main() {

  int n;
  cin >> n;
  int ans = 0;

  int x;
  cin >> x;
  vector<int> a = {1 - x};

  repo(i, n) {
    int x;
    cin >> x;
    for (int z = 0; z < a.size(); z++) {
      if (a[z] == x)
        ans++;
      a[z]++;
    }
    a.pb(1 - x);
  }

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

#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = s; i < n; i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using ll = long long;

// 深さ優先
vector<int> seen;
vector<ll> h;

// 普通の深さ優先探索seenを返す感じ
void dfs(const Graph &G, int v) {
  seen[v] = 0; // v探索済み

  P good = make_pair(v, h[v]);
  for (auto next_v : G[v]) {
    if (seen[next_v] != -1)
      continue; // もう探索していた
    if (h[next_v] > good.second) {
      good = make_pair(next_v, h[next_v]);
    }
    dfs(G, next_v);
  }

  if (!(h[v] == good.second && v == good.first)) {
    seen[good.first] = 1;
  }
}

int main() {
  /*
  int n,m;
  cin>>n>>m;

  h.assign(n,0);
  rep(i,0,n) cin>>h[i];

  Graph G(n);
  rep(i,0,m){
      int a,b; cin>>a>>b;
      G[a-1].push_back(b-1);
      G[b-1].push_back(a-1);
  }

  seen.assign(n,-1);

  rep(i,0,n){
      if(seen[i]==-1){
          dfs(G,i);
      }
  }

  int sum=accumulate(all(seen),0);

  cout<<sum<<endl;*/

  ll n;
  cin >> n;

  map<ll, ll> L, R;
  ll A[n];

  rep(i, 0, n) {
    ll a;
    cin >> a;
    A[i] = a;
    L[i + a]++;
    R[i - a]++;
  }

  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ll l = A[i] + i;
    R[i - A[i]]--;
    ans += R[l];
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, s, n) for (int i = s; i < n; i++)
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using ll = long long;

// 深さ優先
vector<int> seen;
vector<ll> h;

// 普通の深さ優先探索seenを返す感じ
void dfs(const Graph &G, int v) {
  seen[v] = 0; // v探索済み

  P good = make_pair(v, h[v]);
  for (auto next_v : G[v]) {
    if (seen[next_v] != -1)
      continue; // もう探索していた
    if (h[next_v] > good.second) {
      good = make_pair(next_v, h[next_v]);
    }
    dfs(G, next_v);
  }

  if (!(h[v] == good.second && v == good.first)) {
    seen[good.first] = 1;
  }
}

int main() {
  /*
  int n,m;
  cin>>n>>m;

  h.assign(n,0);
  rep(i,0,n) cin>>h[i];

  Graph G(n);
  rep(i,0,m){
      int a,b; cin>>a>>b;
      G[a-1].push_back(b-1);
      G[b-1].push_back(a-1);
  }

  seen.assign(n,-1);

  rep(i,0,n){
      if(seen[i]==-1){
          dfs(G,i);
      }
  }

  int sum=accumulate(all(seen),0);

  cout<<sum<<endl;*/

  ll n;
  cin >> n;
  map<ll, set<ll>> man;
  ll A[n];
  set<ll> s;

  rep(i, 0, n) {
    ll a;
    cin >> a;
    A[i] = a;
    if (a <= 2 * 1e5) {
      man[a].insert(i);
      s.insert(a);
    }
  }

  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    if (A[i] <= 2 * 1e5) {
      // for(auto m:man[A[i]]) cout<<m<<endl;
      man[A[i]].erase(i);
      if (man[A[i]].size() == 0)
        s.erase(A[i]);
      // for(auto m:s) cout<<m<<" ";
      // cout<<endl;
      for (auto ai : s) {
        // cout<<A[i]+ai+i;
        ans += man[ai].count(A[i] + ai + i);
      }
    }
  }

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

#include "bits/stdc++.h"

using namespace std;

#define sp(x) cout << setprecision(x);
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX * 0.99
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define yes "Yes"
#define no "No"
#define divs 1000000007
#define dive 1000000009
#define pi 3.141592653589793238

typedef pair<int, int> P;

signed main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  // Failed to predict input format
  int N;
  cin >> N;
  vector<int> a(N);
  REP(i, N) { cin >> a[i]; }
  int ans = 0;
  map<int, int> ma;
  vector<int> b(N);
  REP(i, N) { ma[i - a[i]]++; }
  REP(i, N) { ans += ma[a[i] + i]; }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

#define sp(x) cout << setprecision(x);
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX * 0.99
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define yes "Yes"
#define no "No"
#define divs 1000000007
#define dive 1000000009
#define pi 3.141592653589793238

typedef pair<int, int> P;

signed main() {
  // ios::sync_with_stdio(false);
  // cin.tie(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  // Failed to predict input format
  int N;
  cin >> N;
  vector<int> a(N);
  REP(i, N) cin >> a[i];
  int ans = 0;
  REP(i, N - 1) {
    FOR(j, i, N) {
      if (a[j] + a[i] == j - i)
        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, ans = 0;
LL a[2000100], b[2000100];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i > a[i]) {
      b[i - a[i]]++;
    }
  }

  for (int i = 1; i <= n; i++) {
    if (i > a[i]) {
      b[i - a[i]]--;
    }
    if (i + a[i] <= n) {
      ans += b[i + a[i]];
    }
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n, ans = 0;
LL a[2000100], b[2000100];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i > a[i]) {
      b[i - a[i]]++;
    }
  }

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

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

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

  for (int i = 0; i < n; i++) {
    int sa = i - a[i];
    ans += mp[sa];
    int wa = i + a[i];
    mp[wa]++;
  }

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

Error: -11

Fixed submission:

#include <iostream>
#include <math.h>
using ll = long long;
using namespace std;

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

  for (int i = 0; i < n; i++) {
    int sa = i - a[i];
    ans += kaitou[sa];
    int wa = i + a[i];
    kaitou[wa]++;
  }

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

#include <bits/stdc++.h>
#define INF 100000000000
#define MOD 1000000007
#define ll long long int
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

// #define int long long
using namespace std;

signed main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  vector<ll> B(N), C(N);
  ll yobun = 0;
  map<ll, ll> Mb, Mc;
  REP(i, N) {
    B[i] = i - A[i];
    C[i] = i + A[i];
    if (B[i] == C[i])
      yobun++;
    Mb[B[i]]++;
    Mc[C[i]]++;
  }
  // sort(all(B));
  // sort(all(C));

  ll ans = 0;

  /*

  REP(i,N){
    cout<<B[i]<<" ";
  }
  cout<<endl;
  REP(i,N){
    cout<<C[i]<<" ";
  }
  cout<<endl;
  */

  // int bi=0,ci=0;
  auto itr1 = Mb.begin();
  auto itr2 = Mc.begin();
  // set <int> com;
  for (;;) {
    auto BB = (*itr1);
    auto CC = (*itr2);

    if (itr1 == Mb.end() or itr2 == Mc.end())
      break;

    if (BB.first > CC.first) {
      itr2++;
      continue;
    }
    if (BB.first < CC.first) {
      itr1++;
      continue;
    }

    ans += BB.second * CC.second;
    // com.insert(B[bi]);

    itr1++;

    // if(bi==N-1 and ci == N-1) break;
  }

  /*

  for(auto itr = com.begin();itr != com.end();itr++){
    int now = (*itr) ;
    int cnt1 = 0,cnt2 = 0;
    REP(i,N){
      if(B[i] == now) cnt1++;
      if(C[i] == now) cnt2++;
    }
    ans += cnt1 * cnt2;
  }
  */

  assert(ans - yobun >= 0);

  cout << ans - yobun << endl;
  // cout<<yobun<<endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 100000000000
#define MOD 1000000007
#define ll long long int
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  vector<int> B(N), C(N);
  int yobun = 0;
  map<int, int> Mb, Mc;
  REP(i, N) {
    B[i] = i - A[i];
    C[i] = i + A[i];
    if (B[i] == C[i])
      yobun++;
    Mb[B[i]]++;
    Mc[C[i]]++;
  }
  // sort(all(B));
  // sort(all(C));

  ll ans = 0;

  /*

  REP(i,N){
    cout<<B[i]<<" ";
  }
  cout<<endl;
  REP(i,N){
    cout<<C[i]<<" ";
  }
  cout<<endl;
  */

  int bi = 0, ci = 0;
  auto itr1 = Mb.begin();
  auto itr2 = Mc.begin();
  // set <int> com;
  for (;;) {
    auto BB = (*itr1);
    auto CC = (*itr2);

    if (itr1 == Mb.end() or itr2 == Mc.end())
      break;

    if (BB.first > CC.first) {
      itr2++;
      continue;
    }
    if (BB.first < CC.first) {
      itr1++;
      continue;
    }

    ans += BB.second * CC.second;
    // com.insert(B[bi]);

    itr1++;
    itr2++;

    // if(bi==N-1 and ci == N-1) break;
  }

  /*

  for(auto itr = com.begin();itr != com.end();itr++){
    int now = (*itr) ;
    int cnt1 = 0,cnt2 = 0;
    REP(i,N){
      if(B[i] == now) cnt1++;
      if(C[i] == now) cnt2++;
    }
    ans += cnt1 * cnt2;
  }
  */

  assert(ans - yobun >= 0);

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

#include <bits/stdc++.h>
#define INT_MAX 2147483647
#define INT_MIN -2147483647
using namespace std;
int n, h;
multimap<int, int> fr;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> n;
  long long cnt = 0;

  for (int i = 1; i <= n; i++) {
    cin >> h;
    int dif = i - h;
    if (dif > 0) {
      if (fr.find(dif) != fr.end())
        cnt += fr.find(dif)->second;
    }
    int nou = i + h;
    if (fr.find(nou) == fr.end())
      fr.insert(make_pair(nou, 0));
    fr.find(nou)->second++;
  }
  cout << cnt;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INT_MAX 2147483647
#define INT_MIN -2147483647
using namespace std;
int n, h[200010];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];
  long long cnt = 0;
  for (int i = 1; i < n; i++)
    for (int j = i + 1; j <= n; j++)
      if (j - i == h[i] + h[j])
        cnt++;
  cout << cnt;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define REP(i, l, n) for (int i = (l), i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define pb push_back

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                  's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

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

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

Error: TLE

Fixed submission:

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

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define REP(i, l, n) for (int i = (l), i##_len = (n); i < i##_len; ++i)
#define ALL(x) (x).begin(), (x).end()
#define pb push_back

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                  'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                  's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

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

int main() {
  int n;
  cin >> n;
  vector<ll> lis(n + 1);
  REP(i, 0, n) { cin >> lis[i + 1]; }
  ll ans = 0;
  REP(i, 1, n) {
    int k = lis[i];
    REP(l, k + i, n + 1) {
      if (l - i == lis[i] + lis[l]) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

#define MAX 1000000005

typedef long long ll;
typedef pair<int, int> P;

int main(int, char **) {
  int n;
  cin >> n;

  ll ind = 0, a;
  ll ans = 0;
  map<int, int> count;
  rep(i, n) {
    cin >> a;
    if (ind - a >= 0) {
      ans += count[ind - a];
    }
    auto itr = count.find(ind + a);
    if (itr != count.end())
      itr->second++;
    else
      count[ind + a] = 1;
    ind++;
  }

  cout << ans << endl;

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

#define MAX 1000000005

typedef long long ll;
typedef pair<int, int> P;

int main(int, char **) {
  int n;
  cin >> n;

  vector<int> count(MAX * 2, 0);
  int ind = 0, a;
  int ans = 0;
  rep(i, n) {
    cin >> a;
    if (ind - a >= 0)
      ans += count[ind - a];
    count[ind + a]++;
    ind++;
  }

  cout << ans << endl;

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

#include <bits/stdc++.h>

/*
Contrainst n is 2 * 10^5 for all pairs it will be
 4  * 10^10 , so if e solve in n^2 time, it will not be solved in 2 seconds.
 this is the hing that we have to solve them better than nlogn

*/
using namespace std;

int main() {
  // int n;
  // unordered_map<int, int> m;
  // int res = 0;
  // cin >> n;
  // vector<int> nums(n);
  //
  // for(int i = 0; i < n; i++){
  // 	cin >> nums[i];
  //   m[i] = nums[i];
  // }
  //
  // for(int i = 0; i < n; i++){
  // 	for(int j = i + 1; j < n; j++){
  //   	if(j - i == (m[i] + m[j]))res++;
  //   }
  // }
  //
  // cout << res << endl;

  // Above code runs in O(n^2) time.

  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  unordered_map<int, int> mp;
  long long res = 0;
  for (int i = 0; i < n; i++) {
    int sa = i - a[i];
    res += mp[sa];
    int wa = a[i] + i;
    mp[wa]++;
  }

  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  long n;
  unordered_map<int, int> m;
  int res = 0;
  cin >> n;
  vector<int> nums(n);

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

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

  cout << res << endl;

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  map<int, int> m;
  ll ans = 0;
  rep(i, n) {
    int s = i - a[i];
    int w = a[i] + i;
    ans += m[s];
    m[w]++;
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> sum_i(n);
  rep(i, n) sum_i[i] = i + a[i];
  ll M = 0;
  rep(i, n) M = max(M, sum_i[i]);
  vector<ll> check(M + 1);
  check[sum_i[0]]++;
  ll ans = 0;
  for (ll i = 1; i < n; i++) {
    ll tmp = i - a[i];
    if (tmp > 0) {
      ans += check[tmp];
    }
    check[sum_i[i]]++;
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded


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

int main(void) {

  int N;
  cin >> N;

  map<ll, ll> L;
  map<ll, ll> R;

  for (ll i = 1; i <= N; ++i) {
    ll A;
    cin >> A;
    L[i + A]++;
    R[i - A]++;
  }
  ll sum = 0;

  for (auto it1 : L) {
    sum += it1.second * R[it1.first];
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:


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

int main(void) {

  int N;
  cin >> N;

  map<ll, int> L;
  map<ll, int> R;

  for (ll i = 1; i <= N; ++i) {
    ll A;
    cin >> A;
    L[i + A]++;
    R[i - A]++;
  }
  int sum = 0;

  for (auto it1 : L) {
    for (auto it2 : R) {
      if (it1.first == it2.first)
        sum += it1.second * it2.second;
    }
  }
  cout << sum << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long ans = 0;
  vector<int> s(n);
  map<int, int> mapp;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    s[i] = i + a + 1;
    mapp[i - a + 1]++;
  }
  for (int i = 0; i < n; i++)
    ans += mapp[s[i]];
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
bool cmp2(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> a(n + 1);
  for (int i = 1; i <= n; i++) {
    a[i].first = i;
    cin >> a[i].second;
  }
  int d = 0;
  sort(a.begin(), a.end(), cmp2);
  for (int i = 1; i <= n - 1; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (a[i].second + a[j].second > n)
        break;
      else if (a[i].second + a[j].second == abs(a[i].first - a[j].first))
        d++;
    }
  }
  cout << d;
  return 0;
}