fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int N, M;
  6. long long Sx, Sy;
  7. cin >> N >> M >> Sx >> Sy;
  8.  
  9. map<long long, set<long long>> row_houses, col_houses;
  10.  
  11. for (int i = 0; i < N; i++) {
  12. long long x, y;
  13. cin >> x >> y;
  14. row_houses[y].insert(x);
  15. col_houses[x].insert(y);
  16. }
  17.  
  18. vector<pair<char, long long>> moves(M);
  19. for (int i = 0; i < M; i++) {
  20. cin >> moves[i].first >> moves[i].second;
  21. }
  22.  
  23. set<pair<long long, long long>> visitedHouses;
  24.  
  25. long long x = Sx, y = Sy;
  26.  
  27. for (auto move : moves) {
  28. char dir = move.first;
  29. long long dist = move.second;
  30.  
  31. if (dir == 'U') {
  32. auto it = col_houses[x].upper_bound(y);
  33. while (it != col_houses[x].end() && *it <= y + dist) {
  34. visitedHouses.insert({x, *it});
  35. it++;
  36. }
  37. y += dist;
  38. } else if (dir == 'D') {
  39. auto it = col_houses[x].lower_bound(y);
  40. while (it != col_houses[x].begin() && *prev(it) >= y - dist) {
  41. --it;
  42. visitedHouses.insert({x, *it});
  43. }
  44. y -= dist;
  45. } else if (dir == 'L') {
  46. auto it = row_houses[y].lower_bound(x);
  47. while (it != row_houses[y].begin() && *prev(it) >= x - dist) {
  48. it--;
  49. visitedHouses.insert({*it, y});
  50. }
  51. x -= dist;
  52. } else if (dir == 'R') {
  53. auto it = row_houses[y].upper_bound(x);
  54. while (it != row_houses[y].end() && *it <= x + dist) {
  55. visitedHouses.insert({*it, y});
  56. it++;
  57. }
  58. x += dist;
  59. }
  60. }
  61.  
  62. cout << x << " " << y << " " << visitedHouses.size() << endl;
  63. return 0;
  64. }
  65.  
Success #stdin #stdout 0s 5284KB
stdin
3 4 3 2
2 2
3 3
2 1
L 2
D 1
R 1
U 2
stdout
2 3 2