Problem p03202 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define N 200005
using namespace std;
int n, a[N], cur;
class Segment_T {
  struct tree {
    int lc, rc, v, f;
    void set() {
      v = 0;
      f = 1;
    }
  } t[N * 100];
  int root, sz;
  bool flag;
  void pushdown(int x) {
    if (t[x].f) {
      if (t[x].lc)
        t[t[x].lc].set();
      if (t[x].rc)
        t[t[x].rc].set();
      t[x].f = 0;
    }
  }
  void cover(int &x, int l, int r, int L, int R) {
    if (!x)
      x = ++sz, t[x].lc = t[x].rc = t[x].v = t[x].f = 0;
    if (L <= l && R >= r) {
      t[x].set();
      return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    if (L <= mid)
      cover(t[x].lc, l, mid, L, R);
    if (R > mid)
      cover(t[x].rc, mid + 1, r, L, R);
  }
  void modify(int &x, int l, int r, int p) {
    if (!x)
      x = ++sz, t[x].lc = t[x].rc = t[x].v = t[x].f = 0;
    if (l == r) {
      if (t[x].v == cur) {
        flag = 1;
        t[x].v = 0;
      } else
        t[x].v++;
      return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    if (p <= mid)
      modify(t[x].lc, l, mid, p);
    else
      modify(t[x].rc, mid + 1, r, p);
  }

public:
  void Cover(int l, int r) { cover(root, 1, 1e9, l, r); }
  bool Modify(int x) {
    flag = 1;
    int pos = x;
    while (flag) {
      flag = 0;
      modify(root, 1, 1e9, pos);
      pos--;
      if (flag && pos == 0)
        return 0;
    }
    return 1;
  }
  void Clear() {
    sz = 0;
    root = 0;
  }
} T;
bool pd(int x) {
  cur = x;
  T.Clear();
  for (int i = 1; i <= n; i++) {
    if (a[i] <= a[i - 1]) {
      T.Cover(a[i] + 1, 1e9);
      if (!T.Modify(a[i]))
        return 0;
    }
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  bool bj = 0;
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1]) {
      bj = 1;
      break;
    }
  if (!bj) {
    puts("1");
    return 0;
  }
  // for(int i=0;i<=n;i++) cout<<pd(i)<<endl;
  int L = 0, R = n, mid;
  while (L < R) {
    // cout<<L<<" "<<R<<endl;
    mid = (L + R) / 2;
    if (!pd(mid))
      L = mid + 1;
    else
      R = mid;
  }
  printf("%d\n", L + 1);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#define N 200005
using namespace std;
int n, a[N], cur;
class Segment_T {
  struct tree {
    int lc, rc, v, f;
    void set() {
      v = 0;
      f = 1;
    }
  } t[N * 100];
  int root, sz;
  bool flag;
  void pushdown(int x) {
    if (t[x].f) {
      if (t[x].lc)
        t[t[x].lc].set();
      if (t[x].rc)
        t[t[x].rc].set();
      t[x].f = 0;
    }
  }
  void cover(int &x, int l, int r, int L, int R) {
    if (!x)
      x = ++sz, t[x].lc = t[x].rc = t[x].v = t[x].f = 0;
    if (L <= l && R >= r) {
      t[x].set();
      return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    if (L <= mid)
      cover(t[x].lc, l, mid, L, R);
    if (R > mid)
      cover(t[x].rc, mid + 1, r, L, R);
  }
  void modify(int &x, int l, int r, int p) {
    if (!x)
      x = ++sz, t[x].lc = t[x].rc = t[x].v = t[x].f = 0;
    if (l == r) {
      if (t[x].v == cur) {
        flag = 1;
        t[x].v = 0;
      } else
        t[x].v++;
      return;
    }
    pushdown(x);
    int mid = (l + r) >> 1;
    if (p <= mid)
      modify(t[x].lc, l, mid, p);
    else
      modify(t[x].rc, mid + 1, r, p);
  }

public:
  void Cover(int l, int r) { cover(root, 1, 1e9, l, r); }
  bool Modify(int x) {
    flag = 1;
    int pos = x;
    while (flag) {
      flag = 0;
      modify(root, 1, 1e9, pos);
      pos--;
      if (flag && pos == 0)
        return 0;
    }
    return 1;
  }
  void Clear() {
    sz = 0;
    root = 0;
  }
} T;
bool pd(int x) {
  cur = x;
  T.Clear();
  for (int i = 1; i <= n; i++) {
    if (a[i] <= a[i - 1]) {
      T.Cover(a[i] + 1, 1e9);
      if (!T.Modify(a[i]))
        return 0;
    }
  }
  return 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  bool bj = 0;
  for (int i = 2; i <= n; i++)
    if (a[i] <= a[i - 1]) {
      bj = 1;
      break;
    }
  if (!bj) {
    puts("1");
    return 0;
  }
  for (int i = 1; i <= 10; i++)
    if (pd(i)) {
      printf("%d\n", i + 1);
      return 0;
    }
  // for(int i=0;i<=n;i++) cout<<pd(i)<<endl;
  int L = 0, R = n, mid;
  while (L < R) {
    // cout<<L<<" "<<R<<endl;
    mid = (L + R) / 2;
    if (!pd(mid))
      L = mid + 1;
    else
      R = mid;
  }
  printf("%d\n", L + 1);
  return 0;
}