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

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif

#define _USE_MATH_DEFINES

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cinttypes>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    is >> *i;
  return is;
}
struct fast_io {
  fast_io &operator>>(int &target) {
    scanf("%" PRId32, &target);
    return *this;
  }
  fast_io &operator>>(unsigned int &target) {
    scanf("%" PRIu32, &target);
    return *this;
  }
  fast_io &operator>>(long long &target) {
    scanf("%" PRId64, &target);
    return *this;
  }
  fast_io &operator>>(unsigned long long &target) {
    scanf("%" PRIu64, &target);
    return *this;
  }
  template <typename T> fast_io &operator>>(vector<T> &target) {
    for (auto &value : target) {
      *this >> value;
    }
    return *this;
  }
  fast_io &operator<<(const int value) {
    printf("%" PRId32, value);
    return *this;
  }
  fast_io &operator<<(const unsigned int value) {
    printf("%" PRIu32, value);
    return *this;
  }
  fast_io &operator<<(const long long value) {
    printf("%" PRId64, value);
    return *this;
  }
  fast_io &operator<<(const unsigned long long value) {
    printf("%" PRIu64, value);
    return *this;
  }
  fast_io &operator<<(const string &value) {
    printf("%s", value.c_str());
    return *this;
  }
  template <typename T> fast_io &operator<<(const vector<T> &target) {
    for (auto &value : target) {
      *this << value << " ";
    }
    return *this;
  }
};
fast_io fio;

template <class ForwardIt, class UnaryPredicate>
ForwardIt first_true(ForwardIt first, ForwardIt last,
                     UnaryPredicate predicate) {
  ForwardIt it;
  typename std::iterator_traits<ForwardIt>::difference_type count, step;
  count = std::distance(first, last);
  while (count > 0) {
    it = first;
    step = count / 2;
    std::advance(it, step);
    if (!predicate(*it)) {
      first = ++it;
      count -= step + 1;
    } else
      count = step;
  }
  return first;
}

#define MOD 1000000007
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define sort_all(x) sort(all(x))
#define make_unique(x) (x).erase(unique(all(x)), (x).end())

int m_add[300000], m_sub[300000];
vector<int> step(vector<int> inp) {
  vector<int> ans(inp.size(), 0);
  for (int i = 0; i < inp.size(); i++) {
    int d = inp[i];
    int mind = i - d, maxd = i + d;
    mind = max(0, mind);
    maxd = min((int)inp.size() - 1, maxd);
    m_add[mind]++;
    m_sub[maxd]++;
  }

  int val = 0;
  for (int i = 0; i < inp.size(); i++) {
    val += m_add[i];
    ans[i] = val;
    val -= m_sub[i];
  }

  for (int i = 0; i < inp.size(); i++) {
    m_add[i] = m_sub[i] = 0;
  }

  return ans;
}

int main() {

#if defined(_DEBUG)
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, k;
  fio >> n >> k;

  vector<int> vec(n);
  fio >> vec;

  for (int i = 0; i < k; i++) {
    vec = step(vec);
    bool any = false;
    for (int x : vec) {
      if (x != n)
        any = true;
    }
    if (!any)
      break;
  }

  fio << vec;

  return 0;
}

Error: TLE

Fixed submission:

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif

#define _USE_MATH_DEFINES

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cinttypes>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    os << *i << (i == end(v) - 1 ? "" : " ");
  return os;
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto i = begin(v); i != end(v); i++)
    is >> *i;
  return is;
}
struct fast_io {
  fast_io &operator>>(int &target) {
    scanf("%" PRId32, &target);
    return *this;
  }
  fast_io &operator>>(unsigned int &target) {
    scanf("%" PRIu32, &target);
    return *this;
  }
  fast_io &operator>>(long long &target) {
    scanf("%" PRId64, &target);
    return *this;
  }
  fast_io &operator>>(unsigned long long &target) {
    scanf("%" PRIu64, &target);
    return *this;
  }
  template <typename T> fast_io &operator>>(vector<T> &target) {
    for (auto &value : target) {
      *this >> value;
    }
    return *this;
  }
  fast_io &operator<<(const int value) {
    printf("%" PRId32, value);
    return *this;
  }
  fast_io &operator<<(const unsigned int value) {
    printf("%" PRIu32, value);
    return *this;
  }
  fast_io &operator<<(const long long value) {
    printf("%" PRId64, value);
    return *this;
  }
  fast_io &operator<<(const unsigned long long value) {
    printf("%" PRIu64, value);
    return *this;
  }
  fast_io &operator<<(const string &value) {
    printf("%s", value.c_str());
    return *this;
  }
  template <typename T> fast_io &operator<<(const vector<T> &target) {
    for (auto &value : target) {
      *this << value << " ";
    }
    return *this;
  }
};
fast_io fio;

template <class ForwardIt, class UnaryPredicate>
ForwardIt first_true(ForwardIt first, ForwardIt last,
                     UnaryPredicate predicate) {
  ForwardIt it;
  typename std::iterator_traits<ForwardIt>::difference_type count, step;
  count = std::distance(first, last);
  while (count > 0) {
    it = first;
    step = count / 2;
    std::advance(it, step);
    if (!predicate(*it)) {
      first = ++it;
      count -= step + 1;
    } else
      count = step;
  }
  return first;
}

#define MOD 1000000007
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define sort_all(x) sort(all(x))
#define make_unique(x) (x).erase(unique(all(x)), (x).end())

vector<int> step(vector<int> inp) {
  vector<int> ans(inp.size(), 0);
  map<int, int> m_add, m_sub;
  for (int i = 0; i < inp.size(); i++) {
    int d = inp[i];
    int mind = i - d, maxd = i + d;
    mind = max(0, mind);
    maxd = min((int)inp.size() - 1, maxd);
    m_add[mind]++;
    m_sub[maxd]++;
  }

  int val = 0;
  for (int i = 0; i < inp.size(); i++) {
    val += m_add[i];
    ans[i] = val;
    val -= m_sub[i];
  }

  return ans;
}

int main() {

#if defined(_DEBUG)
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n, k;
  fio >> n >> k;

  vector<int> vec(n);
  fio >> vec;

  for (int i = 0; i < k; i++) {
    vec = step(vec);
    bool any = false;
    for (int x : vec) {
      if (x != n)
        any = true;
    }
    if (!any)
      break;
  }

  fio << vec;

  return 0;
}