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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

struct LazySegmentTree {
private:
  int n;
  vector<ll> node, lazy;

public:
  LazySegmentTree(vector<ll> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 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 add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    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 {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }

  ll getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k];
    ll vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};

int main() {
  fastio ll n, k;
  cin >> n >> k;
  ll a[n];
  rep(i, 0, n - 1) cin >> a[i];
  ll b[n];
  rep(i, 0, n - 1) b[i] = 0;
  ll c = 0;
  while (true) {
    bool bol = true;
    rep(i, 0, n - 1) {
      if (a[i] >= max(i, n - 1 - i)) {

      } else {
        bol = false;
      }
    }
    if (bol) {
      break;
    }

    LazySegmentTree seg(vector<ll>(n, 0));
    ll all = 0;
    rep(i, 0, n - 1) {
      if (max(0, i - a[i]) == 0 && min(n - 1, i + a[i]) + 1 == n) {
        all++;
      } else {
        seg.add(max(0, i - a[i]), min(n - 1, i + a[i]) + 1, 1);
      }
    }

    rep(i, 0, n - 1) {
      if (a[i] == b[i] && a[i] == n) {
        b[i] = a[i];
        a[i] = n;
      } else {
        b[i] = a[i];
        a[i] = seg.getsum(i, i + 1) + all;
      }
    }

    c++;
    if (c == k)
      break;
  }
  if (c == k) {
    rep(i, 0, n - 1) {
      cout << a[i];
      if (i == n - 1) {
        cout << endl;
      } else {
        cout << ' ';
      }
    }
  } else {
    rep(i, 0, n - 1) {
      cout << n;
      if (i == n - 1) {
        cout << endl;
      } else {
        cout << ' ';
      }
    }
  }

  /*
  ll n=200000;
  cout<<n<<' '<<n<<endl;
  rep(i,0,n-1){
      cout<<0<<' ';
  }
  cout<<endl;*/

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\\n"
#define fastio                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

struct LazySegmentTree {
private:
  int n;
  vector<ll> node, lazy;

public:
  LazySegmentTree(vector<ll> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 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 add(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    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 {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k + 2];
    }
  }

  ll getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    eval(k, l, r);
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b)
      return node[k];
    ll vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    ll vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};

int main() {
  fastio ll n, k;
  cin >> n >> k;
  ll a[n];
  rep(i, 0, n - 1) cin >> a[i];
  ll c = 0;
  while (true) {
    bool bol = true;
    rep(i, 0, n - 1) {
      if (a[i] >= max(i, n - 1 - i)) {

      } else {
        bol = false;
      }
    }
    if (bol) {
      break;
    }

    LazySegmentTree seg(vector<ll>(n, 0));
    rep(i, 0, n - 1) { seg.add(max(0, i - a[i]), min(n - 1, i + a[i]) + 1, 1); }

    rep(i, 0, n - 1) { a[i] = seg.getsum(i, i + 1); }

    c++;
    if (c == k)
      break;
  }
  if (c == k) {
    rep(i, 0, n - 1) {
      cout << a[i];
      if (i == n - 1) {
        cout << endl;
      } else {
        cout << ' ';
      }
    }
  } else {
    rep(i, 0, n - 1) {
      cout << n;
      if (i == n - 1) {
        cout << endl;
      } else {
        cout << ' ';
      }
    }
  }

  /*
  ll n=200000;
  cout<<n<<' '<<n<<endl;
  rep(i,0,n-1){
      cout<<0<<' ';
  }
  cout<<endl;*/

  return 0;
}