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