Problem p02874 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const long long MOD = 1000000007LL;
const string alpha = "abcdefghijklmnopqrstuvwxyz";
struct segment_tree {
  const long INF = 2 * 1e9;
  long n = 1;
  long data[100013];
  segment_tree(int a) {
    while (n < a)
      n *= 2;
    for (int i = 0; i < 2 * n; i++)
      data[i] = INF;
  }
  // segment treeの大きさ取得
  long nreturn() { return n; }
  // 0-indexでk番目の値をaに変更
  void change(long k, long a) {
    k += n - 1;
    data[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = min(data[2 * k + 1], data[2 * k + 2]);
      // cout << "data" << k << "is changed to" << data[k] << endl;
    }
    return;
  }
  // 区間[a,b)の最小値:呼ぶときはrmq(a,b,0,0,n)
  long rmq(long a, long b, long k, long l, long r) {
    if (a >= r || b <= l)
      return INF;
    if (a <= l && b >= r)
      return data[k];
    long teml = rmq(a, b, 2 * k + 1, l, (l + r) / 2);
    long temr = rmq(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(teml, temr);
  }
};
int main() {
  int n;
  cin >> n;
  pair<long, long> lr[n];
  rep(i, n) cin >> lr[i].first >> lr[i].second;
  sort(lr, lr + n);
  segment_tree tree(n);
  rep(i, n) tree.change(i, lr[i].second);
  long ans = 0;
  long nn = tree.nreturn();
  for (int i = 0; i < n - 1; i++) {
    long tmp =
        max(long(0), long(1) + tree.rmq(0, i + 1, 0, 0, nn) - lr[i].first);
    tmp +=
        max(long(0), long(1) + tree.rmq(i + 1, nn, 0, 0, nn) - lr[n - 1].first);
    ans = max(ans, tmp);
    tmp = 0;
    tmp += (lr[i].second - lr[i].first) + 1;
    tmp += max(long(0), long(1) +
                            min(tree.rmq(0, i, 0, 0, nn),
                                tree.rmq(i + 1, nn, 0, 0, nn)) -
                            lr[n - 1].first);
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  /*segment_tree tree(7);
  for(int i = 0;i<7;i++) tree.change(i,i);
  for(int i = 0;i<15;i++) cout << tree.data[i] << " " ;
  cout << tree.rmq(1,2,0,0,7);*/
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const long long MOD = 1000000007LL;
const string alpha = "abcdefghijklmnopqrstuvwxyz";
struct segment_tree {
  const long INF = 2 * 1e9;
  long n = 1;
  long data[300013];
  segment_tree(int a) {
    while (n < a)
      n *= 2;
    for (int i = 0; i < 2 * n; i++)
      data[i] = INF;
  }
  // segment treeの大きさ取得
  long nreturn() { return n; }
  // 0-indexでk番目の値をaに変更
  void change(long k, long a) {
    k += n - 1;
    data[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      data[k] = min(data[2 * k + 1], data[2 * k + 2]);
      // cout << "data" << k << "is changed to" << data[k] << endl;
    }
    return;
  }
  // 区間[a,b)の最小値:呼ぶときはrmq(a,b,0,0,n)
  long rmq(long a, long b, long k, long l, long r) {
    if (a >= r || b <= l)
      return INF;
    if (a <= l && b >= r)
      return data[k];
    long teml = rmq(a, b, 2 * k + 1, l, (l + r) / 2);
    long temr = rmq(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(teml, temr);
  }
};
int main() {
  int n;
  cin >> n;
  pair<long, long> lr[n];
  rep(i, n) cin >> lr[i].first >> lr[i].second;
  sort(lr, lr + n);
  segment_tree tree(n);
  rep(i, n) tree.change(i, lr[i].second);
  long ans = 0;
  long nn = tree.nreturn();
  for (int i = 0; i < n - 1; i++) {
    long tmp =
        max(long(0), long(1) + tree.rmq(0, i + 1, 0, 0, nn) - lr[i].first);
    tmp +=
        max(long(0), long(1) + tree.rmq(i + 1, nn, 0, 0, nn) - lr[n - 1].first);
    ans = max(ans, tmp);
    tmp = 0;
    tmp += (lr[i].second - lr[i].first) + 1;
    tmp += max(long(0), long(1) +
                            min(tree.rmq(0, i, 0, 0, nn),
                                tree.rmq(i + 1, nn, 0, 0, nn)) -
                            lr[n - 1].first);
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  /*segment_tree tree(7);
  for(int i = 0;i<7;i++) tree.change(i,i);
  for(int i = 0;i<15;i++) cout << tree.data[i] << " " ;
  cout << tree.rmq(1,2,0,0,7);*/
}