Problem p03736 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdint>
#include <iostream>

int const MAXN = 2e5;
int64_t const MAXNN = 1 << 18;
int64_t const INF = 1e12;
int N, Q, A, B, x;

struct seg_tree {
  int64_t base_;
  int64_t data_[MAXN + 1];
  int N_;

  void add_all(int64_t v) { base_ += v; }
  void init() {
    base_ = 0;
    for (N_ = 2; N_ <= N;)
      N_ <<= 1;
    std::fill_n(data_, N_ * 2 - 1, INF);
  }
  void set(int k, int64_t v) { set_inner(k, v - base_); }
  int64_t query(int a, int b) { return base_ + query_inner(a, b, 0, 0, N_); }
  void set_inner(int k, int64_t v) {
    k += N_ - 1;
    data_[k] = v;
    while (k > 0) {
      k = (k - 1) / 2;
      data_[k] = std::min(data_[k * 2 + 1], data_[k * 2 + 2]);
    }
  }
  int64_t query_inner(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return data_[k];
    int m = (l + r) / 2;
    int64_t vl = query_inner(a, b, k * 2 + 1, l, m);
    int64_t vr = query_inner(a, b, k * 2 + 2, m, r);
    return std::min(vl, vr);
  }
};

seg_tree dpm, dpp;

inline int64_t cost(int x) { return std::max(x, -x); }
inline void set(int x, int64_t v) {
  dpm.set(x, v - x);
  dpp.set(x, v + x);
}

int main() {
  int prev_x, x;
  std::cin >> N >> Q >> A >> B;
  dpm.init();
  dpp.init();
  std::cin >> x;
  set(A, cost(B - x));
  set(B, cost(A - x));
  for (int i = 1; i < Q; ++i) {
    int prev_x = x;
    std::cin >> x;
    int64_t tmp = std::min(dpm.query(1, x + 1) + x, dpp.query(x, N + 1) - x);
    dpp.add_all(cost(x - prev_x));
    dpm.add_all(cost(x - prev_x));
    set(prev_x, tmp);
  }
  int64_t ans = INF;
  for (int i = 1; i <= N; ++i)
    ans = std::min(ans, dpp.data_[i + dpp.N_ - 1] + dpp.base_ - i);
  std::cout << ans << std::endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdint>
#include <iostream>

int const MAXN = 2e5;
int64_t const MAXNN = 1 << 18;
int64_t const INF = 1e12;
int N, Q, A, B, x;

struct seg_tree {
  int64_t base_;
  int64_t data_[MAXNN * 2 - 1];
  int N_;

  void add_all(int64_t v) { base_ += v; }
  void init() {
    base_ = 0;
    for (N_ = 2; N_ <= N;)
      N_ <<= 1;
    std::fill_n(data_, N_ * 2 - 1, INF);
  }
  void set(int k, int64_t v) { set_inner(k, v - base_); }
  int64_t query(int a, int b) { return base_ + query_inner(a, b, 0, 0, N_); }
  void set_inner(int k, int64_t v) {
    k += N_ - 1;
    data_[k] = v;
    while (k > 0) {
      k = (k - 1) / 2;
      data_[k] = std::min(data_[k * 2 + 1], data_[k * 2 + 2]);
    }
  }
  int64_t query_inner(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return INF;
    if (a <= l && r <= b)
      return data_[k];
    int m = (l + r) / 2;
    int64_t vl = query_inner(a, b, k * 2 + 1, l, m);
    int64_t vr = query_inner(a, b, k * 2 + 2, m, r);
    return std::min(vl, vr);
  }
};

seg_tree dpm, dpp;

inline int64_t cost(int x) { return std::max(x, -x); }
inline void set(int x, int64_t v) {
  dpm.set(x, v - x);
  dpp.set(x, v + x);
}

int main() {
  int prev_x, x;
  std::cin >> N >> Q >> A >> B;
  dpm.init();
  dpp.init();
  std::cin >> x;
  set(A, cost(B - x));
  set(B, cost(A - x));
  for (int i = 1; i < Q; ++i) {
    int prev_x = x;
    std::cin >> x;
    int64_t tmp = std::min(dpm.query(1, x + 1) + x, dpp.query(x, N + 1) - x);
    dpp.add_all(cost(x - prev_x));
    dpm.add_all(cost(x - prev_x));
    set(prev_x, tmp);
  }
  int64_t ans = INF;
  for (int i = 1; i <= N; ++i)
    ans = std::min(ans, dpp.data_[i + dpp.N_ - 1] + dpp.base_ - i);
  std::cout << ans << std::endl;
  return 0;
}