fork download
  1. #include<stdio.h>
  2. #include<set>
  3. #include<deque>
  4. using namespace std;
  5. struct xy {
  6. int x, y, z;
  7. bool operator<(const xy p)const {
  8. if (x != p.x)return x > p.x;
  9. if (y != p.y)return y < p.y;
  10. return z < p.z;
  11. }
  12. };
  13. set<xy>S[2];
  14. int D[2][121212];
  15. int main() {
  16. int i, j;
  17. int n, d;
  18. deque<int>Qc, Qw, Qq;
  19. scanf("%d%d", &n, &d);
  20. for (i = 0; i < 2; i++) {
  21. int x[2];
  22. for (j = 0; j < n; j++) {
  23. scanf("%d%d", &x[i], &x[!i]);
  24. if (x[1] == 0) {
  25. D[i][j] = 2;
  26. Qc.push_back(i);
  27. Qw.push_back(j);
  28. Qq.push_back(x[0]);
  29. }
  30. else S[i].insert({ x[1],x[0],j });
  31. }
  32. }
  33. while (!Qc.empty()) {
  34. int nc = Qc.front(); Qc.pop_front();
  35. int nw = Qw.front(); Qw.pop_front();
  36. int nq = Qq.front(); Qq.pop_front();
  37. set<xy>::iterator it = S[!nc].lower_bound({ nq,(int)-1e9,(int)-1e9 });
  38. while (it != S[!nc].end()) {
  39. if (it->x < nq - d)break;
  40. Qc.push_back(!nc);
  41. Qw.push_back(it->z);
  42. Qq.push_back(it->y);
  43. D[!nc][it->z] = D[nc][nw] + 1;
  44. S[!nc].erase(it++);
  45. }
  46. }
  47. for (i = 0; i < n; i++) printf("%d\n", D[0][i] - 1);
  48. return 0;
  49. }
Success #stdin #stdout 0s 4296KB
stdin
2 1
1 1
5 0
4 2
1 4
stdout
3
1