Problem p03312 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define fi(a, b) for (int i = a; i <= b; i++)
#define fj(a, b) for (int j = a; j <= b; j++)
#define fo(a, b) for (int o = a; o <= b; o++)
#define fdi(a, b) for (int i = a; i >= b; i--)
#define fdj(a, b) for (int j = a; j >= b; j--)
#define fdo(a, b) for (int o = a; o >= b; o--)

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...)                                                               \
  while (false) {                                                              \
  }
#endif

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef long double ld;
typedef unsigned long long ull;

/////////////////////////////////

int const MAX = 2e5 + 41;

int n;
int a[MAX];
ll pref[MAX];
ll suf[MAX];

ll const INF = 1e18;
ll ans;

int cutpref(int len) {
  ll need = pref[len] / 2;
  auto it = lower_bound(pref + 1, pref + len + 1, need);
  int p = (int)(it - pref);
  if (p == len + 1) {
    p = len - 1;
  }
  return p;
}

int cutsuf(int lp) {
  ll need = suf[lp] / 2;
  auto it = lower_bound(suf + lp, suf + n + 1, need);
  int p = (int)(it - suf);
  if (p == n + 1) {
    p = n - 2;
  }
  return p;
}

ll calc(vi v) {
  v.pb(0);
  v.pb(n);
  sort(v.begin(), v.end());
  ll maxi = -INF;
  ll mini = INF;
  fi(1, sz(v) - 1) {
    maxi = max(maxi, pref[v[i]] - pref[v[i - 1]]);
    mini = min(mini, pref[v[i]] - pref[v[i - 1]]);
  }
  return maxi - mini;
}

bool inside(int x) { return (1 <= x && x <= n); }

void solve() {
  fi(1, n) { pref[i] = pref[i - 1] + a[i]; }
  fdi(n, 1) { suf[i] = suf[i + 1] - a[i]; }

  ans = INF;
  fi(2, n - 2) {
    int p1 = cutpref(i);
    int p2 = cutsuf(i + 1);
    for (int d1 = -3; d1 <= 3; d1++) {
      for (int d2 = -3; d2 <= 3; d2++) {
        int np1 = p1 + d1;
        int np2 = i;
        int np3 = p2 + d2;
        if (np1 >= np2)
          continue;
        if (np2 >= np3)
          continue;
        if (!inside(np1) || !inside(np2) || !inside(np3))
          continue;
        /*
        if (i == 4) {
                err("np1 = %d np2 = %d np3 = %d\n", np1, np2, np3);
        }
        */
        vi vp;
        vp.pb(np1);
        vp.pb(np2);
        vp.pb(np3);
        ans = min(ans, calc(vp));
      }
    }
  }

  printf("%lld\n", ans);
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  scanf("%d", &n);
  fi(1, n) { scanf("%d", &a[i]); }
  solve();

#ifdef LOCAL
  err("ELAPSED TIME: %.3Lf\n", (ld)clock() / CLOCKS_PER_SEC);
#endif

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define sz(x) (int)x.size()
#define pb push_back
#define mp make_pair
#define fi(a, b) for (int i = a; i <= b; i++)
#define fj(a, b) for (int j = a; j <= b; j++)
#define fo(a, b) for (int o = a; o <= b; o++)
#define fdi(a, b) for (int i = a; i >= b; i--)
#define fdj(a, b) for (int j = a; j >= b; j--)
#define fdo(a, b) for (int o = a; o >= b; o--)

#ifdef LOCAL
#define err(...) fprintf(stderr, __VA_ARGS__)
#else
#define err(...)                                                               \
  while (false) {                                                              \
  }
#endif

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef long double ld;
typedef unsigned long long ull;

/////////////////////////////////

int const MAX = 2e5 + 41;

int n;
int a[MAX];
ll pref[MAX];
ll suf[MAX];

ll const INF = 1e18;
ll ans;

int cutpref(int len) {
  ll need = pref[len] / 2;
  auto it = lower_bound(pref + 1, pref + len + 1, need);
  int p = (int)(it - pref);
  if (p == len + 1) {
    p = len - 1;
  }
  return p;
}

int cutsuf(int lp) {
  ll need = suf[lp] / 2;
  auto it = lower_bound(suf + lp, suf + n + 1, need);
  int p = (int)(it - suf);
  if (p == n + 1) {
    p = n - 2;
  }
  return p;
}

ll calc(vi v) {
  v.pb(0);
  v.pb(n);
  sort(v.begin(), v.end());
  ll maxi = -INF;
  ll mini = INF;
  fi(1, sz(v) - 1) {
    maxi = max(maxi, pref[v[i]] - pref[v[i - 1]]);
    mini = min(mini, pref[v[i]] - pref[v[i - 1]]);
  }
  return maxi - mini;
}

bool inside(int x) { return (1 <= x && x <= n); }

void solve() {
  fi(1, n) { pref[i] = pref[i - 1] + a[i]; }
  fdi(n, 1) { suf[i] = suf[i + 1] - a[i]; }

  ans = INF;
  fi(2, n - 2) {
    int p1 = cutpref(i);
    int p2 = cutsuf(i + 1);
    for (int d1 = -2; d1 <= 2; d1++) {
      for (int d2 = -2; d2 <= 2; d2++) {
        int np1 = p1 + d1;
        int np2 = i;
        int np3 = p2 + d2;
        if (np1 >= np2)
          continue;
        if (np2 >= np3)
          continue;
        if (!inside(np1) || !inside(np2) || !inside(np3))
          continue;
        /*
        if (i == 4) {
                err("np1 = %d np2 = %d np3 = %d\n", np1, np2, np3);
        }
        */
        vi vp;
        vp.pb(np1);
        vp.pb(np2);
        vp.pb(np3);
        ans = min(ans, calc(vp));
      }
    }
  }

  printf("%lld\n", ans);
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  scanf("%d", &n);
  fi(1, n) { scanf("%d", &a[i]); }
  solve();

#ifdef LOCAL
  err("ELAPSED TIME: %.3Lf\n", (ld)clock() / CLOCKS_PER_SEC);
#endif

  return 0;
}