Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG // TLEの原因になるので注意！！！！！！！！！！！
#include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long

// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

bool chmn(int &a, int b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int A[200005];
int cc = 0;
vector<int> V;
int B[200005];

int main() {
  int n;
  ll ans = 0;
  cin >> n;
  rep(i, n) {
    cin >> A[i];
    B[i] = A[i] - i;
    if (-200005 < B[i] && B[i] < 0) {
      V.push_back(-B[i]);
    }
  }
  sort(V.begin(), V.end());
  rep(i, n) {
    int k = A[i] + i;
    if (200005 > k) {
      int temp = upper_bound(V.begin(), V.end(), k) -
                 lower_bound(V.begin(), V.end(), k);
      ans += temp;
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
// #define _GLIBCXX_DEBUG//TLEの原因になるので注意！！！！！！！！！！！
#include <bits/stdc++.h>
#include <cmath>
using ll = long long;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int, int>> qu2;
priority_queue<int> pq;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define mins(x, y) x = min(x, y)
#define maxs(x, y) x = max(x, y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.sise())
#define mod 1000000007
#define reps(i, s, n) for (int i = s; i < n; i++)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
deque<int> deq;
#define fi first
#define se second
// #define int long long

// cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
// const ll INF = 1LL << 60;
// typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}

bool chmn(int &a, int b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int A[200005];
int cc = 0;
vector<int> V;
int B[200005];

int main() {
  int n;
  ll ans = 0;
  cin >> n;
  rep(i, n) {
    cin >> A[i];
    B[i] = A[i] - i;
    if (-200005 < B[i] && B[i] < 0) {
      V.push_back(-B[i]);
    }
  }
  sort(V.begin(), V.end());
  rep(i, n) {
    int k = A[i] + i;
    if (200005 > k) {
      int temp = upper_bound(V.begin(), V.end(), k) -
                 lower_bound(V.begin(), V.end(), k);
      ans += temp;
    }
  }
  cout << ans << endl;
  return 0;
}