Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long i64;
const int inf = (int)1.05e9;

int construct_tree(int v, vector<int> &as, vector<vector<int>> &tree) {
  int h = as[v];
  int it = v + 1;

  while (it < (int)as.size() && h < as[it]) {
    tree[v].push_back(it);
    int n_it = construct_tree(it, as, tree);
    it = n_it;
  }

  return it;
}

int down(int from, int to, int v, int k) {
  for (int i = 0; i < (from - to); ++i) {
    if (v == 1)
      return 1;
    v = (v + k - 1) / k;
  }
  return v;
}

int compute_values(int v, vector<int> &as, vector<vector<int>> &tree, int k) {
  vector<pair<int, int>> buffer;

  for (int w_it = tree[v].size() - 1; w_it >= 0; --w_it) {
    int w = tree[v][w_it];
    int val = compute_values(w, as, tree, k);
    buffer.push_back({as[w], val});
  }

  while (buffer.size() > 1) {

    int sz = buffer.size();
    auto front = buffer[sz - 1];
    int fh = front.first, fv = front.second;
    auto next = buffer[sz - 2];
    int nh = next.first, nv = next.second;

    buffer.pop_back();
    buffer.pop_back();
    assert(fh >= nh);
    buffer.push_back({nh, down(fh, nh, fv, k) + nv});
  }

  if (buffer.size() > 0) {
    int fh = buffer[0].first;
    int fv = buffer[0].second;
    return down(fh, as[v], fv, k);
  } else {
    return 1;
  }
}

int main() {
  int n;
  vector<int> as;

  scanf("%d", &n);
  as.resize(n + 1);
  as[0] = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &as[i + 1]);
  }

  vector<vector<int>> tree(n + 1);
  construct_tree(0, as, tree);

  int lo = 0, hi = 2 * n;

  while (hi - lo > 1) {

    int mid = (hi + lo) / 2;

    int val = compute_values(0, as, tree, mid);
    bool ok = (val <= 1);

    if (ok) {
      hi = mid;
    } else {
      lo = mid;
    }
  }

  int ans = hi;

  printf("%d\n", ans);

  return 0;
}

/* waffle */


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef long long i64;
const int inf = (int)1.05e9;

int construct_tree(int v, vector<int> &as, vector<vector<int>> &tree) {
  int h = as[v];
  int it = v + 1;

  while (it < (int)as.size() && h < as[it]) {
    tree[v].push_back(it);
    int n_it = construct_tree(it, as, tree);
    it = n_it;
  }

  return it;
}

int down(int from, int to, int v, int k) {
  if (k == 1)
    return v;
  for (int i = 0; i < (from - to); ++i) {
    if (v == 1)
      return 1;
    v = (v + k - 1) / k;
  }
  return v;
}

int compute_values(int v, vector<int> &as, vector<vector<int>> &tree, int k) {
  vector<pair<int, int>> buffer;

  for (int w_it = tree[v].size() - 1; w_it >= 0; --w_it) {
    int w = tree[v][w_it];
    int val = compute_values(w, as, tree, k);
    buffer.push_back({as[w], val});
  }

  while (buffer.size() > 1) {

    int sz = buffer.size();
    auto front = buffer[sz - 1];
    int fh = front.first, fv = front.second;
    auto next = buffer[sz - 2];
    int nh = next.first, nv = next.second;

    buffer.pop_back();
    buffer.pop_back();
    assert(fh >= nh);
    buffer.push_back({nh, down(fh, nh, fv, k) + nv});
  }

  if (buffer.size() > 0) {
    int fh = buffer[0].first;
    int fv = buffer[0].second;
    return down(fh, as[v], fv, k);
  } else {
    return 1;
  }
}

int main() {
  int n;
  vector<int> as;

  scanf("%d", &n);
  as.resize(n + 1);
  as[0] = 0;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &as[i + 1]);
  }

  vector<vector<int>> tree(n + 1);
  construct_tree(0, as, tree);

  int lo = 0, hi = 2 * n;

  while (hi - lo > 1) {

    int mid = (hi + lo) / 2;

    int val = compute_values(0, as, tree, mid);
    bool ok = (val <= 1);

    if (ok) {
      hi = mid;
    } else {
      lo = mid;
    }
  }

  int ans = hi;

  printf("%d\n", ans);

  return 0;
}

/* waffle */