Problem p01777 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 2147483647

int N, input[100000];
vector<int> SegTree[263000];

void construct_segtree(int node_id, int node_left, int node_right) {
  if (node_left == node_right) {

    SegTree[node_id].push_back(input[node_left]);
  } else {
    construct_segtree(2 * node_id + 1, node_left, (node_left + node_right) / 2);
    construct_segtree(2 * node_id + 2, (node_left + node_right) / 2 + 1,
                      node_right);
    SegTree[node_id].resize(node_right - node_left + 1);

    merge(SegTree[2 * node_id + 1].begin(), SegTree[2 * node_id + 1].end(),
          SegTree[2 * node_id + 2].begin(), SegTree[2 * node_id + 2].end(),
          SegTree[node_id].begin());
  }
}

int calc(int base_value, int search_left, int search_right, int node_id,
         int node_left, int node_right, bool isUpper) {

  if (search_right < node_left || search_left > node_right) {
    return 0;
  }

  if (search_left <= node_left && search_right >= node_right) {

    int left = 0, right = SegTree[node_id].size() - 1, m = (left + right) / 2,
        index = -1;

    if (isUpper) {
      while (left <= right) {
        if (SegTree[node_id][m] > base_value) {
          index = m;
          right = m - 1;
        } else {
          left = m + 1;
        }
        m = (left + right) / 2;
      }

      if (index == -1) {
        return 0;
      } else {
        return SegTree[node_id].size() - index;
      }

    } else {
      while (left <= right) {
        if (SegTree[node_id][m] < base_value) {
          index = m;
          left = m + 1;
        } else {
          right = m - 1;
        }
        m = (left + right) / 2;
      }

      if (index == -1) {
        return 0;
      } else {
        return index + 1;
      }
    }
  }

  int left_num = calc(base_value, search_left, search_right, 2 * node_id + 1,
                      node_left, (node_left + node_right) / 2, isUpper);
  int right_num = calc(base_value, search_left, search_right, 2 * node_id + 2,
                       (node_left + node_right) / 2 + 1, node_right, isUpper);

  return left_num + right_num;
}

int main() {

  int first_N;

  scanf("%d", &first_N);
  N = 1;

  while (N < first_N)
    N *= 2;

  for (int i = 0; i < N; i++)
    input[i] = BIG_NUM;

  for (int i = 0; i < first_N; i++) {
    scanf("%d", &input[i]);
  }

  construct_segtree(0, 0, N - 1);

  int Q;
  scanf("%d", &Q);

  int left, right, diff, ans;

  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d", &left, &right, &diff);
    left--;
    right--;
    ans = calc(max(input[left], input[right]) + diff, left, right, 0, 0, N - 1,
               true);
    ans += calc(min(input[left], input[right]) - diff, left, right, 0, 0, N - 1,
                false);
    printf("%d\n", ans);
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 2147483647

int N, input[263000];
vector<int> SegTree[263000];

void construct_segtree(int node_id, int node_left, int node_right) {
  if (node_left == node_right) {

    SegTree[node_id].push_back(input[node_left]);
  } else {
    construct_segtree(2 * node_id + 1, node_left, (node_left + node_right) / 2);
    construct_segtree(2 * node_id + 2, (node_left + node_right) / 2 + 1,
                      node_right);
    SegTree[node_id].resize(node_right - node_left + 1);

    merge(SegTree[2 * node_id + 1].begin(), SegTree[2 * node_id + 1].end(),
          SegTree[2 * node_id + 2].begin(), SegTree[2 * node_id + 2].end(),
          SegTree[node_id].begin());
  }
}

int calc(int base_value, int search_left, int search_right, int node_id,
         int node_left, int node_right, bool isUpper) {

  if (search_right < node_left || search_left > node_right) {
    return 0;
  }

  if (search_left <= node_left && search_right >= node_right) {

    int left = 0, right = SegTree[node_id].size() - 1, m = (left + right) / 2,
        index = -1;

    if (isUpper) {
      while (left <= right) {
        if (SegTree[node_id][m] > base_value) {
          index = m;
          right = m - 1;
        } else {
          left = m + 1;
        }
        m = (left + right) / 2;
      }

      if (index == -1) {
        return 0;
      } else {
        return SegTree[node_id].size() - index;
      }

    } else {
      while (left <= right) {
        if (SegTree[node_id][m] < base_value) {
          index = m;
          left = m + 1;
        } else {
          right = m - 1;
        }
        m = (left + right) / 2;
      }

      if (index == -1) {
        return 0;
      } else {
        return index + 1;
      }
    }
  }

  int left_num = calc(base_value, search_left, search_right, 2 * node_id + 1,
                      node_left, (node_left + node_right) / 2, isUpper);
  int right_num = calc(base_value, search_left, search_right, 2 * node_id + 2,
                       (node_left + node_right) / 2 + 1, node_right, isUpper);

  return left_num + right_num;
}

int main() {

  int first_N;

  scanf("%d", &first_N);
  N = 1;

  while (N < first_N)
    N *= 2;

  for (int i = 0; i < N; i++)
    input[i] = BIG_NUM;

  for (int i = 0; i < first_N; i++) {
    scanf("%d", &input[i]);
  }

  construct_segtree(0, 0, N - 1);

  int Q;
  scanf("%d", &Q);

  int left, right, diff, ans;

  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d", &left, &right, &diff);
    left--;
    right--;
    ans = calc(max(input[left], input[right]) + diff, left, right, 0, 0, N - 1,
               true);
    ans += calc(min(input[left], input[right]) - diff, left, right, 0, 0, N - 1,
                false);
    printf("%d\n", ans);
  }

  return 0;
}