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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const int INF = 1e18 + 10;

/*----------------------------------------------*/

signed main() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, n) cin >> a[i];

  while (k--) {
    vi b(n, 0);
    int all = 0;
    int mae = 0, ushi = -1;
    vector<pair<int, int>> memo(2 * n); // second:0(mae),1(ushi)
    rep(i, n) {
      int x = max(0ll, i - a[i]), y = min(i + a[i], n - 1);
      memo[2 * i] = (make_pair(x, 0));
      memo[2 * i + 1] = (make_pair(y, 1));
    }
    sort(all(memo));

    int cnt = 0;
    rep(i, n) {
      int x = upper_bound(all(memo), make_pair(i, 0ll)) -
              lower_bound(all(memo), make_pair(i, 0ll));
      cnt += x;
      b[i] = cnt;
      int y = upper_bound(all(memo), make_pair(i, 1ll)) -
              lower_bound(all(memo), make_pair(i, 1ll));
      cnt -= y;
    }

    bool ok = 1;
    rep(i, n) {
      a[i] = b[i];
      if (a[i] < n) {
        ok = 0;
      }
    }

    if (ok)
      break;
  }

  rep(i, n) {
    cout << a[i];
    if (i < n - 1)
      cout << ' ';
  }
  cout << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const int INF = 1e18 + 10;

/*----------------------------------------------*/

signed main() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  rep(i, n) cin >> a[i];

  while (k--) {
    vi b(n, 0);
    int all = 0;
    rep(i, n) {
      if (i - a[i] <= 0 && n - 1 <= i + a[i]) {
        all++;
      } else {
        FOR(j, max(0ll, i - a[i]), min(i + a[i], n - 1)) { b[j]++; }
      }
    }

    bool ok = 1;
    rep(i, n) {
      a[i] = b[i] + all;
      if (a[i] < n) {
        ok = 0;
      }
    }

    if (ok)
      break;
  }

  rep(i, n) {
    cout << a[i];
    if (i < n - 1)
      cout << ' ';
  }
  cout << endl;
  return 0;
}