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

/* Bismillahirrahmanirrahim */
/* Author : Jehian Norman Saviero (@Reiva5) */
#include <bits/stdc++.h>

// MACROS SAMPAH
#define Jehian using
#define Norman namespace
#define Saviero std

Jehian Norman Saviero;

// MACROS TYPE
typedef long l;
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef double ld;
typedef pair<int, int> pi;
typedef pair<l, l> pl;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<l> vl;
typedef vector<ll> vll;

// MACROS FOR PRINT
#define nl printf("\\n")
#define sp printf(" ")

// MACROS FOR ATTRIBUTE
#define fi first
#define se second

// MACROS FOR FUNCTION AND METHOD
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ALL(x) (x).begin(), (x).end()
#define ALLN(x, n) (x), (x) + (n)
#define CUBE(x) (SQR(x) * (x))
#define SET(x, y)                                                              \\
  for (int _qwe = sizeof(x) / sizeof(x[0]); _qwe;)                             \\
  x[--_qwe] = (y)
#define SQR(x) ((x) * (x))
#define SSORT(x, sz) sort(ALLN(x, sz))
#define RESET(x) memset((x), 0, sizeof(x))
#define VSORT(x) sort(ALL(x))

// MACROS READ INPUT
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%ld", &x)
#define scll(x) scanf("%lld", &x)
#define scul(x) scl(x)
#define scull(x) scll(x)

const int sz = 1e6;
int seg[2][sz], lazy[sz], a[sz];
int N, M, X, L, R, Y, Z, K;

void check(int p, int s, int e) {
  if (lazy[p] != 0) {
    seg[0][p] += lazy[p];
    // seg[1][p] += lazy[p];
    if (s != e) {
      lazy[2 * p] += lazy[p];
      lazy[2 * p + 1] += lazy[p];
    }
    lazy[p] = 0;
  }
}

void build(int p, int s, int e) {
  check(p, s, e);
  if (s == e) {
    seg[0][p] = a[s];
    // seg[1][p] = a[s];
    return;
  }

  build(2 * p, s, (s + e) / 2);
  build(2 * p + 1, (s + e) / 2 + 1, e);

  seg[0][p] = min(seg[0][2 * p], seg[0][2 * p + 1]);
  // seg[1][p] = max(seg[1][2*p], seg[1][2*p+1]);
}

void update(int p, int s, int e, int a, int b, int v) {
  check(p, s, e);
  if (s >= a && e <= b) {
    seg[0][p] += v;
    // seg[1][p] += v;
    if (s != e) {
      lazy[2 * p] += v;
      lazy[2 * p + 1] += v;
    }
    return;
  }
  if (s > b || e < a) {
    return;
  }
  update(2 * p, s, (s + e) / 2, a, b, v);
  update(2 * p + 1, (s + e) / 2 + 1, e, a, b, v);
  seg[0][p] = min(seg[0][2 * p], seg[0][2 * p + 1]);
  // seg[1][p] = max(seg[1][2*p], seg[1][2*p+1]);
}

int getMin(int p, int s, int e, int a, int b) {
  check(p, s, e);

  if (s >= a && e <= b) {
    return seg[0][p];
  }

  if (s > b || e < a) {
    return INT_MAX;
  }

  return min(getMin(2 * p, s, (s + e) / 2, a, b),
             getMin(2 * p + 1, (s + e) / 2 + 1, e, a, b));
}

int getMax(int p, int s, int e, int a, int b) {
  check(p, s, e);

  if (s >= a && e <= b) {
    return seg[1][p];
  }

  if (s > b || e < a) {
    return INT_MIN;
  }

  return max(getMax(2 * p, s, (s + e) / 2, a, b),
             getMax(2 * p + 1, (s + e) / 2 + 1, e, a, b));
}

int main() {
  sci(N), sci(K);
  if (K > 40) {
    int tmp = N;
    while (tmp--)
      printf("%d ", N);
    printf("\\n");
    return 0;
  }
  for (int i = 0; i < N; ++i) {
    sci(a[i]);
  }
  build(1, 0, N - 1);
  vi dat(N);
  while (K--) {
    for (int i = 0; i < N; ++i) {
      dat[i] = getMin(1, 0, N - 1, i, i);
    }
    RESET(lazy), RESET(seg);
    for (int i = 0; i < N; ++i) {
      update(1, 0, N - 1, max(0, i - dat[i]), min(N - 1, i + dat[i]), 1);
    }
  }
  for (int i = 0; i < N; ++i) {
    printf("%d ", getMin(1, 0, N - 1, i, i));
  }
  printf("\\n");
  return 0;
}


Error: TLE

Fixed submission:

/* Bismillahirrahmanirrahim */
/* Author : Jehian Norman Saviero (@Reiva5) */
#include <bits/stdc++.h>

// MACROS SAMPAH
#define Jehian using
#define Norman namespace
#define Saviero std

Jehian Norman Saviero;

// MACROS TYPE
typedef long l;
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef double ld;
typedef pair<int, int> pi;
typedef pair<l, l> pl;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<l> vl;
typedef vector<ll> vll;

// MACROS FOR PRINT
#define nl printf("\\n")
#define sp printf(" ")

// MACROS FOR ATTRIBUTE
#define fi first
#define se second

// MACROS FOR FUNCTION AND METHOD
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ALL(x) (x).begin(), (x).end()
#define ALLN(x, n) (x), (x) + (n)
#define CUBE(x) (SQR(x) * (x))
#define SET(x, y)                                                              \\
  for (int _qwe = sizeof(x) / sizeof(x[0]); _qwe;)                             \\
  x[--_qwe] = (y)
#define SQR(x) ((x) * (x))
#define SSORT(x, sz) sort(ALLN(x, sz))
#define RESET(x) memset((x), 0, sizeof(x))
#define VSORT(x) sort(ALL(x))

// MACROS READ INPUT
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%ld", &x)
#define scll(x) scanf("%lld", &x)
#define scul(x) scl(x)
#define scull(x) scll(x)

const int sz = 6e5;
int seg[sz], lazy[sz], a[sz], dat[sz];
int N, M, X, L, R, Y, Z, K;

void check(int p, int s, int e) {
  if (lazy[p] != 0) {
    seg[p] += lazy[p];
    // seg[1][p] += lazy[p];
    if (s != e) {
      lazy[2 * p] += lazy[p];
      lazy[2 * p + 1] += lazy[p];
    }
    lazy[p] = 0;
  }
}

void build(int p, int s, int e) {
  check(p, s, e);
  if (s == e) {
    seg[p] = a[s];
    // seg[1][p] = a[s];
    return;
  }

  build(2 * p, s, (s + e) / 2);
  build(2 * p + 1, (s + e) / 2 + 1, e);

  seg[p] = min(seg[2 * p], seg[2 * p + 1]);
  // seg[1][p] = max(seg[1][2*p], seg[1][2*p+1]);
}

void update(int p, int s, int e, int a, int b, int v) {
  check(p, s, e);
  if (s >= a && e <= b) {
    seg[p] += v;
    // seg[1][p] += v;
    if (s != e) {
      lazy[2 * p] += v;
      lazy[2 * p + 1] += v;
    }
    return;
  }
  if (s > b || e < a) {
    return;
  }
  update(2 * p, s, (s + e) / 2, a, b, v);
  update(2 * p + 1, (s + e) / 2 + 1, e, a, b, v);
  seg[p] = min(seg[2 * p], seg[2 * p + 1]);
  // seg[1][p] = max(seg[1][2*p], seg[1][2*p+1]);
}

int getMin(int p, int s, int e, int a, int b) {
  check(p, s, e);

  if (s >= a && e <= b) {
    return seg[p];
  }

  if (s > b || e < a) {
    return INT_MAX;
  }

  return min(getMin(2 * p, s, (s + e) / 2, a, b),
             getMin(2 * p + 1, (s + e) / 2 + 1, e, a, b));
}

int main() {
  sci(N), sci(K);
  if (K > 40) {
    for (int i = 0; i < N; ++i) {
      printf("%d ", N);
    }
    printf("\\n");
  } else {
    for (int i = 0; i < N; ++i) {
      sci(a[i]);
    }
    build(1, 0, N - 1);
    while (K--) {
      for (int i = 0; i < N; ++i) {
        dat[i] = getMin(1, 0, N - 1, i, i);
      }
      RESET(seg);
      for (int i = 0; i < N; ++i) {
        update(1, 0, N - 1, max(0, i - dat[i]), min(N - 1, i + dat[i]), 1);
      }
    }
    for (int i = 0; i < N; ++i) {
      printf("%d ", getMin(1, 0, N - 1, i, i));
    }
    printf("\\n");
  }
  return 0;
}