Problem p02877 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

template <class T> inline void read(T &x) {
  static char ch;
  while (!isdigit(ch = getchar()))
    ;
  x = ch - '0';
  while (isdigit(ch = getchar()))
    x = x * 10 + ch - '0';
}

template <class T> inline void tense(T &x, const T &y) {
  if (x > y)
    x = y;
}

template <class T> inline void relax(T &x, const T &y) {
  if (x < y)
    x = y;
}

typedef std::pair<int, int> pii;

const int MaxN = 2e5 + 5;

struct node {
  int l, r, val;

  node() {}
  node(int a, int b, int c) : l(a), r(b), val(c) {}

  inline bool operator<(const node &rhs) const {
    if (val != rhs.val)
      return val > rhs.val;
    if (r != rhs.r)
      return r < rhs.r;
    return l > rhs.l;
  }

  inline void operator-=(const node &rhs) {
    if (rhs.l <= l && r <= rhs.r)
      return (void)(r = l - 1);
    if (rhs.l >= l)
      tense(r, rhs.l - 1);
    if (rhs.r <= r)
      relax(l, rhs.r + 1);
  }
};

int n, a[MaxN];
std::vector<node> f[MaxN];

int main() {
  freopen("operate.in", "r", stdin);
  freopen("operate.out", "w", stdout);

  read(n);
  for (int i = 1; i <= n; ++i)
    read(a[i]);

  f[0].push_back(node(0, 0, 0));
  for (int i = 1; i <= n + 1; ++i) {
    std::vector<node> cur;

    for (node lst : f[i - 1]) {
      int l = lst.l, v = lst.val;
      int tmin = std::min(l - 1, a[i] - a[i - 1] + l - 1);
      int tmax = std::max(l - 1, a[i] - a[i - 1] + l - 1);
      tense(tmax, a[i]);

      if (tmin >= 0)
        cur.push_back(node(0, tmin, v + 2));
      if (tmin < tmax && tmax >= 0)
        cur.push_back(node(std::max(0, tmin + 1), tmax, v + 1));
      if (tmax < a[i])
        cur.push_back(node(tmax + 1, a[i], v));
    }

    std::sort(cur.begin(), cur.end());
    for (int j = 0; j < (int)cur.size(); ++j) {
      for (int k = 0; k < j; ++k)
        cur[k] -= cur[j];
    }

    for (int j = 0; j < (int)cur.size(); ++j)
      if (cur[j].l <= cur[j].r) {
        if (!f[i].empty() && f[i].rbegin()->val == cur[j].val)
          f[i].rbegin()->r = cur[j].r;
        else
          f[i].push_back(cur[j]);
      }
  }

  std::cout << f[n + 1][0].val << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

template <class T> inline void read(T &x) {
  static char ch;
  while (!isdigit(ch = getchar()))
    ;
  x = ch - '0';
  while (isdigit(ch = getchar()))
    x = x * 10 + ch - '0';
}

template <class T> inline void tense(T &x, const T &y) {
  if (x > y)
    x = y;
}

template <class T> inline void relax(T &x, const T &y) {
  if (x < y)
    x = y;
}

typedef std::pair<int, int> pii;

const int MaxN = 2e5 + 5;

struct node {
  int l, r, val;

  node() {}
  node(int a, int b, int c) : l(a), r(b), val(c) {}

  inline bool operator<(const node &rhs) const {
    if (val != rhs.val)
      return val > rhs.val;
    if (r != rhs.r)
      return r < rhs.r;
    return l > rhs.l;
  }

  inline void operator-=(const node &rhs) {
    if (rhs.l <= l && r <= rhs.r)
      return (void)(r = l - 1);
    if (rhs.l >= l)
      tense(r, rhs.l - 1);
    if (rhs.r <= r)
      relax(l, rhs.r + 1);
  }
};

int n, a[MaxN];
std::vector<node> f[MaxN];

int main() {
  read(n);
  for (int i = 1; i <= n; ++i)
    read(a[i]);

  f[0].push_back(node(0, 0, 0));
  for (int i = 1; i <= n + 1; ++i) {
    std::vector<node> cur;

    for (node lst : f[i - 1]) {
      int l = lst.l, v = lst.val;
      int tmin = std::min(l - 1, a[i] - a[i - 1] + l - 1);
      int tmax = std::max(l - 1, a[i] - a[i - 1] + l - 1);
      tense(tmax, a[i]);

      if (tmin >= 0)
        cur.push_back(node(0, tmin, v + 2));
      if (tmin < tmax && tmax >= 0)
        cur.push_back(node(std::max(0, tmin + 1), tmax, v + 1));
      if (tmax < a[i])
        cur.push_back(node(tmax + 1, a[i], v));
    }

    std::sort(cur.begin(), cur.end());
    for (int j = 0; j < (int)cur.size(); ++j) {
      for (int k = 0; k < j; ++k)
        cur[k] -= cur[j];
    }

    for (int j = 0; j < (int)cur.size(); ++j)
      if (cur[j].l <= cur[j].r) {
        if (!f[i].empty() && f[i].rbegin()->val == cur[j].val)
          f[i].rbegin()->r = cur[j].r;
        else
          f[i].push_back(cur[j]);
      }
  }

  std::cout << f[n + 1][0].val << '\n';

  return 0;
}