Problem p01659 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define PB push_back
#define ITR ::iterator

#define MOD 1000000009LL
#define EPS 1e-8

const int MAX_N = 1 << 18;

int n, ar[MAX_N], dat[MAX_N * 2 - 1];
int dummy;

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < n; ++i) {
    dat[i + n - 1] = i;
  }
  for (int i = n - 2; i >= 0; --i) {
    int vl = dat[i * 2 + 1], vr = dat[i * 2 + 2];
    dat[i] = ar[vl] > ar[vr] ? vr : vl;
  }
  dummy = n - 2;
}

int query(int a, int b, int k = 0, int l = 0, int r = n) {
  if (r <= a || b <= l)
    return dummy;
  if (a <= l && r <= b)
    return dat[k];
  int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  if (ar[vl] > ar[vr])
    return vr;
  return vl;
}

int ans;
void solve(int l, int r) {
  stack<pii> st;
  st.push(pii(l, r));
  while (!st.empty()) {
    l = st.top().first;
    r = st.top().second;
    st.pop();

    int x = query(l, r);
    if (ar[x] != INT_MAX) {
      ++ans;
      while (1) {
        int y = query(l, r);
        if (ar[x] != ar[y]) {
          break;
        }
        st.push(pii(l, y));
        l = y + 1;
      }
      st.push(pii(l, r));
    }
  }
}

int main() {
  for (int i = 0; i < MAX_N; ++i) {
    ar[i] = INT_MAX;
  }

  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &ar[i]);
  }
  init(N + 10);
  solve(0, N);
  printf("%d\\n", ans);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define PB push_back
#define ITR ::iterator

#define MOD 1000000009LL
#define EPS 1e-8

const int MAX_N = 1 << 18;

int n, ar[MAX_N], dat[MAX_N * 2 - 1];
int dummy;

void init(int n_) {
  n = 1;
  while (n < n_)
    n *= 2;
  for (int i = 0; i < n; ++i) {
    dat[i + n - 1] = i;
  }
  for (int i = n - 2; i >= 0; --i) {
    int vl = dat[i * 2 + 1], vr = dat[i * 2 + 2];
    dat[i] = ar[vl] > ar[vr] ? vr : vl;
  }
  dummy = n - 2;
}

int query(int a, int b, int k = 0, int l = 0, int r = n) {
  if (r <= a || b <= l)
    return dummy;
  if (a <= l && r <= b)
    return dat[k];
  int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  if (ar[vl] > ar[vr])
    return vr;
  return vl;
}

int ans;
void solve(int l, int r) {
  while (1) {
    int x = query(l, r);
    if (ar[x] == INT_MAX) {
      return;
    }
    ++ans;
    while (1) {
      int y = query(l, r);
      if (ar[x] != ar[y]) {
        break;
      }
      solve(l, y);
      l = y + 1;
    }
  }
}

int main() {
  for (int i = 0; i < MAX_N; ++i) {
    ar[i] = INT_MAX;
  }

  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &ar[i]);
  }
  init(N + 10);
  solve(0, N);
  printf("%d\\n", ans);
}