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

/*!enjoy karo yaar!*/

// CF,CC,AtC,SPOJ: hp1999
// HE: hemant269
// HR: hemant2132

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

#define int long long int
#define fast()                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define rz(x) resize(x)
#define mem(a, b) memset(a, b, sizeof(a))
#define sz(x) ((int)(x.size()))
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ins insert
#define pii pair<int, int>
#define F first
#define S second
#define makep make_pair
#define maket make_tuple
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define bitcount(x) __builtin_popcountll(x)
#define iceil(n, x) (((n) + (x)-1) / (x))
#define gcd(a, b) (__gcd((a), (b)))
#define lcm(a, b) (((a) * (b)) / gcd((a), (b)))
#define dbug(x) cout << #x << ": " << (x) << "\\n"
#define flush fflush(stdout)
#define show(x)                                                                \\
  for (auto c : x)                                                             \\
    cout << c << " ";                                                          \\
  cout << "\\n";
#define show2(x)                                                               \\
  for (auto c : x)                                                             \\
    cout << c.F << " " << c.S << "\\n";

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

const ld pi = acos(-1);
const ll inf = 1e18, M = 1e9 + 7;
const int N = 2e5 + 10;

int tree[2][4 * N];

void update(int node, int beg, int en, int idx, int val, int pos) {
  if (beg == en) {
    // base[idx]=val;
    tree[pos][node] = val;
    return;
  }

  int mid = (beg + en) / 2;
  if (beg <= idx && idx <= mid)
    update(2 * node, beg, mid, idx, val, pos);
  else
    update(2 * node + 1, mid + 1, en, idx, val, pos);

  tree[pos][node] = max(tree[pos][2 * node], tree[pos][2 * node + 1]);
}

int query(int node, int beg, int en, int l, int r, int pos) {
  if (r < beg || en < l)
    return 0;

  if (l <= beg && en <= r)
    return tree[pos][node];

  int mid = (beg + en) / 2;
  int q1 = query(2 * node, beg, mid, l, r, pos);
  int q2 = query(2 * node + 1, mid + 1, en, l, r, pos);

  return max(q1, q2);
}

void solve() {
  mem(tree, 0);

  int n, k, c;
  cin >> n >> k >> c;

  string s;
  cin >> s;

  vector<int> pre(n, 0), suf(n, 0);

  for (int i = 0; i < c; ++i) {
    if (s[i] == 'o') {
      pre[i] = 1;
      update(1, 1, n, i + 1, 1, 0);
    }
  }

  for (int i = c; i < n; ++i) {
    if (s[i] == 'o') {
      pre[i] = query(1, 1, n, 1, i - c, 0) + 1;
      update(1, 1, n, i + 1, pre[i], 0);
    }
  }

  for (int i = n - 1; i >= n - c; --i) {
    if (s[i] == 'o') {
      suf[i] = 1;
      update(1, 1, n, i + 1, 1, 1);
    }
  }

  for (int i = n - c - 1; i >= 0; --i) {
    if (s[i] == 'o') {
      suf[i] = query(1, 1, n, i + c + 2, n, 1) + 1;
      update(1, 1, n, i + 1, suf[i], 1);
    }
  }

  // show(pre);
  // show(suf);

  int left, right;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'x')
      continue;

    if (i == 0) {
      left = 0;
      right = query(1, 1, n, i + 2, n, 1);
    } else if (i == n - 1) {
      left = query(1, 1, n, 1, i, 0);
      right = 0;
    } else {
      left = query(1, 1, n, 1, i, 0);
      right = query(1, 1, n, i + 2, n, 1);
    }

    if (left + right < k) {
      left = query(1, 1, n, 1, i + 1, 0);
      right = query(1, 1, n, i + 1, n, 1);
      if (left + right - 1 >= k)
        cout << i + 1 << "\\n";
    }
  }
}

int32_t main() {
  fast();

  int t = 1;
  // cin>>t;
  for (int z = 1; z <= t; ++z) {
    solve();
    // cout<<"\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

/*!enjoy karo yaar!*/

// CF,CC,AtC,SPOJ: hp1999
// HE: hemant269
// HR: hemant2132

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

#define int long long int
#define fast()                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define rz(x) resize(x)
#define mem(a, b) memset(a, b, sizeof(a))
#define sz(x) ((int)(x.size()))
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define ins insert
#define pii pair<int, int>
#define F first
#define S second
#define makep make_pair
#define maket make_tuple
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define bitcount(x) __builtin_popcountll(x)
#define iceil(n, x) (((n) + (x)-1) / (x))
#define gcd(a, b) (__gcd((a), (b)))
#define lcm(a, b) (((a) * (b)) / gcd((a), (b)))
#define dbug(x) cout << #x << ": " << (x) << "\\n"
#define flush fflush(stdout)
#define show(x)                                                                \\
  for (auto c : x)                                                             \\
    cout << c << " ";                                                          \\
  cout << "\\n";
#define show2(x)                                                               \\
  for (auto c : x)                                                             \\
    cout << c.F << " " << c.S << "\\n";

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

const ld pi = acos(-1);
const ll inf = 1e18, M = 1e9 + 7;
const int N = 2e5 + 10;

int tree[2][N];

void update(int node, int beg, int en, int idx, int val, int pos) {
  if (beg == en) {
    // base[idx]=val;
    tree[pos][node] = val;
    return;
  }

  int mid = (beg + en) / 2;
  if (beg <= idx && idx <= mid)
    update(2 * node, beg, mid, idx, val, pos);
  else
    update(2 * node + 1, mid + 1, en, idx, val, pos);

  tree[pos][node] = max(tree[pos][2 * node], tree[pos][2 * node + 1]);
}

int query(int node, int beg, int en, int l, int r, int pos) {
  if (r < beg || en < l)
    return 0;

  if (l <= beg && en <= r)
    return tree[pos][node];

  int mid = (beg + en) / 2;
  int q1 = query(2 * node, beg, mid, l, r, pos);
  int q2 = query(2 * node + 1, mid + 1, en, l, r, pos);

  return max(q1, q2);
}

void solve() {
  mem(tree, 0);

  int n, k, c;
  cin >> n >> k >> c;

  string s;
  cin >> s;

  vector<int> pre(n, 0), suf(n, 0);

  for (int i = 0; i < c; ++i) {
    if (s[i] == 'o') {
      pre[i] = 1;
      update(1, 1, n, i + 1, 1, 0);
    }
  }

  for (int i = c; i < n; ++i) {
    if (s[i] == 'o') {
      pre[i] = query(1, 1, n, 1, i - c, 0) + 1;
      update(1, 1, n, i + 1, pre[i], 0);
    }
  }

  for (int i = n - 1; i >= n - c; --i) {
    if (s[i] == 'o') {
      suf[i] = 1;
      update(1, 1, n, i + 1, 1, 1);
    }
  }

  for (int i = n - c - 1; i >= 0; --i) {
    if (s[i] == 'o') {
      suf[i] = query(1, 1, n, i + c + 2, n, 1) + 1;
      update(1, 1, n, i + 1, suf[i], 1);
    }
  }

  // show(pre);
  // show(suf);

  int left, right;
  for (int i = 0; i < n; ++i) {
    if (s[i] == 'x')
      continue;

    if (i == 0) {
      left = 0;
      right = query(1, 1, n, i + 2, n, 1);
    } else if (i == n - 1) {
      left = query(1, 1, n, 1, i, 0);
      right = 0;
    } else {
      left = query(1, 1, n, 1, i, 0);
      right = query(1, 1, n, i + 2, n, 1);
    }

    if (left + right < k) {
      left = query(1, 1, n, 1, i + 1, 0);
      right = query(1, 1, n, i + 1, n, 1);
      if (left + right - 1 >= k)
        cout << i + 1 << "\\n";
    }
  }
}

int32_t main() {
  // fast();

  int t = 1;
  // cin>>t;
  for (int z = 1; z <= t; ++z) {
    solve();
    // cout<<"\\n";
  }

  return 0;
}