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

#include <algorithm> // other
#include <bitset>
#include <cassert> // c
#include <complex>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream> // io
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <valarray>
#include <vector> // container

using namespace std;
typedef long long ll;

#define ALL(c) (c).begin(), (c).end()
#define FOR(i, l, r) for (int i = (int)l; i < (int)r; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORr(i, l, r) for (int i = (int)r - 1; i >= (int)l; --i)
#define REPr(i, n) FORr(i, 0, n)
#define EACH(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())
// debug
#define DUMP(x) cerr << #x << " = " << (x)
#define LINE() cerr << " (L" << __LINE__ << ")"

template <typename T, typename U> T pmod(T x, U M) { return (x % M + M) % M; }

template <typename T> ostream &operator<<(ostream &os, const vector<T> &as) {
  REP(i, as.size()) {
    if (i != 0)
      os << " ";
    os << as[i];
  }
  return os;
}

namespace Fenwicks {
template <typename T> class Fenwick {
public:
  int size;
  vector<T> bit;
  Fenwick() : Fenwick(0) {}
  Fenwick(int n) : size(n) { bit = vector<T>(n + 1); }
  T sum(int n) { // [0,n)
    T s = 0;
    while (n > 0) {
      s += bit[n];
      n -= n & -n; // low
    }
    return s;
  }
  void add(int i, T x) { //[0,...i...,n)
    i++;
    while (i <= size) {
      bit[i] += x;
      i += i & -i; // next
    }
  }

  // lib
  T sum(int a, int b) { // [a,b)
    return sum(b) - sum(a);
  }
};
} // namespace Fenwicks
using namespace Fenwicks;

template <typename T, typename Func> T satisfy_min(T l, T r, Func P) {
  l--;
  while (r - l > 1)
    (P((l + r) / 2) ? r : l) = (l + r) / 2;
  return r;
}

// max{i∈[l,r) | P(i)}．存在しない時、 l-1．
template <typename T, typename Func> T satisfy_max(T l, T r, Func P) {
  l--;
  while (r - l > 1)
    (P((l + r) / 2) ? l : r) = (l + r) / 2;
  return l;
}

// P(v)を満たす最小のindexまで減少
template <typename T, typename Func> T low_sliding_min(T l, T &v, Func P) {
  while (v - 1 >= l && P(v - 1))
    v--;
  return v;
}
// P(v)を満たす最大のindexまで減少
template <typename T, typename Func> T low_sliding_max(T l, T &v, Func P) {
  while (v - 1 >= l && !P(v))
    v--;
  return v;
}

template <typename T, typename Func> T sliding_up(T r, T v, Func P) {
  while (v + 1 < r && P(v + 1))
    v++;
  return v;
}

int W, N;
vector<int> xs;

vector<ll> xsum, wsum;
int can(int spM) {
  Fenwick<int> fen(N);
  // last
  FORr(i, 0, N) if (wsum[N] - wsum[i] - 1 <= W) fen.add(i, 1);

  // back "[i,N)がok" =def "1区間 [i,m)がok" かつ "*区間 [m,N)がok"
  int l = N, r = N;
  FORr(i, 0, N - 1) if (!fen.sum(i, i + 1)) {
    low_sliding_min(i + 2, l, [&](int ml) -> bool {
      ll spc = ml - i - 1, sp = (W - (xsum[ml] - xsum[i]) + spc - 1) / spc;
      return sp <= spM;
    });

    low_sliding_max(
        i + 2, r, [&](int mr) -> bool { return wsum[mr] - wsum[i] - 1 <= W; });
    // cerr <<spM<<" " << i<<" "<< l <<" " << r <<endl;
    // //[i,l)
    // int l=satisfy_min(i+2,N,[&](int ml)->bool{
    //   ll spc=ml-i-1,sp=(W-(xsum[ml]-xsum[i]) + spc-1)/spc;
    //   return sp<=spM;
    // });
    // //[i,r)
    // int r=satisfy_max(i+2,N,[&](int mr)->bool{
    //     return wsum[mr]-wsum[i]-1<=W;
    // });
    if (l <= r && fen.sum(l, r + 1) > 0)
      fen.add(i, 1);
  }
  return fen.sum(0, 1) == 1;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream cin( "in" );
  // ofstream cout( "out" );

  while (true) {
    cin >> W >> N;
    if (N == 0)
      break;
    xs = vector<int>(N);
    REP(i, N) cin >> xs[i];

    xsum = vector<ll>(N + 1);
    FOR(i, 1, N + 1) xsum[i] = xsum[i - 1] + xs[i - 1];
    wsum = vector<ll>(N + 1);
    FOR(i, 1, N + 1) wsum[i] = wsum[i - 1] + 1 + xs[i - 1];

    int res = satisfy_min(1, 100000, [&](int m) { return can(m); });
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm> // other
#include <bitset>
#include <cassert> // c
#include <complex>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream> // io
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <valarray>
#include <vector> // container

using namespace std;
typedef long long ll;

#define ALL(c) (c).begin(), (c).end()
#define FOR(i, l, r) for (int i = (int)l; i < (int)r; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORr(i, l, r) for (int i = (int)r - 1; i >= (int)l; --i)
#define REPr(i, n) FORr(i, 0, n)
#define EACH(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))
#define UNIQUE(v) v.erase(unique(ALL(v)), v.end())
// debug
#define DUMP(x) cerr << #x << " = " << (x)
#define LINE() cerr << " (L" << __LINE__ << ")"

template <typename T, typename U> T pmod(T x, U M) { return (x % M + M) % M; }

template <typename T> ostream &operator<<(ostream &os, const vector<T> &as) {
  REP(i, as.size()) {
    if (i != 0)
      os << " ";
    os << as[i];
  }
  return os;
}

namespace Fenwicks {
template <typename T> class Fenwick {
public:
  int size;
  vector<T> bit;
  Fenwick() : Fenwick(0) {}
  Fenwick(int n) : size(n) { bit = vector<T>(n + 1); }
  T sum(int n) { // [0,n)
    T s = 0;
    while (n > 0) {
      s += bit[n];
      n -= n & -n; // low
    }
    return s;
  }
  void add(int i, T x) { //[0,...i...,n)
    i++;
    while (i <= size) {
      bit[i] += x;
      i += i & -i; // next
    }
  }

  // lib
  T sum(int a, int b) { // [a,b)
    return sum(b) - sum(a);
  }
};
} // namespace Fenwicks
using namespace Fenwicks;

template <typename T, typename Func> T satisfy_min(T l, T r, Func P) {
  l--;
  while (r - l > 1)
    (P((l + r) / 2) ? r : l) = (l + r) / 2;
  return r;
}

// max{i∈[l,r) | P(i)}．存在しない時、 l-1．
template <typename T, typename Func> T satisfy_max(T l, T r, Func P) {
  l--;
  while (r - l > 1)
    (P((l + r) / 2) ? l : r) = (l + r) / 2;
  return l;
}

int W, N;
vector<int> xs;

vector<ll> xsum, wsum;

int _i, _spM;
bool satL(int ml) {
  ll spc = ml - _i - 1, sp = (W - (xsum[ml] - xsum[_i]) + spc - 1) / spc;
  return sp <= _spM;
}

vector<int> rs;

int can(int spM) {
  Fenwick<int> fen(N);
  vector<bool> ok(N);
  // last
  FORr(i, 0, N) if (wsum[N] - wsum[i] - 1 <= W) fen.add(i, 1), ok[i] = true;

  // back "[i,N)がok" =def "1区間 [i,m)がok" かつ "*区間 [m,N)がok"
  FORr(i, 0, N - 1) if (!ok[i]) {
    //[i,l)
    // int l=satisfy_min(i+2,N,[&](int ml)->bool{
    //   ll spc=ml-i-1,sp=(W-(xsum[ml]-xsum[i]) + spc-1)/spc;
    //   return sp<=spM;
    // });
    _i = i;
    _spM = spM;
    int l = satisfy_min(i + 2, N, satL);

    //[i,r)
    // int r=satisfy_max(i+2,N,[&](int mr)->bool{
    //     return wsum[mr]-wsum[i]-1<=W;
    // });
    if (l <= rs[i] && fen.sum(l, rs[i] + 1) > 0)
      fen.add(i, 1), ok[i] = true;
  }
  return ok[0];
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // ifstream cin( "in" );
  // ofstream cout( "out" );
  cerr << fixed << setprecision(20);
  cout << fixed << setprecision(20);

  while (true) {
    cin >> W >> N;
    if (N == 0)
      break;
    xs = vector<int>(N);
    REP(i, N) cin >> xs[i];

    xsum = vector<ll>(N + 1);
    FOR(i, 1, N + 1) xsum[i] = xsum[i - 1] + xs[i - 1];
    wsum = vector<ll>(N + 1);
    FOR(i, 1, N + 1) wsum[i] = wsum[i - 1] + 1 + xs[i - 1];

    rs = vector<int>(N);
    REP(i, N)
    rs[i] = satisfy_max(
        i + 2, N, [&](int mr) -> bool { return wsum[mr] - wsum[i] - 1 <= W; });

    int res = satisfy_min(1, 100000, [&](int m) { return can(m); });
    cout << res << endl;
  }
}