alexjercan/bugnet huggingface dataset:
Problem p02346 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

template <typename T> struct Add {
  T operator()(T x, T y) { return x + y; }
};

template <class T, class Functor> struct SegmentTree {
  size_t n;
  T e;
  vector<T> tree;
  Functor F;
  SegmentTree(size_t m, T e = T()) : n(1), e(e) {
    while (n <= m)
      n <<= 1;
    tree.assign(n << 1, e);
  }
  T range_query(size_t a, size_t b, size_t i = 1, size_t l = 0, size_t r = -1) {
    if (!~r)
      r = n;
    if (a <= l && r <= b)
      return tree[i];

    T vl = e, vr = e;
    size_t c = (l + r) >> 1;

    if (a < c)
      vl = F(e, range_query(a, b, i << 1 | 0, l, c));
    if (c < b)
      vr = F(e, range_query(a, b, i << 1 | 1, c, r));

    return F(vl, vr);
  }
  void update(size_t i, T x) {
    i += n;
    tree[i] = x;
    while (i >>= 1)
      tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
  }
  void add(size_t i, T x) {
    i += n;
    tree[i] += x;
    while (i >>= 1)
      tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
  }
};

int main() {
  size_t n, q;
  scanf("%zu %zu", &n, &q);

  SegmentTree<int, Add<int>> tree(n);
  for (size_t i = 0; i < q; ++i) {
    int com;
    scanf("%d", &com);
    if (com) {
      size_t s, t;
      scanf("%zu %zu", &s, &t);
      printf("%d\\n", tree.range_query(s, t + 1));
    } else {
      size_t i;
      int x;
      scanf("%zu %d", &i, &x);
      tree.add(i, x);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

template <typename T> struct Add {
  T operator()(T x, T y) { return x + y; }
};

template <class T, class Functor> struct SegmentTree {
  size_t n;
  T e;
  vector<T> tree;
  Functor F;
  SegmentTree(size_t m, T e = T()) : n(1), e(e) {
    while (n < m)
      n <<= 1;
    tree.assign(n << 1, e);
  }
  T range_query(size_t a, size_t b, size_t i = 1, size_t l = 0, size_t r = -1) {
    if (!~r)
      r = n;
    if (a <= l && r <= b)
      return tree[i];

    T vl = e, vr = e;
    size_t c = (l + r) >> 1;

    if (a < c)
      vl = F(e, range_query(a, b, i << 1 | 0, l, c));
    if (c < b)
      vr = F(e, range_query(a, b, i << 1 | 1, c, r));

    return F(vl, vr);
  }
  void update(size_t i, T x) {
    i += n;
    tree[i] = x;
    while (i >>= 1)
      tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
  }
  void add(size_t i, T x) {
    i += n;
    tree[i] += x;
    while (i >>= 1)
      tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
  }
};

int main() {
  size_t n, q;
  scanf("%zu %zu", &n, &q);

  SegmentTree<int, Add<int>> tree(n);
  for (size_t i = 0; i < q; ++i) {
    int com;
    scanf("%d", &com);
    if (com) {
      size_t s, t;
      scanf("%zu %zu", &s, &t);
      printf("%d\\n", tree.range_query(s, t + 1));
    } else {
      size_t i;
      int x;
      scanf("%zu %d", &i, &x);
      tree.add(i, x);
    }
  }

  return 0;
}
Problem p02347 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <list>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

template <class T> class _Point {
public:
  T x;
  T y;
  int index; // max 500000;
};

typedef int R;
typedef _Point<R> Point;

array<Point, 500000> A;
typedef pair<R, int> X_IDX;

int main() {
  std::istream &c_in = cin;

  int n;
  c_in >> n;
  for (int i = 0; i < n; ++i) {
    Point &p = A[i];
    c_in >> p.x >> p.y;
    p.index = i;
  }
  vector<X_IDX> LA;
  LA.resize(n);
  for (size_t j = 0; j < LA.size(); ++j) {
    LA[j] = X_IDX(A[j].x, A[j].index);
  }
  sort(LA.begin(), LA.end());

  int q;
  c_in >> q;

  vector<int> match_idx;
  match_idx.resize(LA.size());

  for (int j = 0; j < q; ++j) {
    R sx, tx, sy, ty;
    c_in >> sx >> tx >> sy >> ty;

    int size_of_match = 0;
    // for (vector<X_IDX>::iterator it = LA.begin(); it != LA.end(); ++it)
    X_IDX xx(sx, 0);
    vector<X_IDX>::iterator it = lower_bound(LA.begin(), LA.end(), xx);
    for (; it != LA.end(); ++it) {
      if (it->first < sx) {
        continue;
      }
      if (it->first > tx) {
        break;
      }
      int &y = A[it->second].y;
      if (sy <= y && y <= ty) {
        match_idx[size_of_match++] = it->second;
      }
    }
    sort(match_idx.begin(), match_idx.begin() + size_of_match);
    for (int k = 0; k < size_of_match; ++k) {
      printf("%d\\n", match_idx[k]);
    }
    printf("\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <fstream>
#include <iostream>
#include <list>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

template <class T> class _Point {
public:
  T x;
  T y;
  int index; // max 500000;
};

typedef int R;
typedef _Point<R> Point;

array<Point, 500000> A;
typedef pair<R, int> X_IDX;

int main() {
  std::istream &c_in = cin;

  int n;
  c_in >> n;
  for (int i = 0; i < n; ++i) {
    Point &p = A[i];
    c_in >> p.x >> p.y;
    p.index = i;
  }
  vector<X_IDX> LA;
  LA.resize(n);
  for (size_t j = 0; j < LA.size(); ++j) {
    LA[j] = X_IDX(A[j].x, A[j].index);
  }
  sort(LA.begin(), LA.end());

  int q;
  c_in >> q;
  //	string msg;
  for (int j = 0; j < q; ++j) {
    R sx, tx, sy, ty;
    c_in >> sx >> tx >> sy >> ty;

    vector<int> match_idx;
    match_idx.resize(LA.size());
    int size_of_match = 0;

    for (vector<X_IDX>::iterator it = LA.begin(); it != LA.end(); ++it) {
      if (it->first < sx) {
        continue;
      }
      if (it->first > tx) {
        break;
      }
      int &y = A[it->second].y;
      if (sy <= y && y <= ty) {
        // match_idx.push_back(it->index);
        match_idx[size_of_match++] = it->second;
      }
    }
    sort(match_idx.begin(), match_idx.begin() + size_of_match);
    for (int k = 0; k < size_of_match; ++k) {
      printf("%d\\n", match_idx[k]);
    }
    printf("\\n");
  }
}
Problem p02347 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
int n;
int q;
int sx, tx, sy, ty;
vector<int> viAns;

struct data {
  int x, y;
  int index;
};
data pData[500005];
struct node {
  int x, y;
  node *l;
  node *r;
  int index;
};

node *root;

bool cmpx(data a, data b) { return a.x < b.x; }

bool cmpy(data a, data b) { return a.y < b.y; }

node *addNode(int l, int r, bool d) {
  if (l >= r)
    return NULL;
  node *now = new node;
  int mid = (l + r) / 2;
  if (!d)
    sort(pData + l, pData + r, cmpx);
  else
    sort(pData + l, pData + r, cmpy);
  now->x = pData[mid].x;
  now->y = pData[mid].y;
  now->index = pData[mid].index;

  now->l = addNode(l, mid, !d);
  now->r = addNode(mid + 1, r, !d);
  return now;
}

void Find(node *n, bool d) {
  if (n->x >= sx && n->x <= tx && n->y >= sy && n->y <= ty)
    viAns.push_back(n->index);
  if (d) {
    if (sy <= n->y && n->l)
      Find(n->l, !d);
    if (ty >= n->y && n->r)
      Find(n->r, !d);
  } else {
    if (sx <= n->x && n->l)
      Find(n->l, !d);
    if (tx >= n->x && n->r)
      Find(n->r, !d);
  }
}

int main() {
  cin >> n;
  root = new node;
  root->l = NULL;
  root->r = NULL;
  for (int i = 0; i < n; i++) {
    cin >> pData[i].x >> pData[i].y;
    pData[i].index = i;
  }
  root = addNode(0, n, true);
  cin >> q;
  while (q--) {
    cin >> sx >> tx >> sy >> ty;
    viAns.clear();
    Find(root, true);
    sort(viAns.begin(), viAns.end());
    for (int i = 0; i < viAns.size(); i++)
      printf("%d\\n", viAns[i]);
    printf("\\n");
  }
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
struct aa {
  int x;
  int y;
} aa[500000];
struct a {
  int sx;
  int tx;
  int sy;
  int ty;
} a[20000];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d %d", &aa[i].x, &aa[i].y);
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d%d", &a[i].sx, &a[i].tx, &a[i].sy, &a[i].ty);
    for (int j = 0; j < n; j++) {
      if (a[i].sx <= aa[j].x && aa[j].x <= a[i].tx && a[i].sy <= aa[j].y &&
          aa[j].y <= a[i].ty)
        printf("%d\\n", j);
    }
    printf("\\n");
  }
  return 0;
}
Problem p02347 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

typedef pair<pair<int, int>, int> point;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, x, y;
  point *S, *T;
  cin >> n;
  S = new point[n];
  T = new point[n];

  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    S[i] = make_pair(make_pair(x, y), i);
    T[i] = make_pair(make_pair(y, x), i);
  }
  sort(S, S + n);
  sort(T, T + n);

  int q, sx, tx, sy, ty;
  priority_queue<int, vector<int>, greater<int>> que;
  cin >> q;
  while (q--) {
    cin >> sx >> tx >> sy >> ty;

    int x_low = lower_bound(S, S + n, make_pair(make_pair(sx, sy), 0)) - S;
    int x_high = upper_bound(S, S + n, make_pair(make_pair(tx, ty), n)) - S;
    int y_low = lower_bound(T, T + n, make_pair(make_pair(sy, sx), 0)) - T;
    int y_high = upper_bound(T, T + n, make_pair(make_pair(ty, tx), n)) - T;
    int x_width = x_high - x_low;
    int y_width = y_high - y_low;

    if (x_width < y_width) {
      for (int i = x_low; i < x_high; i++) {
        int y = S[i].first.second;
        if (sy <= y && y <= ty) {
          que.push(S[i].second);
        }
      }
    } else {
      for (int i = y_low; i < y_high; i++) {
        int x = T[i].first.second;
        if (sx <= x && x <= tx) {
          que.push(T[i].second);
        }
      }
    }

    while (!que.empty()) {
      cout << que.top() << "\\n";
      que.pop();
    }
    cout << "\\n";
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>

using namespace std;

typedef pair<pair<int, int>, int> point;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, x, y;
  cin >> n;
  point S[n], T[n];

  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    S[i] = make_pair(make_pair(x, y), i);
    T[i] = make_pair(make_pair(y, x), i);
  }
  sort(S, S + n);
  sort(T, T + n);

  int q, sx, tx, sy, ty;
  priority_queue<int, vector<int>, greater<int>> que;
  cin >> q;
  while (q--) {
    cin >> sx >> tx >> sy >> ty;

    int x_low = lower_bound(S, S + n, make_pair(make_pair(sx, sy), 0)) - S;
    int x_high = upper_bound(S, S + n, make_pair(make_pair(tx, ty), n)) - S;
    int y_low = lower_bound(T, T + n, make_pair(make_pair(sy, sx), 0)) - T;
    int y_high = upper_bound(T, T + n, make_pair(make_pair(ty, tx), n)) - T;
    int x_width = x_high - x_low;
    int y_width = y_high - y_low;

    if (x_width < y_width) {
      for (int i = x_low; i < x_high; i++) {
        int y = S[i].first.second;
        if (sy <= y && y <= ty) {
          que.push(S[i].second);
        }
      }
    } else {
      for (int i = y_low; i < y_high; i++) {
        int x = T[i].first.second;
        if (sx <= x && x <= tx) {
          que.push(T[i].second);
        }
      }
    }

    while (!que.empty()) {
      cout << que.top() << "\\n";
      que.pop();
    }
    cout << "\\n";
  }

  return 0;
}
Problem p02347 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

class Node {
public:
  int location;
  int p, l, r;
  Node() {}
};

class Point {
public:
  int id, x, y;
  Point() {}
  Point(int id, int x, int y) : id(id), x(x), y(y) {}
  bool operator<(const Point &p) const { return id < p.id; }

  void print() { printf("%d\\n", id); }
};

static const int MAX = 1000000;
static const int NIL = -1;

int N;
Point P[MAX];
Node T[MAX];
int np;

bool lessX(const Point &p1, const Point &p2) { return p1.x < p2.x; }

bool lessY(const Point &p1, const Point &p2) { return p1.y < p2.y; }

int makeKDTree(int l, int r, int depth) {
  if (!(l < r))
    return NIL;
  int mid = (l + r) / 2;
  int t = np++;
  if (depth % 2 == 0) {
    sort(P + l, P + r, lessX);
  } else {
    sort(P + l, P + r, lessY);
  }
  T[t].location = mid;
  T[t].l = makeKDTree(l, mid, depth + 1);
  T[t].r = makeKDTree(mid + 1, r, depth + 1);

  return t;
}

void find(int v, int sx, int tx, int sy, int ty, int depth,
          vector<Point> &ans) {
  int x = P[T[v].location].x;
  int y = P[T[v].location].y;

  if (sx <= x && x <= tx && sy <= y && y <= ty) {
    ans.push_back(P[T[v].location]);
  }

  if (depth % 2 == 0) {
    if (T[v].l != NIL) {
      if (sx <= x)
        find(T[v].l, sx, tx, sy, ty, depth + 1, ans);
    }
    if (T[v].r != NIL) {
      if (x <= tx)
        find(T[v].r, sx, tx, sy, ty, depth + 1, ans);
    }
  } else {
    if (T[v].l != NIL) {
      if (sy <= y)
        find(T[v].l, sx, tx, sy, ty, depth + 1, ans);
    }
    if (T[v].r != NIL) {
      if (y <= ty)
        find(T[v].r, sx, tx, sy, ty, depth + 1, ans);
    }
  }
}

int main() {
  int x, y;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &x, &y);
    P[i] = Point(i, x, y);
    T[i].l = T[i].r = T[i].p = NIL;
  }

  np = 0;

  int root = makeKDTree(0, N, 0);

  int q;
  scanf("%d", &q);
  int sx, tx, sy, ty;
  vector<Point> ans;
  for (int i = 0; i < q; i++) {
    scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
    ans.clear();
    find(root, sx, tx, sy, ty, 0, ans);
    sort(ans.begin(), ans.end());
    for (int j = 0; j < ans.size(); j++) {
      ans[j].print();
    }
    printf("\\n");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>

// #define scanf scanf_s
#define MAX 500000

struct Point {
  int x, y;
};

Point p[MAX];

int main() {
  int i, j, n, q, sx, sy, tx, ty, cnt;

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d %d", &p[i].x, &p[i].y);

  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    scanf("%d %d %d %d", &sx, &tx, &sy, &ty);
    for (j = 0; j < n; j++) {
      if (sx <= p[j].x && p[j].x <= tx && sy <= p[j].y && p[j].y <= ty)
        printf("%d\\n", j);
    }
    printf("\\n");
  }
  return 0;
}
Problem p02347 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
#define MAX_N 514514
tuple<int, int, int> A1[MAX_N], A2[MAX_N];
int N, Q, X1, Y1, X2, Y2;
vector<int> vec;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    scanf("%d%d", &X1, &Y1);
    A1[i] = make_tuple(X1, Y1, i);
    A2[i] = make_tuple(Y1, X1, i);
  }
  sort(A1, A1 + N);
  sort(A2, A2 + N);
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    scanf("%d%d%d%d", &X1, &X2, &Y1, &Y2);
    int pos1 = lower_bound(A1, A1 + N, make_tuple(X1, Y1, 0)) - A1;
    int pos2 = upper_bound(A1, A1 + N, make_tuple(X2, Y2, MAX_N)) - A1;
    int pos3 = lower_bound(A2, A2 + N, make_tuple(Y1, X1, 0)) - A2;
    int pos4 = upper_bound(A2, A2 + N, make_tuple(Y2, X2, MAX_N)) - A2;
    int size1 = pos2 - pos1;
    int size2 = pos4 - pos3;
    vec.clear();
    if (size1 <= size2) {
      for (int j = pos1; j < pos2; j++) {
        int Y = get<1>(A1[j]);
        if (Y1 <= Y && Y <= Y2) {
          vec.push_back(get<2>(A1[j]));
        }
      }
    } else {
      for (int j = pos3; j < pos4; j++) {
        int X = get<1>(A2[j]);
        if (X1 <= X && X <= X2) {
          vec.push_back(get<2>(A2[j]));
        }
      }
    }
    sort(vec.begin(), vec.end());
    for (int j = 0; j < vec.size(); j++) {
      printf("%d\\n", vec[j]);
    }
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <tuple>
#include <vector>
using namespace std;
#define MAX_N 514514
tuple<int, int, int> A1[MAX_N], A2[MAX_N];
int N, Q, X1, Y1, X2, Y2;
vector<int> vec;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> X1 >> Y1;
    A1[i] = make_tuple(X1, Y1, i);
    A2[i] = make_tuple(Y1, X1, i);
  }
  sort(A1, A1 + N);
  sort(A2, A2 + N);
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    cin >> X1 >> X2 >> Y1 >> Y2;
    int pos1 = lower_bound(A1, A1 + N, make_tuple(X1, Y1, 0)) - A1;
    int pos2 = upper_bound(A1, A1 + N, make_tuple(X2, Y2, MAX_N)) - A1;
    int pos3 = lower_bound(A2, A2 + N, make_tuple(Y1, X1, 0)) - A2;
    int pos4 = upper_bound(A2, A2 + N, make_tuple(Y2, X2, MAX_N)) - A2;
    int size1 = pos2 - pos1;
    int size2 = pos4 - pos3;
    vec.clear();
    if (size1 <= size2) {
      for (int j = pos1; j < pos2; j++) {
        int Y = get<1>(A1[j]);
        if (Y1 <= Y && Y <= Y2) {
          vec.push_back(get<2>(A1[j]));
        }
      }
    } else {
      for (int j = pos3; j < pos4; j++) {
        int X = get<1>(A2[j]);
        if (X1 <= X && X <= X2) {
          vec.push_back(get<2>(A2[j]));
        }
      }
    }
    sort(vec.begin(), vec.end());
    for (int j = 0; j < vec.size(); j++) {
      cout << vec[j] << endl;
    }
    cout << endl;
  }
  return 0;
}
Problem p02348 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
  int map[2000010];
  void build(int now, int l, int r) {
    if (l == r) {
      map[now] = INF;
      return;
    }
    map[now] = -1;
    int mid = (l + r) / 2;
    build(now * 2 + 1, l, mid);
    build(now * 2 + 2, mid + 1, r);
    return;
  }
  void change(int n, int now, int L, int R, int l, int r) {
    if (R < l || L > r)
      return;
    if (L >= l && R <= r) {
      map[now] = n;
      return;
    }
    if (map[now] != -1) {
      map[now * 2 + 1] = map[now * 2 + 2] = map[now];
      map[now] = -1;
    }
    int mid = (L + R) / 2;
    change(n, now * 2 + 1, L, mid, l, r);
    change(n, now * 2 + 2, mid + 1, R, l, r);
    return;
  }
  int find(int now, int L, int R, int n) {
    if (L == R)
      return map[now];
    if (map[now] != -1) {
      map[now * 2 + 1] = map[now * 2 + 2] = map[now];
      int temp = map[now];
      map[now] = -1;
      return temp;
    }
    int mid = (L + R) / 2;
    if (mid >= n)
      return find(now * 2 + 1, L, mid, n);
    else
      return find(now * 2 + 2, mid + 1, R, n);
  }
};
int main() {
  int n, m, k, l, r;
  segment_tree arr;
  scanf("%d%d", &n, &m);
  arr.build(0, 0, n - 1);
  while (m--) {
    scanf("%d", &k);
    if (k == 0) {
      scanf("%d%d%d", &l, &r, &k);
      arr.change(k, 0, 0, n - 1, l, r);
    } else {
      scanf("%d", &k);
      printf("%d\\n", arr.find(0, 0, n - 1, k));
    }
  }
}

Error: 0

Fixed submission:

#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
  int map[200010];
  void build(int now, int l, int r) {
    if (l == r) {
      map[now] = INF;
      return;
    }
    map[now] = -1;
    int mid = (l + r) / 2;
    build(now * 2 + 1, l, mid);
    build(now * 2 + 2, mid + 1, r);
    return;
  }
  void change(int n, int now, int L, int R, int l, int r) {
    if (R < l || L > r)
      return;
    if (L >= l && R <= r) {
      map[now] = n;
      return;
    }
    if (map[now] != -1) {
      map[now * 2 + 1] = map[now * 2 + 2] = map[now];
      map[now] = -1;
    }
    int mid = (L + R) / 2;
    change(n, now * 2 + 1, L, mid, l, r);
    change(n, now * 2 + 2, mid + 1, R, l, r);
    return;
  }
  int find(int now, int L, int R, int n) {
    if (L == R)
      return map[now];
    if (map[now] != -1) {
      map[now * 2 + 1] = map[now * 2 + 2] = map[now];
      int temp = map[now];
      map[now] = -1;
      return temp;
    }
    int mid = (L + R) / 2;
    if (mid >= n)
      return find(now * 2 + 1, L, mid, n);
    else
      return find(now * 2 + 2, mid + 1, R, n);
  }
};
int main() {
  int n, m, k, l, r;
  segment_tree arr;
  scanf("%d%d", &n, &m);
  arr.build(0, 0, n - 1);
  while (m--) {
    scanf("%d", &k);
    if (k == 0) {
      scanf("%d%d%d", &l, &r, &k);
      arr.change(k, 0, 0, n - 1, l, r);
    } else {
      scanf("%d", &k);
      printf("%d\\n", arr.find(0, 0, n - 1, k));
    }
  }
}
Problem p02348 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

#define INF 2147483647
#define ASTART (1 << 17)
int a[1 << 18][2];
int ti = 1;

int find(int i) {
  int nv = INF, t = 0;
  i += ASTART - 1;
  nv = a[i][0];
  t = a[i][1];
  while (i > 0) {
    i = (i - 1) / 2;
    if (t < a[i][1])
      nv = a[i][0], t = a[i][1];
  }
  return nv;
}
void update(int s, int t, int x, int k, int l, int r) {
  if (l >= s && r <= t) {
    a[k][0] = x;
    a[k][1] = ti;
    return;
  }
  if (r <= s || l >= t)
    return;
  int m = (l + r) / 2;
  update(s, t, x, k * 2 + 1, l, m);
  update(s, t, x, k * 2 + 2, m, r);
}
int main() {
  int n, q, com, s, t, x;
  for (int i = 0; i < (1 << 18); i++)
    a[i][0] = INF, a[i][1] = 0;
  cin >> n >> q;

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

  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;

#define INF 2147483647
#define ASTART (1 << 17)
int a[1 << 18][2];
int ti = 1;

int find(int i) {
  int nv = INF, t = 0;
  i += ASTART - 1;
  nv = a[i][0];
  t = a[i][1];
  while (i > 0) {
    i = (i - 1) / 2;
    if (t < a[i][1])
      nv = a[i][0], t = a[i][1];
    // cout << i << " = " << a[i][0]<< " = " << a[i][1]<< endl;
  }
  return nv;
}
void update(int s, int t, int x, int k, int l, int r) {
  if (l >= s && r < t) {
    a[k][0] = x;
    a[k][1] = ti;
    return;
  }
  if (r <= s || l >= t)
    return;
  int m = (l + r) / 2;
  update(s, t, x, k * 2 + 1, l, m);
  update(s, t, x, k * 2 + 2, m, r);
}
int main() {
  int n, q, com, s, t, x;
  for (int i = 0; i < (1 << 18); i++)
    a[i][0] = INF, a[i][1] = 0;
  cin >> n >> q;

  for (int i = 0; i < q; i++) {
    cin >> com;
    if (com == 0) {
      cin >> s >> t >> x;
      update(s, t + 1, x, 0, 0, ASTART);
      ti++;
    } else {
      cin >> s;
      cout << find(s) << endl;
    }
    // for (int i=0;i<5;i++)cout << a[i] << endl;
    // cout << a[ASTART];
  }

  return 0;
}
Problem p02348 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

int find(int x, int n, vector<int> &node) {
  x += n - 1;
  int ret = node[x];
  while (x > 0) {
    x = (x - 1) / 2;
    ret = max(ret, node[x]);
  }
  return ret;
}

void update(int a, int b, int i, int k, int l, int r, vector<int> &node) {
  if (r <= a || b <= l)
    return;
  if (a <= l && r <= b) {
    node[k] = i;
    return;
  } else {
    update(a, b, i, 2 * k + 1, l, (l + r) / 2, node);
    update(a, b, i, 2 * k + 2, (l + r) / 2, r, node);
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n_, q;
  cin >> n_ >> q;
  int n = 1;
  while (n < n_)
    n *= 2;
  vector<int> node(2 * n - 1, 0);
  vector<int> ans(q + 1, INT_MAX);

  for (int i = 0; i < q; i++) {
    int com;
    cin >> com;
    if (com == 0) {
      int s, t, x;
      cin >> s >> t >> x;
      ans[i + 1] = x;
      update(s, t + 1, i + 1, 0, 0, n, node);
    } else {
      int x;
      cin >> x;
      cout << ans[find(x, n, node)] << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

void update(int s, int t, int x, vector<int> &a) {
  fill(a.begin() + s, a.begin() + t + 1, x);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n_, q;
  cin >> n_ >> q;
  vector<int> a(n_, INT_MAX);

  for (int i = 0; i < q; i++) {
    int com;
    cin >> com;
    if (com == 0) {
      int s, t, x;
      cin >> s >> t >> x;
      update(s, t, x, a);
    } else {
      int x;
      cin >> x;
      cout << a[x] << endl;
    }
  }
}