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

#include <bits/stdc++.h>

#define int long long
#define uint unsigned int
#define ld long double
#define showoff                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define f first
#define se second
#define maxn 400005
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vector<int>, greater<int>>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power(int a, int n) {
  a %= mod;
  if (n == 1)
    return a;
  if (n == 0)
    return 1;
  if (n % 2)
    return (a * (power((a * a) % mod, n / 2) % mod)) % mod;
  return power((a * a) % mod, n / 2) % mod;
}
const int inf = (int)1e18;

int inverse(int x) {
  return power(x, mod - 2) % mod; // little fermat....
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 5);
  FOR(i, 1, n + 1) cin >> a[i];
  FOR(j, 0, k) {
    vector<int> pre(4 * n + 5);
    FOR(i, 1, n + 1) {
      pre[i - a[i] + 2 * n]++;
      pre[i + a[i] + 1 + 2 * n]--;
    }
    FOR(i, 1, 4 * n + 1) pre[i] += pre[i - 1];
    FOR(i, 2 * n + 1, 3 * n + 1) { a[i - 2 * n] = pre[i]; }
    int mi = inf;
    FOR(i, 1, n + 1) mi = min(mi, a[i]);
    if (mi >= n)
      break;
  }
  FOR(i, 1, n + 1) cout << min(a[i], n) << " ";
}
signed main() {
  showoff;
  int T = 1;
  // cin >> T;
  FOR(t, 1, T + 1) {
    solve();
    // cout << "\\n";
  }
  return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long
#define uint unsigned int
#define ld long double
#define showoff                                                                \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define pii pair<int, int>
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define f first
#define se second
#define maxn 400005
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define pqueue priority_queue<int>
#define pdqueue priority_queue<int, vector<int>, greater<int>>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power(int a, int n) {
  a %= mod;
  if (n == 1)
    return a;
  if (n == 0)
    return 1;
  if (n % 2)
    return (a * (power((a * a) % mod, n / 2) % mod)) % mod;
  return power((a * a) % mod, n / 2) % mod;
}
const int inf = (int)1e18;

int inverse(int x) {
  return power(x, mod - 2) % mod; // little fermat....
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<int> a(n + 5);
  FOR(i, 1, n + 1) cin >> a[i];
  FOR(j, 0, k) {
    vector<int> pre(4 * n + 5);
    FOR(i, 1, n + 1) {
      pre[i - a[i] + 2 * n]++;
      pre[i + a[i] + 1 + 2 * n]--;
    }
    FOR(i, 1, 4 * n + 1) pre[i] += pre[i - 1];
    int mi = 0;
    FOR(i, 2 * n + 1, 3 * n + 1) mi = min(mi, pre[i]);
    if (mi >= n)
      break;
    FOR(i, 2 * n + 1, 3 * n + 1) { a[i - 2 * n] = pre[i]; }
  }
  FOR(i, 1, n + 1) cout << min(a[i], n) << " ";
}
signed main() {
  showoff;
  int T = 1;
  // cin >> T;
  FOR(t, 1, T + 1) {
    solve();
    // cout << "\\n";
  }
  return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...