Problem p02647 (C++)

Buggy submission (status: Time Limit Exceeded):

#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

template <typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T &arr, const U &v) {
  arr = v;
}
template <typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR &arr, const U &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}

int64_t N, K;
int64_t A[300000];
int64_t R[300000];
int64_t L[300000];

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);

  in >> N >> K;
  for (size_t i = 1; i <= N; i++) {
    in >> A[i];
  }

  for (int32_t iKKKK = 0; iKKKK < K; iKKKK++) {
    fill_all(R, 0);
    fill_all(L, 0);
    for (int64_t i = 1; i <= N; i++) {
      R[i] += 1;
      R[std::min(i + A[i], N + 2) + 1] -= 1;
    }
    for (int64_t i = 1; i <= N; i++) {
      L[i] -= 1;
      L[std::max(i - A[i], (int64_t)0)] += 1;
    }

    for (int64_t i = 1; i <= N; i++) {
      R[i] += R[i - 1];
    }
    for (int64_t i = 1; i <= N; i++) {
      L[i] += L[i - 1];
    }

    for (int64_t i = 1; i <= N; i++) {
      A[i] = R[i] + L[i];
    }

    bool cont = false;
    for (int64_t i = 1; i <= N; i++) {
      if (A[i] <= N) {
        cont = true;
      }
    }
    if (!cont) {
      break;
    }
  }
  for (int64_t i = 1; i < N; i++) {
    out << A[i] << ' ';
  }
  out << A[N] << endl;

  return 0;
}
#endif


Fixed submission:

#if 1
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <cstdint>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

auto &in = std::cin;
auto &out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI =
    3.141592653589793238462643383279502884197169399375105820974944;

template <typename T, typename U>
std::enable_if_t<std::rank<T>::value == 0> fill_all(T &arr, const U &v) {
  arr = v;
}
template <typename ARR, typename U>
std::enable_if_t<std::rank<ARR>::value != 0> fill_all(ARR &arr, const U &v) {
  for (auto &i : arr) {
    fill_all(i, v);
  }
}

int64_t N, K;
int64_t A[300000];
int64_t R[300000];
int64_t L[300000];

int main() {
  using std::endl;
  in.sync_with_stdio(false);
  out.sync_with_stdio(false);
  in.tie(nullptr);
  out.tie(nullptr);

  in >> N >> K;
  for (size_t i = 1; i <= N; i++) {
    in >> A[i];
  }

  for (int32_t iKKKK = 0; iKKKK < K; iKKKK++) {
    fill_all(R, 0);
    fill_all(L, 0);
    for (int64_t i = 1; i <= N; i++) {
      R[i] += 1;
      R[std::min(i + A[i], N + 2) + 1] -= 1;
    }
    for (int64_t i = 1; i <= N; i++) {
      L[i] -= 1;
      L[std::max(i - A[i], (int64_t)0)] += 1;
    }

    for (int64_t i = 1; i <= N; i++) {
      R[i] += R[i - 1];
    }
    for (int64_t i = 1; i <= N; i++) {
      L[i] += L[i - 1];
    }

    for (int64_t i = 1; i <= N; i++) {
      A[i] = R[i] + L[i];
    }

    bool cont = false;
    for (int64_t i = 1; i <= N; i++) {
      if (A[i] < N) {
        cont = true;
      }
    }
    if (!cont) {
      break;
    }
  }
  for (int64_t i = 1; i < N; i++) {
    out << A[i] << ' ';
  }
  out << A[N] << endl;

  return 0;
}
#endif