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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#include <string.h>
#define inf 1000000000000000000
#define maxn 200005
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
#define PI 3.14159265359
#define endl '\\n'
#define mapint_iterator map<int, int>::iterator
#define mapll_iterator map<ll, ll>::iterator
#define setint_iterator set<int>::iterator
#define setll_iterator set<ll>::iterator
#define setpint_iterator set<pint>::iterator
#define setpll_iterator set<pll>::iterator
#define cout1(a) cout << a << endl
#define cout2(a, b) cout << a << ' ' << b << endl
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define vcout(v, i) cout << v[i].fi << " " << v[i].se << endl
#define print_double(i) printf("%.9llf\\n", i)
typedef priority_queue<ll, vector<ll>, greater<ll>> pqset;
typedef priority_queue<pll, vector<pll>, greater<pll>> pqset_ll;
#define pb push_back
#define pf push_front
#define fi first
#define mkp make_pair
#define se second
ll dxk[] = {0, 0, 1, -1, 1, 1, -1, -1};
ll dyk[] = {1, -1, 0, 0, 1, -1, 1, -1};
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os;
typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    os_pair;
#define acc (ios::sync_with_stdio(false), cin.tie(0))
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per1(i, n) for (ll i = n; i > 0; i--)
#define repeat(i, start, n) for (ll i = start; i < n; i++)
#define power2(i) ((ll)1 << (ll)i)
ll ll_max(ll a, ll b, ll c) { return max(a, max(b, c)); }
int int_max(int a, int b, int c) { return max(a, max(b, c)); }
ll ll_min(ll a, ll b, ll c) { return min(a, min(b, c)); }
int int_min(int a, int b, int c) { return min(a, min(b, c)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll dx[] = {0, 0, 1, -1};
ll dy[] = {1, -1, 0, 0};
ll power(ll a, ll b) {
  if (a == 1)
    return 1;
  if (b == 0)
    return 1;
  ll c = power(a, b / 2);
  ll res = 1;
  if (b % 2) {
    res = (c * c);
    if (res >= mod)
      res %= mod;
    res *= a;
  } else
    res = ((c * c));
  if (res >= mod)
    res %= mod;
  return res;
}
ll modInv(ll a) { return power(a, mod - 2) % mod; }
ll fact[1], inv[1];
void factorial(ll n) {
  fact[0] = 1;
  for (ll i = 1; i <= n; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
  }
}
void InvFactorial(ll n) {
  inv[0] = 1;
  for (ll i = 1; i <= n; i++)
    inv[i] = modInv(fact[i]);
}
ll ncr(ll n, ll r) {
  if (n < r || n < 0 || r < 0)
    return 0;
  ll b = inv[n - r];
  ll c = inv[r];
  ll a = fact[n] * b;
  if (a >= mod)
    a %= mod;
  a *= c;
  if (a >= mod)
    a %= mod;
  return a;
}
ll power(ll a, ll b, ll mod1) {
  if (a == 1)
    return 1;
  if (b == 0)
    return 1;
  ll c = power(a, b / 2, mod1);
  ll res = 1;
  if (b % 2) {
    res = (c * c);
    if (res >= mod1)
      res %= mod1;
    res *= a;
  } else
    res = ((c * c));
  if (res >= mod1)
    res %= mod1;
  return res;
}
// ifstream cin("b_read_on.txt"); ofstream cout("output3.txt");
// Use (<<) for multiplication
// Use (>>) for division
// ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL);
//  find_by_order -> value at index
//  order_of_key -> index of value
//  while using (1<<i) use ((ll)1<<(ll)i)
//  in Floyd-Warshall Algo, k is outer loop
//  If an element was not initially in map and if asked mp[a],the element gets
//  inserted a%=mod take a lot of time... try to use it minimum and use memset
//  as it reduces a lot of time usage...use if(a>=mod) a%=mod
// cout<<(double) can be harmful , always use printf(%.9llf)...take
// scanf("%lf",&p[i][j]) as input , not llf; use s.erase(it++) for erasing
// iterator and then moving to the next one never use adj.resize(n) as value is
// persistent, always erase use __builtin_popcountll() for ll
//  no of prime numbers in range : (70,19) , (1000,168) , (100000,1229) ,
//  (sqrt(10^9),3409) ;
// always check the use of segment tree using bottom-up dp
//__gcd(0,0) gives runtime error
void solve(int countu) {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  rep(i, n) cin >> a[i];
  if (n == 1) {
    cout1(1);
    return;
  }
  int t = 0;
  while (k) {
    k--;
    vector<int> b(n + 1, 0);
    rep(i, n) {
      b[max(0, i - a[i])]++;
      b[min(n, i + a[i] + 1)]--;
    }
    int count = 1;
    int u = b[0];
    repeat(i, 1, n) {
      b[i] += b[i - 1];
      if (b[i] == u + 1)
        count++;
    }
    if (b[n - 1] == u + 1)
      count--;
    if (b[n - 1] == b[0])
      count++;
    a = b;
    if (count == n) {
      t = 1;
      break;
    }
    count = 0;
    rep(i, n) if (a[i] == n) count++;
    if (count == n) {
      t = 1;
      break;
    }
  }
  rep(i, n) a[i] = min(n, a[i] + k * 2);
  rep(i, n) cout << a[i] << ' ';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed;
  cerr.tie(NULL);
  int t = 1;
  // cin>>t;
  int countu = 1;
  while (t--) {
    solve(countu);
    countu++;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#include <string.h>
#define inf 1000000000000000000
#define maxn 200005
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
#define PI 3.14159265359
#define endl '\\n'
#define mapint_iterator map<int, int>::iterator
#define mapll_iterator map<ll, ll>::iterator
#define setint_iterator set<int>::iterator
#define setll_iterator set<ll>::iterator
#define setpint_iterator set<pint>::iterator
#define setpll_iterator set<pll>::iterator
#define cout1(a) cout << a << endl
#define cout2(a, b) cout << a << ' ' << b << endl
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define vcout(v, i) cout << v[i].fi << " " << v[i].se << endl
#define print_double(i) printf("%.9llf\\n", i)
typedef priority_queue<ll, vector<ll>, greater<ll>> pqset;
typedef priority_queue<pll, vector<pll>, greater<pll>> pqset_ll;
#define pb push_back
#define pf push_front
#define fi first
#define mkp make_pair
#define se second
ll dxk[] = {0, 0, 1, -1, 1, 1, -1, -1};
ll dyk[] = {1, -1, 0, 0, 1, -1, 1, -1};
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os;
typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    os_pair;
#define acc (ios::sync_with_stdio(false), cin.tie(0))
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per1(i, n) for (ll i = n; i > 0; i--)
#define repeat(i, start, n) for (ll i = start; i < n; i++)
#define power2(i) ((ll)1 << (ll)i)
ll ll_max(ll a, ll b, ll c) { return max(a, max(b, c)); }
int int_max(int a, int b, int c) { return max(a, max(b, c)); }
ll ll_min(ll a, ll b, ll c) { return min(a, min(b, c)); }
int int_min(int a, int b, int c) { return min(a, min(b, c)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll dx[] = {0, 0, 1, -1};
ll dy[] = {1, -1, 0, 0};
ll power(ll a, ll b) {
  if (a == 1)
    return 1;
  if (b == 0)
    return 1;
  ll c = power(a, b / 2);
  ll res = 1;
  if (b % 2) {
    res = (c * c);
    if (res >= mod)
      res %= mod;
    res *= a;
  } else
    res = ((c * c));
  if (res >= mod)
    res %= mod;
  return res;
}
ll modInv(ll a) { return power(a, mod - 2) % mod; }
ll fact[1], inv[1];
void factorial(ll n) {
  fact[0] = 1;
  for (ll i = 1; i <= n; i++) {
    fact[i] = fact[i - 1] * i;
    fact[i] %= mod;
  }
}
void InvFactorial(ll n) {
  inv[0] = 1;
  for (ll i = 1; i <= n; i++)
    inv[i] = modInv(fact[i]);
}
ll ncr(ll n, ll r) {
  if (n < r || n < 0 || r < 0)
    return 0;
  ll b = inv[n - r];
  ll c = inv[r];
  ll a = fact[n] * b;
  if (a >= mod)
    a %= mod;
  a *= c;
  if (a >= mod)
    a %= mod;
  return a;
}
ll power(ll a, ll b, ll mod1) {
  if (a == 1)
    return 1;
  if (b == 0)
    return 1;
  ll c = power(a, b / 2, mod1);
  ll res = 1;
  if (b % 2) {
    res = (c * c);
    if (res >= mod1)
      res %= mod1;
    res *= a;
  } else
    res = ((c * c));
  if (res >= mod1)
    res %= mod1;
  return res;
}
// ifstream cin("b_read_on.txt"); ofstream cout("output3.txt");
// Use (<<) for multiplication
// Use (>>) for division
// ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL);
//  find_by_order -> value at index
//  order_of_key -> index of value
//  while using (1<<i) use ((ll)1<<(ll)i)
//  in Floyd-Warshall Algo, k is outer loop
//  If an element was not initially in map and if asked mp[a],the element gets
//  inserted a%=mod take a lot of time... try to use it minimum and use memset
//  as it reduces a lot of time usage...use if(a>=mod) a%=mod
// cout<<(double) can be harmful , always use printf(%.9llf)...take
// scanf("%lf",&p[i][j]) as input , not llf; use s.erase(it++) for erasing
// iterator and then moving to the next one never use adj.resize(n) as value is
// persistent, always erase use __builtin_popcountll() for ll
//  no of prime numbers in range : (70,19) , (1000,168) , (100000,1229) ,
//  (sqrt(10^9),3409) ;
// always check the use of segment tree using bottom-up dp
//__gcd(0,0) gives runtime error
void solve(int countu) {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 1);
  rep(i, n) cin >> a[i];
  if (n * k <= 10000000) {
    while (k--) {
      vector<int> b(n + 1);
      rep(i, n) {
        b[max(0, i - a[i])]++;
        b[min(n, i + a[i] + 1)]--;
      }
      repeat(i, 1, n) b[i] += b[i - 1];
      a = b;
    }
    rep(i, n) cout << a[i] << ' ';
    return;
  }
  while (k--) {
    vector<int> b(n + 1);
    rep(i, n) {
      b[max(0, i - a[i])]++;
      b[min(n, i + a[i] + 1)]--;
    }
    int count = 1;
    int u = b[0];
    repeat(i, 1, n) {
      b[i] += b[i - 1];
      if (b[i] == u + 1)
        count++;
    }
    if (b[n - 1] == u + 1)
      count--;
    if (b[n - 1] == b[0])
      count++;
    a = b;
    if (count == n)
      break;
    count = 0;
    rep(i, n) if (a[i] == n) count++;
    if (count == n)
      break;
  }
  k++;
  rep(i, n) a[i] = min(n, a[i] + k * 2);
  rep(i, n) cout << a[i] << ' ';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed;
  cerr.tie(NULL);
  int t = 1;
  // cin>>t;
  int countu = 1;
  while (t--) {
    solve(countu);
    countu++;
  }
}