Korotkodul

ЗОШ объедини прямоуг-ки OK

Jan 8th, 2022 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <queue>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <set>
  9. #include <map>
  10. #define pii pair <int,int>
  11. using namespace std;
  12. using ll = long long;
  13. using ld = long double;
  14. void cv(vector <int> &v){
  15. for (auto x: v) cout<<x<<' ';
  16. cout<<"\n";
  17. }
  18.  
  19. void cvl(vector <ll> &v){
  20. for (auto x: v) cout<<x<<' ';
  21. cout<<"\n";
  22. }
  23.  
  24.  
  25. void cvv(vector <vector <int> > &v){
  26. for (auto x: v) cv(x);
  27. cout<<"\n";
  28. }
  29.  
  30. struct hpny{
  31. int y, tp;
  32. };
  33.  
  34. struct hpnx{
  35. int x,tp; //0-открыл, 1 -закрыл
  36. hpny y1, y2;
  37. };
  38.  
  39. void shx(hpnx sm){
  40. cout<<"x = "<<sm.x<<"\n";
  41. cout<<"type = "<<sm.tp<<"\n";
  42. cout<<"\n";
  43. }
  44.  
  45. void shy(hpny sm){
  46. cout<<"y = "<<sm.y<<"\n";
  47. cout<<"type = "<<sm.tp<<"\n";
  48. cout<<"\n";
  49. }
  50.  
  51. vector <hpnx> allx;
  52.  
  53. vector <hpny> ally;//now
  54.  
  55.  
  56. bool cmpx(hpnx a, hpnx b){
  57. return a.x < b.x;
  58. }
  59.  
  60. bool cmpy(hpny a, hpny b){
  61. return a.y < b.y;
  62. }
  63.  
  64. ll ans=0;
  65. int x1, x2;
  66. int idx = 1;
  67. //scan y
  68. ll scy(){ //x join
  69. ll join = 0;
  70. int bly = 1;
  71. for (int i = 1; i < ally.size();++i){
  72.  
  73. if (bly > 0){
  74. join += ally[i].y - ally[i-1].y;
  75. }
  76. if (ally[i].tp == 0){
  77. bly++;
  78. }
  79. else{
  80. bly--;
  81. }
  82.  
  83. }
  84.  
  85. ans += (x2 - x1) * join;
  86. }
  87.  
  88. int main()
  89. {
  90. ios::sync_with_stdio(0);
  91. cin.tie(0);
  92. cout.tie(0);
  93. int n; cin>>n;
  94. for (int i=0;i<n;++i){
  95. int x1, y1, x2, y2;
  96. cin>>x1>>y1>>x2>>y2;
  97. hpnx xl, xr;
  98. hpny yl, yr;
  99. yl = {y1, 0};
  100. yr = {y2, 1};
  101. xl = {x1, 0, yl, yr};
  102. xr = {x2, 1, yl, yr};
  103. allx.push_back(xl);
  104. allx.push_back(xr);
  105. }
  106. sort(allx.begin(), allx.end(), cmpx);
  107. int blx = 1;
  108.  
  109. x1 = allx[0].x;
  110. ally.push_back(allx[0].y1);
  111. ally.push_back(allx[0].y2);
  112. while (idx < n * 2) {
  113. x2 = allx[idx].x;
  114. scy();
  115. if (allx[idx].tp == 1){
  116. blx--;
  117. int id1, id2;
  118. for (int j = 0; j < ally.size();++j){
  119. if (ally[j].y == allx[idx].y1.y && ally[j].tp == 0){
  120. id1 = j;
  121. }
  122. else if (ally[j].y == allx[idx].y2.y && ally[j].tp == 1){
  123. id2 = j;
  124. }
  125. }
  126. ally.erase(ally.begin() + id1);
  127. id2--;
  128. ally.erase(ally.begin() + id2);
  129. }
  130.  
  131.  
  132.  
  133.  
  134. if (allx[idx].tp == 0){
  135. blx++;
  136. ally.push_back(allx[idx].y1);
  137. ally.push_back(allx[idx].y2);
  138. sort(ally.begin(), ally.end(), cmpy);
  139. }
  140.  
  141. x1 = x2;
  142.  
  143. idx++;
  144. }
  145. //cout<<"ans = "<<ans<<"\n";
  146. cout<<ans<<"\n";
  147. }
  148. /*
  149. 3
  150. 1 1 3 5
  151. 5 2 7 4
  152. 2 4 6 7
  153.  
  154. 4
  155. 1 1 3 5
  156. 5 2 7 4
  157. 2 4 6 7
  158. 10 0 20 10
  159.  
  160. 5
  161. 1 1 3 5
  162. 5 2 7 4
  163. 2 4 6 7
  164. 10 0 20 10
  165. 0 9 15 10
  166. */
  167.  
Add Comment
Please, Sign In to add comment