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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define INT(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define INPUT(x) scanf("%d", &x)
#define REP1(x, n) for (int x = 0; x < n; x++)
#define REP2(x, s, e) for (int x = s; x <= e; x++)
#define BR printf("\\n")
#define INF 2000000000
#define NIL -1

int A[500000][3]; // x,y,id

void sort_ck(int l, int r, int c) {
  priority_queue<pair<int, pair<int, int>>> PQ;
  int c_ = (c + 1) % 2;
  REP1(i, r - l)
  PQ.push(make_pair(-A[l + i][c], make_pair(A[l + i][c_], A[l + i][2])));
  REP1(i, r - l) {
    A[i + l][c] = -PQ.top().first;
    A[i + l][c_] = PQ.top().second.first;
    A[i + l][2] = PQ.top().second.second;
    PQ.pop();
  }
}

struct Node {
  int id, l, r, p, x, y;
};

Node T[500000];
int np = 0;

int make2Dtree(int l, int r, int d) {
  if (r - l <= 0)
    return NIL;
  int mid = (l + r) / 2;
  sort_ck(l, r, d);
  int t = np++;
  T[t].id = A[mid][2];
  T[t].x = A[mid][0];
  T[t].y = A[mid][1];
  int d_ = (d + 1) % 2;
  T[t].l = make2Dtree(l, mid, d_);
  T[t].r = make2Dtree(mid + 1, r, d_);

  return t;
}

vector<int> ans;

void findTree(int i, int xs, int xt, int ys, int yt, int d) {
  if (d == 0) {
    if (xs <= T[i].x && T[i].l != NIL)
      findTree(T[i].l, xs, xt, ys, yt, 1);
    if (xs <= T[i].x && T[i].x <= xt && ys <= T[i].y && T[i].y <= yt)
      ans.push_back(T[i].id);
    if (T[i].x <= xt && T[i].r != NIL)
      findTree(T[i].r, xs, xt, ys, yt, 1);
  } else {
    if (ys <= T[i].y && T[i].l != NIL)
      findTree(T[i].l, xs, xt, ys, yt, 0);
    if (xs <= T[i].x && T[i].x <= xt && ys <= T[i].y && T[i].y <= yt)
      ans.push_back(T[i].id);
    if (T[i].y <= yt && T[i].r != NIL)
      findTree(T[i].r, xs, xt, ys, yt, 0);
  }
}

void in_order(Node T[], int i) {
  if (T[i].l != NIL)
    in_order(T, T[i].l);
  printf("%d\\n", T[i].id);
  if (T[i].r != NIL)
    in_order(T, T[i].r);
}

int main() {
  INT(n);
  REP1(i, n) {
    INT(x);
    INT(y);
    A[i][0] = x;
    A[i][1] = y;
    A[i][2] = i;
  }
  make2Dtree(0, n, 0);
  INT(q);
  REP1(i, q) {
    INT(xs);
    INT(xt);
    INT(ys);
    INT(yt);
    findTree(0, xs, xt, ys, yt, 0);
    sort(ans.begin(), ans.end());
    REP1(j, ans.size()) printf("%d\\n", ans[j]);
    BR;
    ans.clear();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define INT(x)                                                                 \\
  int x;                                                                       \\
  scanf("%d", &x)
#define INPUT(x) scanf("%d", &x)
#define REP1(x, n) for (int x = 0; x < n; x++)
#define REP2(x, s, e) for (int x = s; x <= e; x++)
#define BR printf("\\n")
#define INF 2000000000

int upper(vector<pair<int, int>> list, int l, int r, int t) {
  int med = (l + r) / 2;
  if (r - l == 1) {
    return l;
  }
  if (list[med].first <= t) {
    return upper(list, med, r, t);
  } else {
    return upper(list, l, med, t);
  }
}

int downer(vector<pair<int, int>> list, int l, int r, int s) {
  int med = (l + r) / 2;
  if (r - l == 1) {
    if (list[l].first >= s) {
      return l;
    } else {
      return r;
    }
  }
  if (list[med].first >= s) {
    return downer(list, l, med, s);
  } else {
    return downer(list, med, r, s);
  }
}

void rec(int n, vector<pair<int, int>> listx, vector<pair<int, int>> listy,
         int xs, int xt, int ys, int yt) {
  int xis = downer(listx, 0, n, xs);
  int xit = upper(listx, 0, n, xt);
  int yis = downer(listy, 0, n, ys);
  int yit = upper(listy, 0, n, yt);

  priority_queue<int> ans;
  bool tbit[100000] = {};
  REP2(i, xis, xit) tbit[listx[i].second] = true;
  REP2(i, yis, yit) {
    if (tbit[listy[i].second]) {
      ans.push((-1) * (listy[i].second));
    }
  }
  while (!ans.empty()) {
    printf("%d\\n", (-1) * (ans.top()));
    ans.pop();
  }
  BR;
}

int main() {
  // value,id
  vector<pair<int, int>> up_x, up_y;

  INT(n);
  REP1(i, n) {
    INT(x);
    INT(y);
    up_x.push_back(make_pair(x, i));
    up_y.push_back(make_pair(y, i));
  }
  sort(up_x.begin(), up_x.end());
  sort(up_y.begin(), up_y.end());
  INT(q);
  REP1(i, q) {
    INT(xs);
    INT(xt);
    INT(ys);
    INT(yt);
    rec(n, up_x, up_y, xs, xt, ys, yt);
  }

  return 0;
}