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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
    tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 1e9 + 7;
const int maxn = 1000005;
#define endl '\\n'
#define int ll
#define ld long double
#define all(x) (x).begin(), (x).end()
struct FT {
  vector<ll> s;
  FT(int n) : s(n, 0) {}
  void update(int pos, ll dif) { // a[pos] += dif
    for (; pos < (s).size(); pos |= pos + 1)
      s[pos] = (s[pos] + dif);
  }
  ll query(int pos) { // sum of values in [0, pos)
    ll res = 0;
    for (; pos > 0; pos &= pos - 1)
      res = (res + s[pos - 1]);
    return res;
  }
};

int32_t main() {
  IOS

      int n,
      k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  bool changed = true;
  while ((k--) and changed) {
    changed = false;
    FT ft(n + 2);
    for (int i = 1; i <= n; i++) {
      ft.update(min(n + 1, i + a[i] + 1), -1ll);
      ft.update(max(0ll, i - a[i]), 1ll);
    }
    for (int i = 1; i <= n; i++) {
      int temp = ft.query(i + 1);
      if (temp != a[i]) {
        changed = true;
        a[i] = temp;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
    tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 1e9 + 7;
const int maxn = 1000005;
#define endl '\\n'
#define int ll
#define ld long double
#define all(x) (x).begin(), (x).end()
struct FT {
  vector<ll> s;
  FT(int n) : s(n, 0) {}
  void update(int pos, ll dif) { // a[pos] += dif
    for (; pos < (s).size(); pos |= pos + 1)
      s[pos] = (s[pos] + dif);
  }
  ll query(int pos) { // sum of values in [0, pos)
    ll res = 0;
    for (; pos > 0; pos &= pos - 1)
      res = (res + s[pos - 1]);
    return res;
  }
};

int32_t main() {
  IOS int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  bool changed = true;
  while ((k--) and changed) {
    changed = true;
    FT ft(n + 2);
    for (int i = 1; i <= n; i++) {
      ft.update(min(n + 1, i + a[i] + 1), -1ll);
      ft.update(max(0ll, i - a[i]), 1ll);
    }
    for (int i = 1; i <= n; i++) {
      int temp = ft.query(i + 1);
      if (temp != a[i]) {
        changed = true;
        a[i] = temp;
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}