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

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin >>
#define ll long long
#define ln cout << '\\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\\n", a)
#define mem(a) memset(a, 0, sizeof(a))
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++)
ll check(ll n, ll m, ll x, ll y) { return x >= 0 && x < n && y >= 0 && y < m; }
void pr() { ln; }
template <class A, class... B> void pr(const A &a, const B &...b) {
  cout << a << (sizeof...(b) ? " " : "");
  pr(b...);
}
template <class A> void PR(A a, ll n) {
  rep(i, n) cout << (i ? " " : "") << a[i];
  ln;
}
const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1},
         dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
typedef pair<ll, ll> P;

void Main() {
  ll n, m;
  cin >> n >> m;
  ll a[n];
  rep(i, n) R a[i];
  vector<P> v;
  rep(i, n) {
    if (a[i] != n)
      v.pb(P(i, a[i]));
  }
  ll d[n + 1];
  rep(k, m) {
    mem(d);
    rep(i, v.size()) {
      ll x = v[i].F, y = v[i].S;
      d[max(0LL, x - y)]++;
      d[min(n, x + y + 1)]--;
    }
    d[0] += n - v.size();
    d[n] -= n - v.size();
    rep(i, n) d[i + 1] += d[i];
    vector<P> g;
    rep(i, n) {
      ll x = i;
      ll y = d[x];
      if (y != n)
        g.pb(P(x, y));
    }
    v = g;
    if (!v.size()) {
      PR(d, n);
      return;
    }
  }
  PR(d, n);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin >>
#define ll long long
#define ln cout << '\\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\\n", a)
#define mem(a) memset(a, 0, sizeof(a))
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++)
ll check(ll n, ll m, ll x, ll y) { return x >= 0 && x < n && y >= 0 && y < m; }
void pr() { ln; }
template <class A, class... B> void pr(const A &a, const B &...b) {
  cout << a << (sizeof...(b) ? " " : "");
  pr(b...);
}
template <class A> void PR(A a, ll n) {
  rep(i, n) cout << (i ? " " : "") << a[i];
  ln;
}
const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1},
         dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
typedef pair<ll, ll> P;

int N = 1 << 18;
class StarrySkyTree {
public:
  ll Mi[555555], A[555555];
  void init() { memset(Mi, 0, sizeof(Mi)), memset(A, 0, sizeof(A)); }
  void add(int a, int b, ll x, int k = 0, int l = 0, int r = N) {
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      A[k] += x;
      while (k) {
        k = (k - 1) / 2;
        Mi[k] = min(Mi[k * 2 + 1] + A[k * 2 + 1], Mi[k * 2 + 2] + A[k * 2 + 2]);
      }
      return;
    }
    add(a, b, x, k * 2 + 1, l, (l + r) / 2);
    add(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
  ll getMin(int a, int b, int k = 0, int l = 0, int r = N) {
    if (r <= a || b <= l)
      return LLONG_MAX;
    if (a <= l && r <= b)
      return Mi[k] + A[k];
    ll left = getMin(a, b, k * 2 + 1, l, (l + r) / 2),
       right = getMin(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(left, right) + A[k];
  }
};

StarrySkyTree t;

void Main() {
  ll n, m;
  cin >> n >> m;
  ll a[n];
  rep(i, n) R a[i];
  vector<P> v;
  rep(i, n) {
    if (a[i] != n)
      v.pb(P(i, a[i]));
  }
  rep(k, m) {
    t.init();
    rep(i, v.size()) {
      ll x = v[i].F, y = v[i].S;
      t.add(max(0LL, x - y), min(n, x + y + 1), 1);
    }
    t.add(0, n, n - v.size());
    vector<P> g;
    if (k < 20) {
      rep(i, n) {
        ll x = i;
        ll y = t.getMin(x, x + 1);
        if (y != n)
          g.pb(P(x, y));
      }
    } else {
      rep(i, v.size()) {
        ll x = v[i].F;
        ll y = t.getMin(x, x + 1);
        if (y != n)
          g.pb(P(x, y));
      }
    }
    v = g;
    if (!v.size()) {
      rep(i, n) {
        if (i)
          cout << " ";
        cout << t.getMin(i, i + 1);
      }
      ln;
      return;
    }
  }
  rep(i, n) {
    if (i)
      cout << " ";
    cout << t.getMin(i, i + 1);
  }
  ln;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  Main();
  return 0;
}