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

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

int w, n;

bool ok1(vi &sm, int st, int cri) {
  int diff = sm[st + cri] - sm[st] + cri - 1;
  return diff <= w;
}

bool ok2(vi &sm, int st, int cri, int d) {
  int diff = sm[st + cri] - sm[st] + (cri - 1) * d;
  return w <= diff;
}

bool possible(vi &sm, int d) {
  vi dp(n + 5, 0);
  dp[0] += 1;
  dp[1] += -1;
  int nw = 0;
  int last;
  rep(i, n) {
    nw += dp[i];
    if (nw) {
      last = i;
      int lb, rb;
      if (ok1(sm, i, n - i)) {
        lb = n;
      } else {
        int l = 2, r = n - i;
        while (r - l > 1) {
          int mid = (l + r) / 2;
          if (ok1(sm, i, mid)) {
            l = mid;
          } else {
            r = mid;
          }
        }
        lb = i + l;
      }
      if (ok2(sm, i, 2, d)) {
        rb = i + 2;
      } else {
        int l = 2, r = n - i;
        while (r - l > 1) {
          int mid = (l + r) / 2;
          if (ok2(sm, i, mid, d)) {
            r = mid;
          } else {
            l = mid;
          }
        }
        rb = i + r;
      }
      if (rb <= lb) {
        dp[rb] += 1;
        dp[lb + 1] -= 1;
      }
    }
  }
  if (nw + dp[n]) {
    return true;
  }
  if (sm[n] - sm[last] + (n - last - 1) <= w) {
    return true;
  } else {
    return false;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    cin >> w >> n;
    if (w + n == 0) {
      break;
    }
    vi vec(n), sm(n + 1);
    rep(i, n) { cin >> vec[i]; }
    sm[0] = 0;
    rep(i, n) { sm[i + 1] = sm[i] + vec[i]; }
    // possible(sm,1);
    int l = 0, r = w;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (possible(sm, mid)) {
        r = mid;
      } else {
        l = mid;
      }
    }
    cout << r << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define each(a, b) for (auto(a) : (b))
#define all(v) (v).begin(), (v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define cmx(x, y) x = max(x, y)
#define cmn(x, y) x = min(x, y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout << #x << " = " << (x) << endl
#define spair(p) cout << #p << ": " << p.fi << " " << p.se << endl
#define svec(v)                                                                \\
  cout << #v << ":";                                                           \\
  rep(kbrni, v.size()) cout << " " << v[kbrni];                                \\
  cout << endl
#define sset(s)                                                                \\
  cout << #s << ":";                                                           \\
  each(kbrni, s) cout << " " << kbrni;                                         \\
  cout << endl
#define smap(m)                                                                \\
  cout << #m << ":";                                                           \\
  each(kbrni, m) cout << " {" << kbrni.first << ":" << kbrni.second << "}";    \\
  cout << endl

using namespace std;

typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

template <typename V> class segtree {
private:
  int n, sz;
  vector<V> node, lazy;
  vector<bool> lazyFlag;

public:
  segtree(vector<V> &v) {
    sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1);
    lazyFlag.resize(2 * n - 1, false);
    rep(i, sz) node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }
  void eval(int k, int l, int r) {
    if (lazyFlag[k]) {
      node[k] = lazy[k];
      if (r - l > 1) {
        lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
        lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
      }
      lazyFlag[k] = false;
    }
  }
  void range(int a, int b, V x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] = x;
      lazyFlag[k] = true;
      eval(k, l, r);
    } else {
      range(a, b, x, 2 * k + 1, l, (l + r) / 2);
      range(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = min(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  V query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return numeric_limits<V>::max();
    if (a <= l && r <= b)
      return node[k];
    V vl = query(a, b, 2 * k + 1, l, (l + r) / 2),
      vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
  void print() {
    rep(i, sz) cout << query(i, i + 1) << " ";
    cout << endl;
  }
};

int w, n;

bool ok1(vi &sm, int st, int cri) {
  int diff = sm[st + cri] - sm[st] + cri - 1;
  return diff <= w;
}

bool ok2(vi &sm, int st, int cri, int d) {
  int diff = sm[st + cri] - sm[st] + (cri - 1) * d;
  return w <= diff;
}

bool possible(vi &sm, int d) {
  vi dp(n + 1, 0);
  dp[0] = 1;
  segtree<int> sg(dp);
  rep(i, n) {
    if (sg.query(i, i + 1)) {
      int lb, rb;
      if (ok1(sm, i, n - i)) {
        lb = n;
      } else {
        int l = 2, r = n - i;
        while (r - l > 1) {
          int mid = (l + r) / 2;
          if (ok1(sm, i, mid)) {
            l = mid;
          } else {
            r = mid;
          }
        }
        lb = i + l;
      }
      if (ok2(sm, i, 2, d)) {
        rb = i + 2;
      } else {
        int l = 2, r = n - i;
        while (r - l > 1) {
          int mid = (l + r) / 2;
          if (ok2(sm, i, mid, d)) {
            r = mid;
          } else {
            l = mid;
          }
        }
        rb = i + r;
      }
      if (rb <= lb) {
        sg.range(rb, lb + 1, 1);
      }
    }
  }
  if (sg.query(n, n + 1)) {
    return true;
  }
  rrep(i, n) {
    if (sg.query(i, i + 1)) {
      if (sm[n] - sm[i] + (n - i - 1) <= w) {
        return true;
      } else {
        return false;
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    cin >> w >> n;
    if (w + n == 0) {
      break;
    }
    vi vec(n), sm(n + 1);
    rep(i, n) { cin >> vec[i]; }
    sm[0] = 0;
    rep(i, n) { sm[i + 1] = sm[i] + vec[i]; }
    // possible(sm,1);
    int l = 0, r = w;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (possible(sm, mid)) {
        r = mid;
      } else {
        l = mid;
      }
    }
    cout << r << "\\n";
  }
  return 0;
}