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

#include <bits/stdc++.h>
#define LL long long

namespace io {
template <typename T> inline void read(T &_x) {
  int f = 0, ch;
  _x = 0;
  while (!isdigit(ch = getchar()))
    f |= ch == '-';
  while (isdigit(ch))
    _x = _x * 10 + ch - '0', ch = getchar();
  if (f)
    _x = -_x;
}
template <typename T, typename... Args> inline void read(T &_f, Args &...args) {
  read(_f), read(args...);
}
inline void _deal(char ch) { putchar(ch); }
template <typename T> inline void _deal(T _x) {
  if (_x < 0)
    putchar('-'), _x = -_x;
  if (_x > 9)
    _deal(_x / 10);
  putchar(_x % 10 + '0');
}
inline void write() {}
template <typename T, typename... Args> inline void write(T _f, Args... args) {
  _deal(_f), write(args...);
}
} // namespace io

const int N = 2e5 + 5;

int n, k, a[N], cf[N], fir;

int main() {
  io::read(n, k);
  for (int i = 1; i <= n; i++) {
    io::read(a[i]);
    if (a[i] >= i - 1)
      ++fir;
    if (i - a[i] >= 2)
      ++cf[i - a[i]];
    if (i + a[i] + 1 <= n)
      --cf[i + a[i] + 1];
  }
  for (int i = 1; i <= k; ++i) {
    a[0] = fir;
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      a[i] = a[i - 1] + cf[i];
      cf[i] = 0;
      cnt += (a[i] >= n);
    }
    fir = 0;
    if (cnt >= n)
      break;
    for (int i = 1; i <= n; i++) {
      if (a[i] >= i - 1)
        ++fir;
      if (i - a[i] >= 2)
        ++cf[i - a[i]];
      if (i + a[i] + 1 <= n)
        --cf[i + a[i] + 1];
    }
  }
  for (int i = 1; i <= n; ++i)
    io::write(a[i], ' ');
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define LL long long

namespace io {
template <typename T> inline void read(T &_x) {
  int f = 0, ch;
  _x = 0;
  while (!isdigit(ch = getchar()))
    f |= ch == '-';
  while (isdigit(ch))
    _x = _x * 10 + ch - '0', ch = getchar();
  if (f)
    _x = -_x;
}
template <typename T, typename... Args> inline void read(T &_f, Args &...args) {
  read(_f), read(args...);
}
inline void _deal(char ch) { putchar(ch); }
template <typename T> inline void _deal(T _x) {
  if (_x < 0)
    putchar('-'), _x = -_x;
  if (_x > 9)
    _deal(_x / 10);
  putchar(_x % 10 + '0');
}
inline void write() {}
template <typename T, typename... Args> inline void write(T _f, Args... args) {
  _deal(_f), write(args...);
}
} // namespace io

const int N = 2e5 + 5;

int n, k, a[N];

struct ZKW {
  static const int MS = N;
  int m;
  LL dat[MS << 2], add[MS << 2];
  void maintain(int x) { dat[x] = dat[x << 1] + dat[x << 1 | 1]; }
  void build(int x) {
    for (m = 1; m < x; m <<= 1)
      ;
    //        for(int i = m + 1; i <= m + x; ++i) dat[i] = a[i - m];
    //        for(int i = m; i >= 1; --i) maintain(i);
  }
  void modify(int l, int r, int v) {
    int tl = 0, tr = 0, tn = 1;
    for (l = l + m - 1, r = r + m + 1; l ^ r ^ 1; l >>= 1, r >>= 1, tn <<= 1) {
      dat[l] += 1LL * v * tl, dat[r] += 1LL * v * tr;
      if (~l & 1)
        add[l ^ 1] += v, dat[l ^ 1] += 1LL * v * tn, tl += tn;
      if (r & 1)
        add[r ^ 1] += v, dat[r ^ 1] += 1LL * v * tn, tr += tn;
    }
    for (; l; l >>= 1, r >>= 1)
      dat[l] += 1LL * v * tl, dat[r] += 1LL * v * tr;
  }
  LL query(int l, int r) {
    int tl = 0, tr = 0, tn = 1;
    LL res = 0;
    for (l = l + m - 1, r = r + m + 1; l ^ r ^ 1; l >>= 1, r >>= 1, tn <<= 1) {
      if (add[l])
        res += 1LL * add[l] * tl;
      if (add[r])
        res += 1LL * add[r] * tr;
      if (~l & 1)
        res += dat[l ^ 1], tl += tn;
      if (r & 1)
        res += dat[r ^ 1], tr += tn;
    }
    for (; l; l >>= 1, r >>= 1)
      res += 1LL * add[l] * tl, res += 1LL * add[r] * tr;
    return res;
  }
} t;

int main() {
  io::read(n, k);
  for (int i = 1; i <= n; ++i)
    io::read(a[i]);
  t.build(n);
  for (int i = 1; i <= k; ++i) {
    int flg = 1;
    for (int j = 1; j <= n; ++j) {
      t.modify(std::max(1, j - a[j]), std::min(n, j + a[j]), 1);
    }
    for (int j = 1; j <= n; ++j) {
      a[j] = t.query(j, j);
      t.modify(j, j, -a[j]);
      if (a[j] != n - 1)
        flg = 0;
      //			printf("%d ", a[j]);
    }
    //		puts("");
    if (flg)
      break;
  }
  for (int i = 1; i <= n; ++i)
    printf("%d ", a[i]);
  return 0;
}