Problem p02647 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i)
#define REP(i, y) FOR(i, 0, y)
#define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i)
#define RREP(i, x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...)                                                      \\
  cerr << "line " << __LINE__ << " : ";                                        \\
  debug_print_in(x);

template <typename First> void debug_print_in(First first) {
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest) {
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

template <typename First> void OUT(First first) {
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
};
template <class t, class u> void chmin(t &a, u b) {
  if (a > b)
    a = b;
};
int popcount(int t) { return __builtin_popcount(t); }  // GCC
int popcount(ll t) { return __builtin_popcountll(t); } // GCC

template <typename T> void vec_print(vector<T> VEC) {
  REP(i, VEC.size()) { cerr << VEC[i] << " "; }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    REP(i, MAT.size()){REP(j, MAT[i].size()){cerr << MAT[i][j] << " ";
}
cerr << endl;
}
}
;

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9) + 7);

template <typename DATA> class Segment_Tree {
private:
  ll n;
  ll n_;

  using F = function<DATA(DATA, DATA)>;
  const F f; // query function
  const DATA init;

public:
  vector<DATA> dat;
  vector<DATA>
      lazy1; // 遅延評価 - http://tsutaj.hatenablog.com/entry/2017/03/30/224339
  vector<DATA>
      lazy2; // 遅延評価 - http://tsutaj.hatenablog.com/entry/2017/03/30/224339
  vector<bool> lazy1_flag;
  vector<bool> lazy2_flag;
  Segment_Tree(ll n__, const F &f, DATA init) : f(f), init(init) {
    // For simply, the number of component is set to be power of 2
    n = 1;
    n_ = n__;
    while (n < n_)
      n *= 2;

    dat.resize(2 * n - 1, init);
    lazy1.resize(2 * n - 1, init);
    lazy2.resize(2 * n - 1, init);
    lazy1_flag.resize(2 * n - 1, false);
    lazy2_flag.resize(2 * n - 1, false);
    return;
  };

  ~Segment_Tree(void) { return; };

  void zero(void) {
    for (ll i = 0; i < 2 * n - 1; ++i) {
      dat[i] = init;
      lazy1[i] = init;
      lazy2[i] = init;
      lazy1_flag[i] = false;
      lazy2_flag[i] = false;
    }
    return;
  }

  void eval1(ll k, ll l, ll r) {
    if (!lazy1_flag[k])
      return;
    if (l >= r)
      return;
    lazy1_flag[k] = false;

    dat[k] = lazy1[k];

    if (r - l > 1) {
      lazy1[2 * k + 1] = lazy1[k];
      lazy1[2 * k + 2] = lazy1[k];
      lazy1_flag[2 * k + 1] = true;
      lazy1_flag[2 * k + 2] = true;
    }

    lazy1[k] = init;
  }

  DATA eval2_2(DATA tmp, ll n) {
    DATA ans = tmp;
    n--;
    while (n > 0) {
      if (n & 1)
        ans = f(ans, tmp);
      tmp = f(tmp, tmp);
      n >>= 1;
    }
    return ans;
  }

  void eval2(ll k, ll l, ll r) {
    if (!lazy2_flag[k])
      return;
    if (l >= r)
      return;
    lazy2_flag[k] = false;

    dat[k] = f(dat[k], eval2_2(lazy2[k], r - l));

    if (r - l > 1) {
      lazy2[2 * k + 1] = f(lazy2[2 * k + 1], lazy2[k]);
      lazy2[2 * k + 2] = f(lazy2[2 * k + 2], lazy2[k]);
      lazy2_flag[2 * k + 1] = true;
      lazy2_flag[2 * k + 2] = true;
    }

    lazy2[k] = init;
  }

  // k-th value is changed to a
  void update1(ll k, DATA a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
    return;
  };

  // k-th value is operated of a
  void update2(ll k, DATA a) {
    k += n - 1;
    dat[k] = f(dat[k], a);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
    return;
  };

  //[a,b)の範囲の値をxに変える
  void rangeupdate1(ll a, ll b, DATA x, ll k, ll l, ll r) {

    eval1(k, l, r);

    if (r <= a || b <= l)
      return;

    if (a <= l && r <= b) {
      lazy1[k] = x;
      lazy1_flag[k] = true;
      // eval1(k,l,r);
    } else {
      rangeupdate1(a, b, x, 2 * k + 1, l, (l + r) / 2);
      rangeupdate1(a, b, x, 2 * k + 2, (l + r) / 2, r);
      dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
    }
    return;
  }

  void rangeupdate1(ll a, ll b, DATA x) { rangeupdate1(a, b, x, 0, 0, n); }

  //[a,b)の範囲に対してxを作用させる
  void rangeupdate2(ll a, ll b, DATA x, ll k, ll l, ll r) {

    eval2(k, l, r);

    if (r <= a || b <= l)
      return;

    if (a <= l && r <= b) {
      lazy2[k] = f(lazy2[k], x);
      lazy2_flag[k] = true;
      eval2(k, l, r);
    } else {
      rangeupdate2(a, b, x, 2 * k + 1, l, (l + r) / 2);
      rangeupdate2(a, b, x, 2 * k + 2, (l + r) / 2, r);
      dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
    }

    return;
  }

  void rangeupdate2(ll a, ll b, DATA x) { rangeupdate2(a, b, x, 0, 0, n); }

  // search the minimum of [a, b)
  // Other values are used for simply
  // From outside, call query(a, b, 0, 0, n)
  // k is the node index,
  // temporary block is [l, r)
  DATA query(ll a, ll b, ll k, ll l, ll r) {
    // if there are no overlap between [a, b) and [l, r)
    if (r <= a || b <= l)
      return init;

    eval1(k, l, r);
    eval2(k, l, r);

    // if [a, b) contains [l, r), return the value of this node
    if (a <= l && r <= b)
      return dat[k];

    DATA vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    DATA vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return f(vl, vr);
  };

  // search the minimum of [a, b)
  DATA query(ll a, ll b) {
    if (a < 0 || n_ < b || a == b) {
      fprintf(stderr, "Segment_Tree Search Space Error\\n");
      fprintf(stderr, "Desired:\\n");
      fprintf(stderr, "%lld <= %lld < %lld <= %lld\\n", 0, a, b, n_);
      exit(0);
    }
    return query(a, b, 0, 0, n);
  };
};

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\\n";)

  ll N, K;
  IN(N, K);
  vll A(N);
  REP(i, N) { IN(A[i]); }
  vll A_new(N + 1);

  auto f = [](ll a, ll b) { return a + b; };

  Segment_Tree<ll> que(N, f, 0);

  if (K > sqrt(N) * 2 + 10) {
    REP(i, N) { A[i] = N; }
  } else {
    REP(k, K) {
      bool N_flag = true;
      REP(i, N) A_new[i] = 0;
      REP(i, N) {
        if (A[i] < N) {
          N_flag = false;
        }

        ll start = max(i - A[i], (ll)0);
        ll end = min(i + A[i] + 1, N);
        A_new[start]++;
        A_new[end]--;
      }
      if (N_flag) {
        REP(i, N) { A[i] = N; }
        break;
      }

      REP(i, N - 1) {
        A[i] = A_new[i];
        A_new[i + 1] += A_new[i];
      }
      A[N - 1] = A_new[N - 1];
    }
  }

  REP(i, N) { printf("%lld ", A[i]); }
  printf("\\n");

  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i, x, y) for (ll i = (ll)x; i < (ll)y; ++i)
#define REP(i, y) FOR(i, 0, y)
#define RFOR(i, x, y) for (ll i = (ll)x; i >= (ll)y; --i)
#define RREP(i, x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...)                                                      \\
  cerr << "line " << __LINE__ << " : ";                                        \\
  debug_print_in(x);

template <typename First> void debug_print_in(First first) {
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest) {
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void) { return; }

template <typename First, typename... Rest>
void IN(First &first, Rest &...rest) {
  cin >> first;
  IN(rest...);
  return;
}

template <typename First> void OUT(First first) {
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest) {
  cout << first << " ";
  OUT(rest...);
  return;
}

template <class t, class u> void chmax(t &a, u b) {
  if (a < b)
    a = b;
};
template <class t, class u> void chmin(t &a, u b) {
  if (a > b)
    a = b;
};
int popcount(int t) { return __builtin_popcount(t); }  // GCC
int popcount(ll t) { return __builtin_popcountll(t); } // GCC

template <typename T> void vec_print(vector<T> VEC) {
  REP(i, VEC.size()) { cerr << VEC[i] << " "; }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T>> MAT){
    REP(i, MAT.size()){REP(j, MAT[i].size()){cerr << MAT[i][j] << " ";
}
cerr << endl;
}
}
;

constexpr int INF = (1 << 30);
constexpr ll INFLL = 1LL << 62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9) + 7);

template <typename DATA> class Segment_Tree {
private:
  ll n;
  ll n_;

  using F = function<DATA(DATA, DATA)>;
  const F f; // query function
  const DATA init;

public:
  vector<DATA> dat;
  vector<DATA>
      lazy1; // 遅延評価 - http://tsutaj.hatenablog.com/entry/2017/03/30/224339
  vector<DATA>
      lazy2; // 遅延評価 - http://tsutaj.hatenablog.com/entry/2017/03/30/224339
  vector<bool> lazy1_flag;
  vector<bool> lazy2_flag;
  Segment_Tree(ll n__, const F &f, DATA init) : f(f), init(init) {
    // For simply, the number of component is set to be power of 2
    n = 1;
    n_ = n__;
    while (n < n_)
      n *= 2;

    dat.resize(2 * n - 1, init);
    lazy1.resize(2 * n - 1, init);
    lazy2.resize(2 * n - 1, init);
    lazy1_flag.resize(2 * n - 1, false);
    lazy2_flag.resize(2 * n - 1, false);
    return;
  };

  ~Segment_Tree(void) { return; };

  void zero(void) {
    for (ll i = 0; i < 2 * n - 1; ++i) {
      dat[i] = init;
      lazy1[i] = init;
      lazy2[i] = init;
      lazy1_flag[i] = false;
      lazy2_flag[i] = false;
    }
    return;
  }

  void eval1(ll k, ll l, ll r) {
    if (!lazy1_flag[k])
      return;
    if (l >= r)
      return;
    lazy1_flag[k] = false;

    dat[k] = lazy1[k];

    if (r - l > 1) {
      lazy1[2 * k + 1] = lazy1[k];
      lazy1[2 * k + 2] = lazy1[k];
      lazy1_flag[2 * k + 1] = true;
      lazy1_flag[2 * k + 2] = true;
    }

    lazy1[k] = init;
  }

  DATA eval2_2(DATA tmp, ll n) {
    DATA ans = tmp;
    n--;
    while (n > 0) {
      if (n & 1)
        ans = f(ans, tmp);
      tmp = f(tmp, tmp);
      n >>= 1;
    }
    return ans;
  }

  void eval2(ll k, ll l, ll r) {
    if (!lazy2_flag[k])
      return;
    if (l >= r)
      return;
    lazy2_flag[k] = false;

    dat[k] = f(dat[k], eval2_2(lazy2[k], r - l));

    if (r - l > 1) {
      lazy2[2 * k + 1] = f(lazy2[2 * k + 1], lazy2[k]);
      lazy2[2 * k + 2] = f(lazy2[2 * k + 2], lazy2[k]);
      lazy2_flag[2 * k + 1] = true;
      lazy2_flag[2 * k + 2] = true;
    }

    lazy2[k] = init;
  }

  // k-th value is changed to a
  void update1(ll k, DATA a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
    return;
  };

  // k-th value is operated of a
  void update2(ll k, DATA a) {
    k += n - 1;
    dat[k] = f(dat[k], a);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = f(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
    return;
  };

  //[a,b)の範囲の値をxに変える
  void rangeupdate1(ll a, ll b, DATA x, ll k, ll l, ll r) {

    eval1(k, l, r);

    if (r <= a || b <= l)
      return;

    if (a <= l && r <= b) {
      lazy1[k] = x;
      lazy1_flag[k] = true;
      // eval1(k,l,r);
    } else {
      rangeupdate1(a, b, x, 2 * k + 1, l, (l + r) / 2);
      rangeupdate1(a, b, x, 2 * k + 2, (l + r) / 2, r);
      dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
    }
    return;
  }

  void rangeupdate1(ll a, ll b, DATA x) { rangeupdate1(a, b, x, 0, 0, n); }

  //[a,b)の範囲に対してxを作用させる
  void rangeupdate2(ll a, ll b, DATA x, ll k, ll l, ll r) {

    eval2(k, l, r);

    if (r <= a || b <= l)
      return;

    if (a <= l && r <= b) {
      lazy2[k] = f(lazy2[k], x);
      lazy2_flag[k] = true;
      eval2(k, l, r);
    } else {
      rangeupdate2(a, b, x, 2 * k + 1, l, (l + r) / 2);
      rangeupdate2(a, b, x, 2 * k + 2, (l + r) / 2, r);
      dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
    }

    return;
  }

  void rangeupdate2(ll a, ll b, DATA x) { rangeupdate2(a, b, x, 0, 0, n); }

  // search the minimum of [a, b)
  // Other values are used for simply
  // From outside, call query(a, b, 0, 0, n)
  // k is the node index,
  // temporary block is [l, r)
  DATA query(ll a, ll b, ll k, ll l, ll r) {
    // if there are no overlap between [a, b) and [l, r)
    if (r <= a || b <= l)
      return init;

    eval1(k, l, r);
    eval2(k, l, r);

    // if [a, b) contains [l, r), return the value of this node
    if (a <= l && r <= b)
      return dat[k];

    DATA vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    DATA vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return f(vl, vr);
  };

  // search the minimum of [a, b)
  DATA query(ll a, ll b) {
    if (a < 0 || n_ < b || a == b) {
      fprintf(stderr, "Segment_Tree Search Space Error\\n");
      fprintf(stderr, "Desired:\\n");
      fprintf(stderr, "%lld <= %lld < %lld <= %lld\\n", 0, a, b, n_);
      exit(0);
    }
    return query(a, b, 0, 0, n);
  };
};

int main() {
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after
              // std::cin)
  ios::sync_with_stdio(
      false); // cut the iostream and stdio (DON'T endl; BUT "\\n";)

  ll N, K;
  IN(N, K);
  vll A(N);
  REP(i, N) { IN(A[i]); }
  vll A_new(N);

  auto f = [](ll a, ll b) { return a + b; };

  Segment_Tree<ll> que(N, f, 0);

  if (K > sqrt(N) * 2 + 10) {
    REP(i, N) { A[i] = N; }
  } else {
    REP(k, K) {
      bool N_flag = true;
      REP(i, N) A_new[i] = 0;
      REP(i, N) {
        if (A[i] < N) {
          N_flag = false;
        }

        ll start = max(i - A[i], (ll)0);
        ll end = min(i + A[i] + 1, N);
        A_new[start]++;
        A_new[end]--;
      }
      if (N_flag) {
        REP(i, N) { A[i] = N; }
        break;
      }

      REP(i, N - 1) {
        A[i] = A_new[i];
        A_new[i + 1] += A_new[i];
      }
      A[N - 1] = A_new[N - 1];
    }
  }

  REP(i, N) { printf("%lld ", A[i]); }
  printf("\\n");

  return 0;
}