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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int x[100000], dat[100000], n, W, dp[100000], nex[100000];
priority_queue<pair<int, int>, vector<pair<int, int>>, less<pair<int, int>>> Q;
void add(int p, int q) {
  while (p <= n) {
    dat[p] += q;
    p += p & -p;
  }
}
int sum(int p) {
  int sum2 = 0;
  while (p >= 1) {
    sum2 += dat[p];
    p -= p & -p;
  }
  return sum2;
}
int Rangesum(int W1, int W2) { return sum(W2) - sum(W1 - 1); }
int hantei(int L1, int R1) {
  if (L1 == 0)
    return 2147000000;
  int E = Rangesum(L1, R1);
  E += (R1 - L1);
  return E;
}
int main() {
  while (true) {
    cin >> W >> n;
    for (int i = 0; i < 100000; i++) {
      dat[i] = 0;
      dp[i] = 999999999;
      nex[i] = -2;
    }
    if (n == 0 && W == 0)
      break;
    for (int i = 1; i <= n; i++)
      cin >> x[i];
    add(1, x[1]);
    dp[0] = 1;
    while (!Q.empty())
      Q.pop();
    Q.push(make_pair(99999999, 1));
    Q.push(make_pair(0, 0));

    for (int i = 2; i <= n - 1; i++) {
      add(i, x[i]);
      int L = 1, R = i, M = 0;
      while (true) {
        M = (L + R) / 2;
        int V1 = hantei(M, i);
        int V2 = hantei(M - 1, i);
        if (V1 <= W && V2 > W) {
          int j = M;
          while (true) {
            int KO = i - j;
            int V1 = hantei(j, i);
            dp[i] = min(dp[i], max(dp[j - 1], ((W - V1) + KO - 1) / KO + 1));
            j = nex[j - 1] + 1;
            if (j == -1 || j >= i)
              break;
          }
          break;
        }
        if (V1 > W)
          L = M;
        if (V2 <= W)
          R = M;
      }
      while (!Q.empty()) {
        if (dp[i] < Q.top().first) {
          nex[Q.top().second] = i;
          Q.pop();
        } else {
          break;
        }
      }
      Q.push(make_pair(dp[i], i));
    }
    add(n, x[n]);
    int minx = 999999999;
    for (int i = 0; i < n; i++) {
      if (Rangesum(i + 1, n) + (n - (i + 1)) <= W)
        minx = min(minx, dp[i]);
    }
    cout << minx << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int x[100000], dat[100000], n, W, dp[100000];
void add(int p, int q) {
  while (p <= n) {
    dat[p] += q;
    p += p & -p;
  }
}
int sum(int p) {
  int sum2 = 0;
  while (p >= 1) {
    sum2 += dat[p];
    p -= p & -p;
  }
  return sum2;
}
int Rangesum(int W1, int W2) { return sum(W2) - sum(W1 - 1); }
int hantei(int L1, int R1) {
  if (L1 == 0)
    return 2147000000;
  int E = Rangesum(L1, R1);
  E += (R1 - L1);
  return E;
}
int main() {
  while (true) {
    cin >> W >> n;
    for (int i = 0; i < 100000; i++) {
      dat[i] = 0;
      dp[i] = 999999999;
    }
    if (n == 0 && W == 0)
      break;
    for (int i = 1; i <= n; i++)
      cin >> x[i];
    add(1, x[1]);
    dp[0] = 1;
    for (int i = 2; i <= n - 1; i++) {
      add(i, x[i]);
      int L = 1, R = i, M = 0;
      while (true) {
        M = (L + R) / 2;
        int V1 = hantei(M, i);
        int V2 = hantei(M - 1, i);
        if (V1 <= W && V2 > W) {
          int ret = dp[M - 1];
          for (int j = M; j < i; j++) {
            if (j != M && ret <= dp[j - 1])
              continue;
            int KO = i - j;
            int V1 = hantei(j, i);
            dp[i] = min(dp[i], max(dp[j - 1], ((W - V1) + KO - 1) / KO + 1));
          }
          break;
        }
        if (V1 > W)
          L = M;
        if (V2 <= W)
          R = M;
      }
    }
    add(n, x[n]);
    int minx = 999999999;
    for (int i = 0; i < n; i++) {
      if (Rangesum(i + 1, n) + (n - (i + 1)) <= W)
        minx = min(minx, dp[i]);
    }
    cout << minx << endl;
  }
  return 0;
}