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

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<ll, bool> pib;

typedef vector<bool> vb;

typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

typedef vector<double> vf;

typedef queue<ll> qi;

typedef long double ld;

#define mp make_pair
#define mt make_tuple
#define pb push_back

#define rep(i, N) for (ll i = 0; i < N; i++)
#define repa(i, a, N) for (ll i = a; i < N; i++)
#define repr(i, N) for (ll i = N - 1; i >= 0; i--)

#define drep(i, N, j, M) rep(i, N) rep(j, M)
#define trep(i, N, j, M, k, L) rep(i, N) rep(j, M) rep(k, L)

#define all(A) A.begin(), A.end()
#define uni(A) A.erase(unique(all(A)), A.end())

template <class t, class u>
ostream &operator<<(ostream &os, const pair<t, u> &p) {
  return os << "{" << p.first << "," << p.second << "}";
}

template <class t> ostream &operator<<(ostream &os, const vector<t> &v) {
  os << "{";
  for (auto e : v)
    os << e << ",";
  return os << "}" << endl;
}

template <class t, class u>
ostream &operator<<(ostream &os, const unordered_map<t, u> &m) {
  os << "{";
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    os << "(" << itr->first << ", " << itr->second << "), ";
  }
  return os << "}";
}

template <class t> ostream &operator<<(ostream &os, const set<t> &m) {
  os << "{";
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    os << *itr << ", ";
  }
  return os << "}";
}

template <class t> ostream &operator<<(ostream &os, const multiset<t> &m) {
  os << "{";
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    os << *itr << ", ";
  }
  return os << "}";
}

const ll LLMAX = numeric_limits<long long>::max() / 3;
const double DMAX = numeric_limits<double>::max() / 3;

const double PI = 3.141592653589793;
// const ll MOD = 1e9 + 7;
const ll MOD = 998244353;

template <std::int_fast64_t Modulus> class modint {
  using i64 = std::int_fast64_t;

public:
  i64 a;

  constexpr modint(const i64 x = 0) noexcept : a(x % Modulus) {}
  constexpr i64 &value() noexcept { return a; }
  constexpr const i64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    i64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

template <std::int_fast64_t Modulus>
ostream &operator<<(ostream &os, const modint<Modulus> &m) {
  return os << (m.a + Modulus) % Modulus;
}

typedef modint<MOD> mod;
typedef vector<mod> vm;
typedef vector<vm> vvm;
typedef vector<vvm> vvvm;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void No() { cout << "No" << endl; }
void Yes() { cout << "Yes" << endl; }
void NO() { cout << "NO" << endl; }
void YES() { cout << "YES" << endl; }

vector<ll> fac(1e6 + 1);  // n!(mod M)
vector<ll> ifac(1e6 + 1); // k!^{M-2} (mod
ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll comb(ll a, ll b) { // aCbをmod計算
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

void pre_calc_comb() {
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i < 1e6; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD; // n!(mod M)
    ifac[i + 1] = ifac[i] * mpow(i + 1, MOD - 2) %
                  MOD; // k!^{M-2} (mod M) ←累乗にmpowを採用
  }
}

template <class T> class SegTree {
  int n;
  vector<T> data;
  T def;
  function<T(T, T)> operation;
  function<T(T, T)> update;
  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return data[k];
    else {
      T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
      T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
      return operation(c1, c2);
    }
  }

public:
  SegTree(size_t _n, T _def, function<T(T, T)> _operation,
          function<T(T, T)> _update)
      : def(_def), operation(_operation), update(_update) {
    n = 1;
    while (n < _n) {
      n *= 2;
    }
    data = vector<T>(2 * n - 1, def);
  }

  void change(int i, T x) {
    i += n - 1;
    data[i] = update(data[i], x);
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }

  T query(int a, int b) { return _query(a, b, 0, 0, n); }

  T operator[](int i) { return data[i + n - 1]; }
};

vector<ll> divisor(ll n) {
  vector<ll> ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

/*
SegTree<ll> st(N, LLMAX,
        // a: left child, b: right child
        [](ll a, ll b) {return min(a, b); },
        // d: old, x: query
        [](ll d, ll x) {return x; });
// */

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  if (b == 0)
    return a;

  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll N, M, K, Q;
ll ans = 0;
ll H, W;

void solveD() {
  cin >> N;
  vpii VW(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    VW[i] = mp(v, w);
  }
  cin >> Q;
  vpii VL(Q);
  ll ml = 1e5 + 1;
  rep(i, Q) {
    ll v, l;
    cin >> v >> l;

    // chmax(ml, l);
    VL[i] = mp(v, l);
  }

  vi Used(N + 1, false);
  vvi dp(N + 1, vi(ml + 1, 0));

  repa(i, 1, N + 1) {
    pii vw = VW[i - 1];
    ll value = vw.first;
    ll weight = vw.second;
    rep(w, ml + 1) {
      if (w >= weight) {
        dp[i][w] = max(dp[i / 2][w - weight] + value, dp[i / 2][w]);
      } else {
        dp[i][w] = dp[i / 2][w];
      }
    }
  }

  rep(i, Q) {

    pii vl = VL[i];
    ll v = vl.first;
    ll l = vl.second;
    cout << dp[v][l] << endl;
  }
}

void solve() {
  cin >> N >> K;
  vi A(N);
  rep(i, N) { cin >> A[i]; }

  chmin(K, 50LL);
  rep(k, K) {
    vi B(N + 1, 0);
    rep(n, N) {
      int l = max(0LL, n - A[n]);
      int r = min(N - 1, n + A[n]);

      B[l]++;
      B[r + 1]--;
    }
    repa(n, 1, N) { B[n] += B[n - 1]; }
    rep(n, N) A[n] = B[n];
  }
  for (auto a : A) {
    cout << a << " ";
  }
  cout << endl;
}

int main() {
  solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long double ld;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef pair<ll, bool> pib;

typedef vector<bool> vb;

typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

typedef vector<double> vf;

typedef queue<ll> qi;

typedef long double ld;

#define mp make_pair
#define mt make_tuple
#define pb push_back

#define rep(i, N) for (ll i = 0; i < N; i++)
#define repa(i, a, N) for (ll i = a; i < N; i++)
#define repr(i, N) for (ll i = N - 1; i >= 0; i--)

#define drep(i, N, j, M) rep(i, N) rep(j, M)
#define trep(i, N, j, M, k, L) rep(i, N) rep(j, M) rep(k, L)

#define all(A) A.begin(), A.end()
#define uni(A) A.erase(unique(all(A)), A.end())

template <class t, class u>
ostream &operator<<(ostream &os, const pair<t, u> &p) {
  return os << "{" << p.first << "," << p.second << "}";
}

template <class t> ostream &operator<<(ostream &os, const vector<t> &v) {
  os << "{";
  for (auto e : v)
    os << e << ",";
  return os << "}" << endl;
}

template <class t, class u>
ostream &operator<<(ostream &os, const unordered_map<t, u> &m) {
  os << "{";
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    os << "(" << itr->first << ", " << itr->second << "), ";
  }
  return os << "}";
}

template <class t> ostream &operator<<(ostream &os, const set<t> &m) {
  os << "{";
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    os << *itr << ", ";
  }
  return os << "}";
}

template <class t> ostream &operator<<(ostream &os, const multiset<t> &m) {
  os << "{";
  for (auto itr = m.begin(); itr != m.end(); ++itr) {
    os << *itr << ", ";
  }
  return os << "}";
}

const ll LLMAX = numeric_limits<long long>::max() / 3;
const double DMAX = numeric_limits<double>::max() / 3;

const double PI = 3.141592653589793;
// const ll MOD = 1e9 + 7;
const ll MOD = 998244353;

template <std::int_fast64_t Modulus> class modint {
  using i64 = std::int_fast64_t;

public:
  i64 a;

  constexpr modint(const i64 x = 0) noexcept : a(x % Modulus) {}
  constexpr i64 &value() noexcept { return a; }
  constexpr const i64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
    return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
    return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
    return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
    return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
    a += rhs.a;
    if (a >= Modulus) {
      a -= Modulus;
    }
    return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
    if (a < rhs.a) {
      a += Modulus;
    }
    a -= rhs.a;
    return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
    a = a * rhs.a % Modulus;
    return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
    i64 exp = Modulus - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

template <std::int_fast64_t Modulus>
ostream &operator<<(ostream &os, const modint<Modulus> &m) {
  return os << (m.a + Modulus) % Modulus;
}

typedef modint<MOD> mod;
typedef vector<mod> vm;
typedef vector<vm> vvm;
typedef vector<vvm> vvvm;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void No() { cout << "No" << endl; }
void Yes() { cout << "Yes" << endl; }
void NO() { cout << "NO" << endl; }
void YES() { cout << "YES" << endl; }

vector<ll> fac(1e6 + 1);  // n!(mod M)
vector<ll> ifac(1e6 + 1); // k!^{M-2} (mod
ll mpow(ll x,
        ll n) { // x^n(mod M) ←普通にpow(x,n)では溢れてしまうため，随時mod計算
  ll ans = 1;
  while (n != 0) {
    if (n & 1)
      ans = ans * x % MOD;
    x = x * x % MOD;
    n = n >> 1;
  }
  return ans;
}

ll comb(ll a, ll b) { // aCbをmod計算
  if (a == 0 && b == 0)
    return 1;
  if (a < b || a < 0)
    return 0;
  ll tmp = ifac[a - b] * ifac[b] % MOD;
  return tmp * fac[a] % MOD;
}

void pre_calc_comb() {
  fac[0] = 1;
  ifac[0] = 1;
  for (ll i = 0; i < 1e6; i++) {
    fac[i + 1] = fac[i] * (i + 1) % MOD; // n!(mod M)
    ifac[i + 1] = ifac[i] * mpow(i + 1, MOD - 2) %
                  MOD; // k!^{M-2} (mod M) ←累乗にmpowを採用
  }
}

template <class T> class SegTree {
  int n;
  vector<T> data;
  T def;
  function<T(T, T)> operation;
  function<T(T, T)> update;
  T _query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return def;
    if (a <= l && r <= b)
      return data[k];
    else {
      T c1 = _query(a, b, 2 * k + 1, l, (l + r) / 2);
      T c2 = _query(a, b, 2 * k + 2, (l + r) / 2, r);
      return operation(c1, c2);
    }
  }

public:
  SegTree(size_t _n, T _def, function<T(T, T)> _operation,
          function<T(T, T)> _update)
      : def(_def), operation(_operation), update(_update) {
    n = 1;
    while (n < _n) {
      n *= 2;
    }
    data = vector<T>(2 * n - 1, def);
  }

  void change(int i, T x) {
    i += n - 1;
    data[i] = update(data[i], x);
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] = operation(data[i * 2 + 1], data[i * 2 + 2]);
    }
  }

  T query(int a, int b) { return _query(a, b, 0, 0, n); }

  T operator[](int i) { return data[i + n - 1]; }
};

vector<ll> divisor(ll n) {
  vector<ll> ret;
  for (ll i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

/*
SegTree<ll> st(N, LLMAX,
        // a: left child, b: right child
        [](ll a, ll b) {return min(a, b); },
        // d: old, x: query
        [](ll d, ll x) {return x; });
// */

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  if (b == 0)
    return a;

  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll N, M, K, Q;
ll ans = 0;
ll H, W;

void solveD() {
  cin >> N;
  vpii VW(N);
  rep(i, N) {
    ll v, w;
    cin >> v >> w;
    VW[i] = mp(v, w);
  }
  cin >> Q;
  vpii VL(Q);
  ll ml = 1e5 + 1;
  rep(i, Q) {
    ll v, l;
    cin >> v >> l;

    // chmax(ml, l);
    VL[i] = mp(v, l);
  }

  vi Used(N + 1, false);
  vvi dp(N + 1, vi(ml + 1, 0));

  repa(i, 1, N + 1) {
    pii vw = VW[i - 1];
    ll value = vw.first;
    ll weight = vw.second;
    rep(w, ml + 1) {
      if (w >= weight) {
        dp[i][w] = max(dp[i / 2][w - weight] + value, dp[i / 2][w]);
      } else {
        dp[i][w] = dp[i / 2][w];
      }
    }
  }

  rep(i, Q) {

    pii vl = VL[i];
    ll v = vl.first;
    ll l = vl.second;
    cout << dp[v][l] << endl;
  }
}

void solve() {
  cin >> N >> K;
  vi A(N);
  rep(i, N) { cin >> A[i]; }

  vi B(N);
  rep(k, K) {
    rep(n, N) {
      if (A[n] != N)
        goto Label;
    }

    break;
  Label:
    priority_queue<pii> que;

    rep(i, N) {
      que.push(mp(-(i - A[i]), 1));
      que.push(mp(-(i + A[i]), 0));
    }

    ll c = 0;
    ll idx = 0;
    while (que.empty() == false) {
      pii p = que.top();
      que.pop();

      while (idx < -p.first && idx < N) {
        chmax(B[idx], c);
        idx++;
      }

      if (p.second == 1) {
        c++;
      } else {
        if (idx < N) {
          chmax(B[idx], c);
        }
        c--;
      }
    }

    rep(i, N) {
      A[i] = B[i];
      B[i] = 0;
    }
  }
  for (auto a : A) {
    cout << a << " ";
  }
  cout << endl;
}

int main() {
  solve();
  return 0;
}