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

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

typedef pair<int, int> pii;

long long seg[262149], n, v, a, b, c, d, INF = (1LL << 31) - 1, size_ = 1;
void update(long long p, long long q, long long r, long long s, long long t,
            long long u, long long v) {
  if (s <= p || q <= r) {
    if (v != INF)
      seg[u] = v;
    return;
  }
  if (p <= r && s <= q) {
    seg[u] = t;
    return;
  }
  long long w = v;
  if (w == INF)
    w = seg[u];
  seg[u] = INF;
  update(p, q, r, (r + s) / 2, t, u * 2, w);
  update(p, q, (r + s) / 2, s, t, u * 2 + 1, w);
}
long long find(long long p, long long q, long long r, long long s,
               long long u) {
  if (s <= p || q <= r)
    return INF;
  if ((r <= p && q <= s) && (seg[u] != INF || s - r == 1)) {
    return seg[u];
  }
  long long a1 = find(p, q, r, (r + s) / 2, u * 2);
  long long a2 = find(p, q, (r + s) / 2, s, u * 2 + 1);
  return min(a1, a2);
}
int main() {
  cin >> n >> v;
  while (size_ < n)
    size_ *= 2;
  for (int i = 1; i <= size_ * 2; i++)
    seg[i] = INF;
  for (int i = 1; i <= v; i++) {
    scanf("%d", &a);
    if (a == 0) {
      scanf("%lld%lld%lld", &b, &c, &d);
      c++;
      update(b, c, 0, size_, d, 1, INF);
    }
    if (a == 1) {
      scanf("%lld", &b);
      printf("%lld\\n", find(b, b + 1, 0, size_, 1));
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define _USE_MATH_DEFINES

#include <map>
#include <math.h>

#define SENTINEL 1000000001

#define min(a, b) (a) > (b) ? (b) : (a)
#define max(a, b) (a) > (b) ? (a) : (b)

using namespace std;

typedef pair<int, int> pii;

struct OwnNode {
  int num;
  int l, r;
  OwnNode *left;
  OwnNode *right;

  OwnNode(int l, int r)
      : l(l), r(r), num(2147483647), left(nullptr), right(nullptr) {}

  void Build() {
    if (l + 1 == r) {
      return;
    }
    int m = (l + r) / 2;
    left = new OwnNode(l, m);
    right = new OwnNode(m, r);
    left->Build();
    right->Build();
  }

  void update(int s, int t, int v) {
    if (t <= l || r <= s) {
      return;
    }
    if (s <= l && r <= t) {
      num = v;
      return;
    }
    int m = (l + r) / 2;
    if (right != nullptr) {
      if (num >= 0) {
        right->num = num;
      }
      if (m < t) {
        right->update(s, t, v);
      }
    }
    if (left != nullptr) {
      if (num >= 0) {
        left->num = num;
      }
      if (s < m) {
        left->update(s, t, v);
      }
    }
    num = -1;
  }

  int find(int s, int t) {
    if (t <= l || r <= s) {
      return 2147483647;
    }
    if (num >= 0) {
      return num;
    }
    int mini = 2147483647;
    int m = (l + r) / 2;
    if (right != nullptr) {
      if (m < t) {
        mini = min(mini, right->find(s, t));
      }
    }
    if (left != nullptr) {
      if (s < m) {
        mini = min(mini, left->find(s, t));
      }
    }
    return mini;
  }
};

using namespace std;

OwnNode *root;

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

  root = new OwnNode(0, n);
  root->Build();

  for (int i = 0; i < q; i++) {
    int c, s, t, v;
    scanf("%d", &c);

    switch (c) {
    case 0:
      scanf("%d %d %d", &s, &t, &v);
      root->update(s, t + 1, v);
      break;
    case 1:
      scanf("%d", &s);
      cout << root->find(s, s + 1) << endl;
      break;
    }
  }

  return 0;
}