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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
template <typename T, typename E> struct LazySegmentTree {
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  int sn, hei;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> node;
  vector<E> lazy;
  LazySegmentTree(const vector<T> &v, F f, G g, H h, T ti, E ei)
      : f(f), g(g), h(h), ti(ti), ei(ei) {
    int n = v.size();
    sn = 1, hei = 0;
    while (sn < n) {
      sn <<= 1;
      hei++;
    }
    node.assign(2 * sn, ti);
    lazy.assign(2 * sn, ei);
    for (int i = 0; i < n; i++) {
      node[sn + i] = v[i];
    }
    for (int i = sn - 1; i > 0; i--) {
      node[i] = f(node[(i << 1) | 0], node[(i << 1) | 1]);
    }
  }
  inline T reflect(int k) {
    return lazy[k] == ei ? node[k] : g(node[k], lazy[k]);
  }
  inline void propagate(int k) {
    if (lazy[k] == ei)
      return;
    lazy[(k << 1) | 0] = h(lazy[(k << 1) | 0], lazy[k]);
    lazy[(k << 1) | 1] = h(lazy[(k << 1) | 1], lazy[k]);
    node[k] = reflect(k);
    lazy[k] = ei;
  }
  inline void thrust(int k) {
    for (int i = hei; i > 0; i--) {
      propagate(k >> i);
    }
  }
  inline void recalc(int k) {
    while (k >>= 1) {
      node[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1));
    }
  }
  void update(int a, int b, E x) {
    if (a >= b)
      return;
    thrust(a += sn);
    thrust(b += sn - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) {
        lazy[l] = h(lazy[l], x);
        l++;
      }
      if (r & 1) {
        r--;
        lazy[r] = h(lazy[r], x);
      }
    }
    recalc(a);
    recalc(b);
  }
  void set_val(int a, T x) {
    thrust(a += sn);
    node[a] = x;
    lazy[a] = ei;
    recalc(a);
  }
  T query(int a, int b) {
    if (a >= b)
      return ti;
    thrust(a += sn);
    thrust(b += sn - 1);
    T vl = ti, vr = ti;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) {
        vl = f(vl, reflect(l));
        l++;
      }
      if (r & 1) {
        r--;
        vr = f(reflect(r), vr);
      }
    }
    return f(vl, vr);
  }
};
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  while (k--) {
    vector<ll> s(n + 1);
    rep(i, n) {
      s[max(0ll, i - a[i])]++;
      s[min(n, i + a[i] + 1)]--;
    }
    rep(i, n) s[i + 1] += s[i];
    a = s;
    a.pop_back();
    if (*min_element(all(a)) == n)
      break;
  }
  cout << a << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define get_unique(x) x.erase(unique(all(x)), x.end());
typedef long long ll;
typedef complex<double> Complex;
const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (int i = 0; i < sz(v); i++) {
    os << v[i];
    if (i < sz(v) - 1)
      os << " ";
  }
  return os;
}
template <typename T, typename E> struct LazySegmentTree {
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  int sn, hei;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> node;
  vector<E> lazy;
  LazySegmentTree(const vector<T> &v, F f, G g, H h, T ti, E ei)
      : f(f), g(g), h(h), ti(ti), ei(ei) {
    int n = v.size();
    sn = 1, hei = 0;
    while (sn < n) {
      sn <<= 1;
      hei++;
    }
    node.assign(2 * sn, ti);
    lazy.assign(2 * sn, ei);
    for (int i = 0; i < n; i++) {
      node[sn + i] = v[i];
    }
    for (int i = sn - 1; i > 0; i--) {
      node[i] = f(node[(i << 1) | 0], node[(i << 1) | 1]);
    }
  }
  inline T reflect(int k) {
    return lazy[k] == ei ? node[k] : g(node[k], lazy[k]);
  }
  inline void propagate(int k) {
    if (lazy[k] == ei)
      return;
    lazy[(k << 1) | 0] = h(lazy[(k << 1) | 0], lazy[k]);
    lazy[(k << 1) | 1] = h(lazy[(k << 1) | 1], lazy[k]);
    node[k] = reflect(k);
    lazy[k] = ei;
  }
  inline void thrust(int k) {
    for (int i = hei; i > 0; i--) {
      propagate(k >> i);
    }
  }
  inline void recalc(int k) {
    while (k >>= 1) {
      node[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1));
    }
  }
  void update(int a, int b, E x) {
    if (a >= b)
      return;
    thrust(a += sn);
    thrust(b += sn - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) {
        lazy[l] = h(lazy[l], x);
        l++;
      }
      if (r & 1) {
        r--;
        lazy[r] = h(lazy[r], x);
      }
    }
    recalc(a);
    recalc(b);
  }
  void set_val(int a, T x) {
    thrust(a += sn);
    node[a] = x;
    lazy[a] = ei;
    recalc(a);
  }
  T query(int a, int b) {
    if (a >= b)
      return ti;
    thrust(a += sn);
    thrust(b += sn - 1);
    T vl = ti, vr = ti;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1) {
        vl = f(vl, reflect(l));
        l++;
      }
      if (r & 1) {
        r--;
        vr = f(reflect(r), vr);
      }
    }
    return f(vl, vr);
  }
};
int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> a(n + 1);
  rep(i, n) cin >> a[i];
  while (k--) {
    vector<ll> s(n + 1);
    rep(i, n) {
      s[max(0ll, i - a[i])]++;
      s[min(n, i + a[i] + 1)]--;
    }
    rep(i, n) s[i + 1] += s[i];
    swap(s, a);
    if (*min_element(all(a)) == n)
      break;
  }
  a.pop_back();
  cout << a << endl;
}