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

#include <bits/stdc++.h>

using namespace std;

#define SZ(x) (int)(x.size())
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RREP(i, n) for (int i = (int)(n); i >= 0; --i)
#define RFOR(i, a, b) for (int i = (int)(a); i >= (int)(b); --i)
#define ALL(a) (a).begin(), (a).end()
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;

const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  REP(i, SZ(v)) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}

struct FenwickTree {
  vector<long long> v;
  int n;
  FenwickTree(int n) : n(n), v(n) {}
  void add(int i, long long x) {
    i++;
    while (i <= n) {
      v[i - 1] += x;
      i += i & -i;
    }
  }
  long long get(int i) {
    long long r = 0;
    while (i >= 1) {
      r += v[i - 1];
      i -= i & -i;
    }
    return r;
  }
};

struct ExFenwickTree {
  FenwickTree p, q;
  ExFenwickTree(int n) : p(n + 1), q(n + 1) {}
  void add(int l, int r, long long w) {
    p.add(l, -w * l);
    p.add(r, w * r);
    q.add(l, w);
    q.add(r, -w);
  }
  long long get(int r) { return p.get(r) + q.get(r) * r; }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  ll n, k;
  cin >> n >> k;
  vll a(n);
  REP(i, n) { cin >> a[i]; }

  ExFenwickTree bit(n);
  REP(i, n) { bit.add(i, i + 1, a[i]); }
  REP(j, k) {
    if (bit.get(n) >= n * n)
      break;
    ExFenwickTree bit2(n);
    REP(i, n) {
      ll val = bit.get(i + 1) - bit.get(i);
      bit2.add(max(0ll, i - val), min(n, i + val + 1), 1);
    }
    bit = bit2;
  }

  REP(i, n) {
    cout << bit.get(i + 1) - bit.get(i);
    if (i == n - 1)
      cout << endl;
    else
      cout << " ";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
template <typename T> struct LazySegTreeAddSum {
public:
  int n;
  std::vector<T> node, lazy;
  LazySegTreeAddSum(int sz) {
    n = 1;
    while (n < sz)
      n <<= 2;
    node.resize(2 * n);
    lazy.resize(2 * n, 0);
  }
  LazySegTreeAddSum(std::vector<T> v) {
    int sz = (int)(v.size());
    n = 1;
    while (n < sz)
      n <<= 2;
    node.resize(2 * n);
    lazy.resize(2 * n, 0);
    for (int i = n - 1; i < n - 1 + sz; ++i) {
      node[i] = v[i - n + 1];
    }
    for (int i = n - 2; i >= 0; --i) {
      node[i] = node[2 * i + 1] + node[2 * i + 2];
    }
  }
  void eval(int k, int l, int r) {
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
      lazy[k] = 0;
    }
  }
  void update(int a, int b, T x, int k, int l, int r) {
    eval(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] += (r - l) * x;
      eval(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }
  void update(int a, int b, T x) { update(a, b, x, 0, 0, n); }
  T query(int a, int b, int k, int l, int r) {
    if (b <= l || r <= a)
      return 0;
    eval(k, l, r);
    if (a <= l && r <= b)
      return node[k];
    T v1 = query(a, b, 2 * k + 1, l, (l + r) / 2);
    T v2 = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return v1 + v2;
  }
  T query(int a, int b) { return query(a, b, 0, 0, n); }
};
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2> bool chmax(T1 &a, const T2 &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2> bool chmin(T1 &a, const T2 &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < ((int)(v.size())); ++i) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll n, k;
  cin >> n >> k;
  vll a(n);
  for (int i = 0; i < (n); ++i)
    cin >> a[i];
  LazySegTreeAddSum<ll> *st1;
  st1 = new LazySegTreeAddSum<ll>(a);
  for (int _ = 0; _ < (k); ++_) {
    if (st1->query(0, n) >= n * n) {
      break;
    }
    LazySegTreeAddSum<ll> *st2;
    st2 = new LazySegTreeAddSum<ll>(n);
    for (int i = 0; i < (n); ++i) {
      ll val = st1->query(i, i + 1);
      st2->update(max(0ll, i - val), min(n, i + val + 1), 1);
    }
    st1 = st2;
  }
  for (int i = 0; i < (n); ++i) {
    cout << min(n, st1->query(i, i + 1));
    if (i == n - 1)
      cout << endl;
    else
      cout << " ";
  }
}