Problem p01777 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 114514
int n;
vector<int> dat[MAX * 2 - 1];

int query(int a, int b, int u, int d, int k = 0, int l = 0, int r = n) {
  // cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b) {
    // cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
    return upper_bound(dat[k].begin(), dat[k].end(), u) -
           lower_bound(dat[k].begin(), dat[k].end(), d);
  }
  return query(a, b, u, d, k * 2 + 1, l, (l + r) / 2) +
         query(a, b, u, d, k * 2 + 2, (l + r) / 2, r);
}

int x[MAX], l[MAX], r[MAX], e[MAX];

signed main() {
  int d, q;
  cin >> d;
  vector<int> v;
  for (int i = 0; i < d; i++) {
    cin >> x[i];
    v.push_back(x[i]);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i] >> e[i];
    l[i]--;
    v.push_back(min(x[l[i]], x[r[i] - 1]) - e[i]);
    v.push_back(max(x[l[i]], x[r[i] - 1]) + e[i]);
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  map<int, int> m;
  for (int i = 0; i < (int)v.size(); i++)
    m[v[i]] = i;

  n = 1;
  while (n < d)
    n *= 2;
  // cout<<n<<endl;

  for (int i = 0; i < d; i++) {
    dat[n - 1 + i].push_back(m[x[i]]);
  }

  for (int i = n - 2; i >= 0; i--) {
    for (int k = 1; k <= 2; k++) {
      for (int j = 0; j < (int)dat[i * 2 + k].size(); j++)
        dat[i].push_back(dat[i * 2 + k][j]);
    }
    sort(dat[i].begin(), dat[i].end());
  }
  /*//
  for(int i=0;i<n*2-1;i++){
    cout<<i<<"-"<<endl;
    for(int j=0;j<(int)dat[i].size();j++){
      cout<<dat[i][j]<<" ";
    }
    cout<<endl;
  }
  //*/
  for (int i = 0; i < q; i++) {
    // cout<<r[i]-l[i]<<endl;
    int inner = query(l[i], r[i], m[max(x[l[i]], x[r[i] - 1]) + e[i]],
                      m[min(x[l[i]], x[r[i] - 1]) - e[i]]);
    int outer = query(l[i], r[i], v.size(), 0);
    // cout<<outer<<":"<<inner<<endl;
    cout << outer - inner << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MAX 414514
int n;
vector<int> dat[MAX * 2 - 1];

int query(int a, int b, int u, int d, int k = 0, int l = 0, int r = n) {
  // cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b) {
    // cout<<a<<" "<<b<<" "<<k<<" "<<l<<" "<<r<<endl;
    return upper_bound(dat[k].begin(), dat[k].end(), u) -
           lower_bound(dat[k].begin(), dat[k].end(), d);
  }
  return query(a, b, u, d, k * 2 + 1, l, (l + r) / 2) +
         query(a, b, u, d, k * 2 + 2, (l + r) / 2, r);
}

int x[MAX], l[MAX], r[MAX], e[MAX];

signed main() {
  int d, q;
  cin >> d;
  vector<int> v;
  for (int i = 0; i < d; i++) {
    cin >> x[i];
    v.push_back(x[i]);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i] >> e[i];
    l[i]--;
    v.push_back(min(x[l[i]], x[r[i] - 1]) - e[i]);
    v.push_back(max(x[l[i]], x[r[i] - 1]) + e[i]);
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  map<int, int> m;
  for (int i = 0; i < (int)v.size(); i++)
    m[v[i]] = i;

  n = 1;
  while (n < d)
    n *= 2;
  // cout<<n<<endl;

  for (int i = 0; i < d; i++) {
    dat[n - 1 + i].push_back(m[x[i]]);
  }

  for (int i = n - 2; i >= 0; i--) {
    for (int k = 1; k <= 2; k++) {
      for (int j = 0; j < (int)dat[i * 2 + k].size(); j++)
        dat[i].push_back(dat[i * 2 + k][j]);
    }
    sort(dat[i].begin(), dat[i].end());
  }
  /*//
  for(int i=0;i<n*2-1;i++){
    cout<<i<<"-"<<endl;
    for(int j=0;j<(int)dat[i].size();j++){
      cout<<dat[i][j]<<" ";
    }
    cout<<endl;
  }
  //*/
  for (int i = 0; i < q; i++) {
    // cout<<r[i]-l[i]<<endl;
    int inner = query(l[i], r[i], m[max(x[l[i]], x[r[i] - 1]) + e[i]],
                      m[min(x[l[i]], x[r[i] - 1]) - e[i]]);
    int outer = query(l[i], r[i], v.size(), 0);
    // cout<<outer<<":"<<inner<<endl;
    cout << outer - inner << endl;
  }

  return 0;
}