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

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
// I LOVE IT :D
// #define int long long
using namespace std;
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline string getStr() {
  char ch[1000000];
  scanf("%s", ch);
  return ch;
}
const int MAX_N = 500005;
const int MAX_LG = 22;

pair<int, int> x[MAX_N];
int y[MAX_N];
int st[MAX_LG][MAX_N];
int ptr[4 * MAX_N];
void build(int d, int b, int e) {
  if (b == e) {
    st[d][b] = x[b].second;
    return;
  }
  int m = (b + e) >> 1;
  build(d + 1, b, m);
  build(d + 1, m + 1, e);
  int l = b, r = m + 1;
  for (int pt = b; pt <= e; ++pt) // p = point ha
    if (l != m + 1 && (r == e + 1 || y[st[d + 1][l]] < y[st[d + 1][r]]))
      st[d][pt] = st[d + 1][l++];
    else
      st[d][pt] = st[d + 1][r++];
}
int j;
int result[105];
int L = 0;
inline void query(int dep, int l, int r, int x1, int y1, int x2, int y2,
                  int p = 0) {
  if (x[r].first < x1 || x[l].first > x2)
    return;
  if (y[st[dep][l]] > y2 || y[st[dep][r]] < y1)
    return;
  if (x[l].first >= x1 && x[r].first <= x2) {

    if (ptr[p] < l)
      ptr[p] = l;
    while (ptr[p] <= r && (y[st[dep][ptr[p]]] < y1))
      ptr[p]++;
    for (int j = ptr[p]; j <= r; j++) {
      if (!(y[st[dep][j]] >= y1 && y[st[dep][j]] <= y2))
        break;
      L++;
      result[L] = st[dep][j];
    }
    return;
  }
  int mid = (l + r) >> 1;
  query(dep + 1, l, mid, x1, y1, x2, y2, 2 * p + 1);
  query(dep + 1, mid + 1, r, x1, y1, x2, y2, 2 * p + 2);
  return;
}
const int MAX_Q = 20000;
pair<pair<pair<int, int>, pair<int, int>>, int> *quer =
    new pair<pair<pair<int, int>, pair<int, int>>, int>[MAX_Q];
vector<vector<int>> res(MAX_Q);
int32_t main() {
  int n = in();
  for (int i = 0; i < n; i++) {
    x[i] = {in(), i};
    y[i] = in();
  }
  sort(x, x + n);
  build(0, 0, n - 1);
  int q = in();
  int x1, x2, y1, y2;

  for (int i = 0; i < q; i++) {
    x1 = in(), x2 = in(), y1 = in(), y2 = in();
    quer[i] = {{{y1, y2}, {x1, x2}}, i};
  }
  sort(quer, quer + q);
  for (int j = 0; j < q; j++) {
    int ind = quer[j].second;
    pair<int, int> xc = quer[j].first.second;
    pair<int, int> yc = quer[j].first.first;
    L = -1;
    query(0, 0, n - 1, xc.first, yc.first, xc.second, yc.second);
    for (int j = 0; j <= L; j++) {
      res[ind].push_back(result[j]);
    }
    sort(res[ind].begin(), res[ind].end());
  }
  for (int j = 0; j < q; j++) {
    for (int i = 0; i < res[j].size(); i++)
      printf("%d\\n", res[j][i]);
    printf("\\n");
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
// I LOVE IT :D
// #define int long long
using namespace std;
inline int in() {
  int32_t x;
  scanf("%d", &x);
  return x;
}
inline string getStr() {
  char ch[1000000];
  scanf("%s", ch);
  return ch;
}
const int MAX_N = 500005;
const int MAX_LG = 22;
pair<int, int> x[MAX_N];
int y[MAX_N];
int st[MAX_LG][MAX_N];
bool cmp(int &a, int &b) { return y[a] < y[b]; }
/*
 void build(int dep , int l , int r)
 {
 if (l == r)
 {
 st[dep][l] = x[l].second;
 return ;
 }
 int mid = (l + r) >> 1;
 build(dep + 1 , l , mid);
 build(dep + 1 , mid + 1 , r);
 merge(st[dep + 1] + l , st[dep + 1] + mid + 1 , st[dep + 1] + mid + 1 , st[dep
 + 1] + r + 1 , st[dep] + l , cmp);
 }
 */
void build(int d, int b, int e) {
  if (b == e) {
    st[d][b] = x[b].second;
    return;
  }
  int m = (b + e) >> 1;
  build(d + 1, b, m);
  build(d + 1, m + 1, e);
  int l = b, r = m + 1;
  for (int pt = b; pt <= e; ++pt) // p = point ha
    if (l != m + 1 && (r == e + 1 || y[st[d + 1][l]] < y[st[d + 1][r]]))
      st[d][pt] = st[d + 1][l++];
    else
      st[d][pt] = st[d + 1][r++];
}
int j;
int result[105];
int L = 0;
inline void query(int dep, int l, int r, int x1, int y1, int x2, int y2) {
  if (x[r].first < x1 || x[l].first > x2)
    return;
  if (y[st[dep][l]] > y2 || y[st[dep][r]] < y1)
    return;
  if (x[l].first >= x1 && x[r].first <= x2) {
    if (y[st[dep][l]] >= y1 && y[st[dep][r]] <= y2) {
      for (j = l; j <= r; j++) {
        L++;
        result[L] = st[dep][j];
      }
      return;
    }
  }
  int mid = (l + r) >> 1;
  query(dep + 1, l, mid, x1, y1, x2, y2);
  query(dep + 1, mid + 1, r, x1, y1, x2, y2);
  return;
}
int32_t main() {
  int n = in();
  for (int i = 0; i < n; i++) {
    x[i] = {in(), i};
    y[i] = in();
  }
  sort(x, x + n);
  build(0, 0, n - 1);
  int q = in();
  int x1, x2, y1, y2;
  while (q--) {
    x1 = in(), x2 = in(), y1 = in(), y2 = in();
    L = -1;
    query(0, 0, n - 1, x1, y1, x2, y2);
    sort(result, result + L + 1);
    for (j = 0; j <= L; j++)
      printf("%d\\n", result[j]);
    printf("\\n");
  }
}