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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pairInt;
typedef tuple<int, int, int> tuple3Int;
#define FOR(i, n) for (int i = 0; i < int(n); i++)
#define FOR1(i, m, n) for (int i = int(m); i < int(n); i++)
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define MIN(a, b) ((a) <= (b) ? (a) : (b))

ll pow(ll base, uint exp) {
  if (exp == 0)
    return 1;

  if (exp % 2 == 0) {
    return pow(base * base, exp / 2);
  } else {
    return base * pow(base * base, exp / 2);
  }
}

int main(int argc, char *argv[]) {
  int N, S;
  vector<vector<int>> vecs;

  scanf("%d%d", &N, &S);
  vecs.emplace_back(N);

  FOR(i, N) {
    scanf("%d", &vecs[0][i]);
    if (vecs[0][i] >= S) {
      printf("1\\n");
      return 0;
    }
  }

  int step = 1;
  int k;
  for (k = 1; 2 * step <= N; k++) {
    vecs.emplace_back(N);
    FOR(j, N - 2 * step + 1) {
      vecs[k][j] = vecs[k - 1][j] + vecs[k - 1][j + step];
      if (vecs[k][j] >= S) {
        goto LOOP_END;
      }
    }
    step *= 2;
  }
LOOP_END:
  k -= 1;

  vector<int> vec(vecs[k]);
  for (k--; k >= 0; k--) {
    if (step + pow(2, k) <= N) {
      vector<int> tmp(N);
      FOR(i, N - step - pow(2, k) + 1) {
        tmp[i] = vec[i] + vecs[k][i + step];
        if (tmp[i] >= S) {
          goto CONTINUE;
        }
      }
      step += pow(2, k);
      vec.clear();
      copy(tmp.begin(), tmp.end(), back_inserter(vec));
    }
  CONTINUE:;
  }

  if (step == N) {
    printf("0\\n");
  } else {
    printf("%d\\n", step + 1);
  }

  // for (int i = step + 1; i <= MIN(2 * step, N); i++) {
  //   FOR(j, N - i + 1) {
  //     b[j] += a[j + i - 1];
  //     if (b[j] >= S) {
  //       printf("%d\\n", i);
  //       goto EXIT;
  //     }
  //   }
  // }
  // printf("0\\n");

EXIT:
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pairInt;
typedef tuple<int, int, int> tuple3Int;
#define FOR(i, n) for (int i = 0; i < int(n); i++)
#define FOR1(i, m, n) for (int i = int(m); i < int(n); i++)
#define MAX(a, b) ((a) >= (b) ? (a) : (b))
#define MIN(a, b) ((a) <= (b) ? (a) : (b))

ll pow(ll base, uint exp) {
  if (exp == 0)
    return 1;

  if (exp % 2 == 0) {
    return pow(base * base, exp / 2);
  } else {
    return base * pow(base * base, exp / 2);
  }
}

int main(int argc, char *argv[]) {
  int N, S;
  vector<int> a;

  scanf("%d%d", &N, &S);
  a.resize(N);

  FOR(i, N) {
    scanf("%d", &a[i]);
    if (a[i] >= S) {
      printf("1\\n");
      return 0;
    }
  }

  vector<int> b(a);
  vector<int> c;
  c.resize(N);
  int step = 1;
  for (int k = 1; 2 * step <= N; k++) {
    FOR(j, N - 2 * step + 1) {
      c[j] = b[j] + b[j + step];
      if (c[j] >= S) {
        goto LOOP_END;
      }
    }
    b.clear();
    copy(c.begin(), c.end(), back_inserter(b));
    step *= 2;
  }
LOOP_END:

  for (int i = step + 1; i <= MIN(2 * step, N); i++) {
    FOR(j, N - i + 1) {
      b[j] += a[j + i - 1];
      if (b[j] >= S) {
        printf("%d\\n", i);
        goto EXIT;
      }
    }
  }
  printf("0\\n");

EXIT:
  return 0;
}