Problem p00912 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 50000
#define INF 100000000
#define MOD 1000000007
#define RANK 1000000000
#define EPS 1e-10
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> IP;

int w, n;
int bit[MAX_N + 1];
int x[MAX_N];
int sum[MAX_N + 1];

int bsum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i) {
  while (i <= n) {
    bit[i]++;
    i += i & -i;
  }
}

bool check(int le) {
  memset(bit, 0, sizeof(bit));

  int prest = -1;
  int s, e, mid;
  int st, en;
  int v, q;

  for (int i = 1; i < n; i++) {
    s = prest;
    e = i;
    while (e - s > 1) {
      mid = (s + e) / 2;
      if (sum[i + 1] - sum[mid] - 1 > w) {
        s = mid;
      } else {
        e = mid;
      }
    }
    st = e;
    prest = st - 1;
    if (i == n - 1) {
      en = i;
    } else {
      s = -1;
      e = i;
      while (e - s > 1) {
        mid = (s + e) / 2;
        if (w - sum[i + 1] + sum[mid] + 1 > (le - 1) * (i - mid)) {
          e = mid;
        } else {
          s = mid;
        }
      }
      en = s;
    }
    if (st > en)
      continue;
    if (st == 0)
      q = 1;
    else
      q = bsum(en) - bsum(st - 1);
    if (q) {
      add(i + 1);
      if (i == n - 1)
        return true;
    }
  }
  return false;
}

int solve() {
  rep(i, n) scanf("%d", x + i);
  rep(i, n) {
    if (i == 0) {
      sum[i + 1] = x[i] + 1;
    } else {
      sum[i + 1] = sum[i] + x[i] + 1;
    }
  }
  int ss = 0, ee = w, mid;
  while (ee - ss > 1) {
    mid = (ss + ee) / 2;
    if (check(mid)) {
      ee = mid;
    } else {
      ss = mid;
    }
  }
  printf("%d\\n", ee);
}

int main() {
  while (scanf("%d%d", &w, &n)) {
    if (w == 0 && n == 0)
      break;
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define MAX_N 50000
#define INF 100000000
#define MOD 1000000007
#define RANK 1000000000
#define EPS 1e-10
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> IP;

int w, n;
int bit[MAX_N + 1];
int x[MAX_N];
int sum[MAX_N + 1];

int bsum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i) {
  while (i <= n) {
    bit[i]++;
    i += i & -i;
  }
}

bool check(int le) {
  memset(bit, 0, sizeof(bit));

  int prest = -1;
  int s, e, mid;
  int st, en;
  int v, q;

  for (int i = 1; i < n; i++) {
    s = prest;
    e = i;
    while (e - s > 1) {
      mid = (s + e) / 2;
      if (sum[i + 1] - sum[mid] - 1 > w) {
        s = mid;
      } else {
        e = mid;
      }
    }
    st = e;
    prest = st - 1;
    if (i == n - 1) {
      en = i;
    } else {
      s = -1;
      e = i;
      while (e - s > 1) {
        mid = (s + e) / 2;
        if ((w + i - mid - sum[i + 1] + sum[mid]) / (i - mid) + 1 > le) {
          e = mid;
        } else {
          s = mid;
        }
      }
      en = s;
    }
    if (st > en)
      continue;
    if (st == 0)
      q = 1;
    else
      q = bsum(en) - bsum(st - 1);
    if (q) {
      add(i + 1);
      if (i == n - 1)
        return true;
    }
  }
  return false;
}

int solve() {
  rep(i, n) scanf("%d", x + i);
  rep(i, n) {
    if (i == 0) {
      sum[i + 1] = x[i] + 1;
    } else {
      sum[i + 1] = sum[i] + x[i] + 1;
    }
  }
  int ss = 0, ee = w, mid;
  while (ee - ss > 1) {
    mid = (ss + ee) / 2;
    if (check(mid)) {
      ee = mid;
    } else {
      ss = mid;
    }
  }
  printf("%d\\n", ee);
}

int main() {
  while (scanf("%d%d", &w, &n)) {
    if (w == 0 && n == 0)
      break;
    solve();
  }
}