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

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1 << 30;
const long long LINF = 1e18;
const int EPS = 1e-8;
#define PB push_back
#define mk make_pair
#define fr first
#define sc second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
typedef pair<ll, int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll A[300010] = {};
Pii sum[300010] = {};
int main() {
  int N;
  scanf("%d", &N);

  rep(i, N) {
    scanf("%lld", &A[i]);
    A[N + i] = A[i];
    A[2 * N + i] = A[i];
  }

  sum[0].fr = 0;
  sum[3 * N].sc = 3 * N;
  rep(i, 3 * N) {
    sum[i + 1].fr += sum[i].fr + A[i];
    sum[i].sc = i;
  }

  /*rep(i,3*N+1){
          printf("%lld ",sum[i].fr);
  }
  puts("");

  rep(i,3*N+1){
          printf("%lld ",sum[i].sc);
  }
  puts("");*/

  ll ans = -LINF;

  rep(i, N) {
    ll l = 0;
    ll r = sum[N].fr - sum[0].fr;
    int p, q, w;
    while (l + 1 != r) {

      ll m = (l + r) / 2;
      // printf("[%d , %d] => %d\\n",l,r,m);
      Pii tmp;
      tmp.fr = m + sum[i].fr;
      tmp.sc = 0;
      Pii res1 = *lower_bound(sum + i, sum + i + N, tmp);
      tmp.fr = m + res1.fr;
      Pii res2 = *lower_bound(sum + res1.sc, sum + res1.sc + N, tmp);
      tmp.fr = m + res2.fr;
      Pii res3 = *lower_bound(sum + res2.sc, sum + res2.sc + N, tmp);
      // printf("res1(%lld , %d)\\n",res1.fr,res1.sc);
      // printf("res2(%lld , %d)\\n",res2.fr,res2.sc);
      // printf("res3(%lld , %d)\\n",res3.fr,res3.sc);

      if (res3.sc <= i + N) {
        l = m;
      } else {
        r = m;
      }
    }
    Pii _tmp;
    _tmp.fr = l + sum[i].fr;
    _tmp.sc = 0;
    Pii _res1 = *lower_bound(sum + i, sum + i + N, _tmp);
    _tmp.fr = l + _res1.fr;
    Pii _res2 = *lower_bound(sum + _res1.sc, sum + _res1.sc + N, _tmp);
    _tmp.fr = l + _res2.fr;
    Pii _res3 = *lower_bound(sum + _res2.sc, sum + _res2.sc + N, _tmp);
    p = _res1.sc;
    q = _res2.sc;
    w = _res3.sc;
    // printf("[%d , %d] => %lld %lld
    // %lld\\n",l,r,sum[p].fr-sum[i].fr,sum[q].fr-sum[p].fr,sum[w].fr-sum[q].fr);

    ans = max(ans, min(sum[p].fr - sum[i].fr,
                       min(sum[q].fr - sum[p].fr, sum[w].fr - sum[q].fr)));
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1 << 30;
const long long LINF = 1e18;
const int EPS = 1e-8;
#define PB push_back
#define mk make_pair
#define fr first
#define sc second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
typedef pair<ll, int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
ll A[300000] = {};
Pii sum[300000] = {};
int main() {
  int N;
  scanf("%d", &N);

  rep(i, N) {
    scanf("%lld", &A[i]);
    A[N + i] = A[i];
    A[2 * N + i] = A[i];
  }

  sum[0].fr = 0;
  sum[3 * N].sc = 3 * N;
  rep(i, 3 * N) {
    sum[i + 1].fr += sum[i].fr + A[i];
    sum[i].sc = i;
  }

  /*rep(i,3*N+1){
          printf("%lld ",sum[i].fr);
  }
  puts("");

  rep(i,3*N+1){
          printf("%lld ",sum[i].sc);
  }
  puts("");*/

  ll ans = -LINF;

  rep(i, N) {
    int l = 0;
    int r = sum[N].fr - sum[0].fr;
    int p, q, w;
    while (l + 1 != r) {

      int m = (l + r) / 2;
      // printf("[%d , %d] => %d\\n",l,r,m);
      Pii tmp;
      tmp.fr = m + sum[i].fr;
      tmp.sc = 0;
      Pii res1 = *lower_bound(sum + i, sum + i + N - 1, tmp);
      tmp.fr = m + res1.fr;
      Pii res2 = *lower_bound(sum + res1.sc, sum + res1.sc + N - 1, tmp);
      tmp.fr = m + res2.fr;
      Pii res3 = *lower_bound(sum + res2.sc, sum + res2.sc + N - 1, tmp);
      // printf("res1(%lld , %d)\\n",res1.fr,res1.sc);
      // printf("res2(%lld , %d)\\n",res2.fr,res2.sc);
      // printf("res3(%lld , %d)\\n",res3.fr,res3.sc);

      if (res3.sc <= i + N) {
        l = m;
      } else {
        r = m;
      }
    }
    Pii _tmp;
    _tmp.fr = l + sum[i].fr;
    _tmp.sc = 0;
    Pii _res1 = *lower_bound(sum + i, sum + i + N - 1, _tmp);
    _tmp.fr = l + _res1.fr;
    Pii _res2 = *lower_bound(sum + _res1.sc, sum + _res1.sc + N - 1, _tmp);
    _tmp.fr = l + _res2.fr;
    Pii _res3 = *lower_bound(sum + _res2.sc, sum + _res2.sc + N - 1, _tmp);
    p = _res1.sc;
    q = _res2.sc;
    w = _res3.sc;
    // printf("[%d , %d] => %lld %lld
    // %lld\\n",l,r,sum[p].fr-sum[i].fr,sum[q].fr-sum[p].fr,sum[w].fr-sum[q].fr);

    ans = max(ans, min(sum[p].fr - sum[i].fr,
                       min(sum[q].fr - sum[p].fr, sum[w].fr - sum[q].fr)));
  }

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

  return 0;
}