Advertisement
a53

JocCuLasere

a53
May 6th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. ifstream in("lasere.in");
  5. ofstream out("lasere.out");
  6.  
  7. #define N (int)1e6
  8. #define MAX (int)1e9
  9.  
  10. map<int, int> nivel;
  11. vector<tuple<int, int, int>>ordine;
  12. struct segment {
  13. int x1, y1, x2, y2;
  14. }V[N];
  15.  
  16. int AI[4 * N];
  17.  
  18.  
  19. int query(int a, int b, int n) {
  20. a += n; b += n;
  21. int s = 0;
  22. while (a <= b) {
  23. if (a % 2 == 1) s += AI[a++];
  24. if (b % 2 == 0) s += AI[b--];
  25. a /= 2; b /= 2;
  26. }
  27. return s;
  28. }
  29.  
  30. void update(int k, int x, int n) {
  31. k += n;
  32. AI[k] += x;
  33. for (k /= 2; k >= 1; k /= 2) {
  34. AI[k] = AI[2 * k] + AI[2 * k + 1];
  35. }
  36. }
  37.  
  38. int main()
  39. {
  40. int n;
  41. int tip, a, b;
  42. int x1, x2, y1, y2;
  43. in >> n;
  44.  
  45. for (int i = 0; i < n; i++) {
  46. in >> tip >> x1 >> a >> b;
  47. if (tip == 1)
  48. {
  49. x2 = x1 + a;
  50. y1 = y2 = b;
  51. nivel[y1] = 0;
  52. ordine.push_back(make_tuple( x1, 0, i ));
  53. ordine.push_back(make_tuple( x2, 2, i ));
  54. }
  55. else
  56. {
  57. x2 = x1;
  58. y1 = a;
  59. y2 = b;
  60. nivel[y1] = 0;
  61. nivel[y2] = 0;
  62. ordine.push_back(make_tuple( x1, 1, i ));
  63. }
  64.  
  65. V[i].x1 = x1; V[i].y1 = y1;
  66. V[i].x2 = x2; V[i].y2 = y2;
  67.  
  68. }
  69.  
  70. int i = 0;
  71. for (map<int, int>::iterator it = nivel.begin(); it != nivel.end(); it++)
  72. it->second = i++ + 1;
  73.  
  74. sort(ordine.begin(), ordine.end());
  75.  
  76. long long suma = 0;
  77. int s = 0;
  78. int Maxim = nivel.size();
  79. for (auto t : ordine) //baleierea
  80. switch (get<1>(t))
  81. {
  82. case 0: //actualizare - intrare
  83. update(nivel[V[get<2>(t)].y1], 1, Maxim);
  84. break;
  85. case 1: //interogare
  86. s = query(nivel[V[get<2>(t)].y1], nivel[V[get<2>(t)].y2], Maxim);
  87. out << s << endl;
  88. suma += s;
  89. break;
  90. case 2: //actualizare - iesire
  91. update(nivel[V[get<2>(t)].y1], -1, Maxim);
  92. break;
  93. }
  94. out << suma;
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement