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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF ((1 << 30) - 1 + (1 << 30))
#define EPS 1.0e-6

using namespace std;

typedef long long ll;

int n, q;
int m[420000];

int init(int n) {
  int k = 1;
  while (k < n) {
    k *= 2;
  }
  for (int i = 0; i < 2 * k; i++) {
    m[i] = INF;
  }
  return k;
}

//[l,r)???x?????´??°??????.[bottom,top),node??????????????????
void update(int x, int l, int r, int bottom = 0, int top = n, int node = 1) {
  if (l <= bottom && top <= r) {
    m[node] = x;
    return;
  }

  if (top <= l || r <= bottom)
    return;

  int mid = (bottom + top) / 2;

  //-INF???????????????????????¨?????????????????????????????¨?????¨???
  if (m[node] != -INF) {
    m[2 * node] = m[node];
    m[2 * node + 1] = m[node];
    m[node] = -INF;
  }
  update(x, l, r, bottom, mid, 2 * node);
  update(x, l, r, mid, top, 2 * node + 1);
}

int find(int i, int bottom = 0, int top = n, int node = 1) {
  if (i < bottom || top <= i)
    return -INF;
  if (m[node] != -INF)
    return m[node];

  int mid = (bottom + top) / 2;
  int l = find(i, bottom, mid, 2 * node);
  int r = find(i, mid, top, 2 * node + 1);
  return max(l, r);
}

int main() {
  cin >> n >> q;

  n = init(n);

  int a, s, t, x, k;
  for (int i = 0; i < q; i++) {
    cin >> a;
    if (a == 0) {
      cin >> s >> t >> x;
      update(x, s, t + 1);
    }
    if (a == 1) {
      cin >> k;
      cout << find(k) << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF ((1 << 30) - 1 + (1 << 30))
#define EPS 1.0e-6

using namespace std;

typedef long long ll;

int n, q;
int m[210000];

int init(int n) {
  int k = 1;
  while (k < n) {
    k *= 2;
  }
  for (int i = 0; i < 2 * k; i++) {
    m[i] = INF;
  }
  return k;
}

//[l,r)???x?????´??°??????.[bottom,top),node??????????????????
void update(int x, int l, int r, int bottom = 0, int top = n, int node = 1) {
  if (l <= bottom && top <= r) {
    m[node] = x;
    return;
  }

  if (top <= l || r <= bottom)
    return;

  int mid = (bottom + top) / 2;

  //-INF???????????????????????¨?????????????????????????????¨?????¨???
  if (m[node] != -INF) {
    m[2 * node] = m[node];
    m[2 * node + 1] = m[node];
    m[node] = -INF;
  }
  update(x, l, r, bottom, mid, 2 * node);
  update(x, l, r, mid, top, 2 * node + 1);
}

int find(int i, int bottom = 0, int top = n, int node = 1) {
  if (i < bottom || top <= i)
    return -INF;
  if (m[node] != -INF)
    return m[node];

  int mid = (bottom + top) / 2;
  int l = find(i, bottom, mid, 2 * node);
  int r = find(i, mid, top, 2 * node + 1);
  return max(l, r);
}

int main() {
  cin >> n >> q;

  n = init(n);

  int a, s, t, x, k;
  for (int i = 0; i < q; i++) {
    cin >> a;
    if (a == 0) {
      cin >> s >> t >> x;
      update(x, s, t + 1);
    }
    if (a == 1) {
      cin >> k;
      cout << find(k) << endl;
    }
  }

  return 0;
}