Guest User

Untitled

a guest
Feb 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. #include <fstream>
  2. using namespace std;
  3. #define MAX_SIZE 400000
  4. class rect
  5. {
  6. public:
  7. int llx,lly,urx,ury;
  8. int color;
  9. };
  10.  
  11. rect input[1001];
  12. int nrect;
  13. rect queue[MAX_SIZE];
  14. int q_begin = 0;
  15. int q_end = 0;
  16.  
  17. void cut( rect , rect );
  18. int res[1001];
  19. int main()
  20. {
  21. ifstream fin("rect1.in");
  22. ofstream fout("rect1.out");
  23. int A,B,N;
  24. fin>>A>>B>>N;
  25. input[0].llx=0;
  26. input[0].lly=0;
  27. input[0].urx=A;
  28. input[0].ury=B;
  29. input[0].color = 1;
  30.  
  31. for (int i = 1; i<=N ;i++)
  32. {
  33. fin>>input[i].llx>>input[i].lly
  34. >>input[i].urx>>input[i].ury
  35. >>input[i].color;
  36. }
  37. nrect = N+1;
  38. //rect cut;
  39. queue[0] = input[0];
  40.  
  41. for (int i = 1; i<=N ; i++)
  42. {
  43. /* add rect[i] */
  44. int end = q_end%MAX_SIZE;
  45. for ( int j = q_begin ; j <= end ;j++)
  46. {
  47. cut(input[i],queue[j]);
  48. }
  49.  
  50. q_end = (++q_end)%MAX_SIZE;
  51. queue[q_end] = input[i];
  52. q_begin = (end+1)%MAX_SIZE;
  53.  
  54. }
  55.  
  56. //output
  57. if (q_begin>q_end)
  58. {
  59. q_end += MAX_SIZE;
  60. }
  61. for (int j = q_begin ; j<= q_end ;j++)
  62. {
  63. res[queue[j%MAX_SIZE].color] += (queue[j%MAX_SIZE].urx-queue[j%MAX_SIZE].llx)*(queue[j%MAX_SIZE].ury-queue[j%MAX_SIZE].lly);
  64. }
  65. for (int i = 1 ; i<1001 ;i++)
  66. {
  67. if (res[i])
  68. {
  69. fout<<i<<' '<<res[i]<<endl;
  70. }
  71. }
  72.  
  73.  
  74. fin.close();
  75. fout.close();
  76. return 0;
  77. }
  78.  
  79. void cut(rect i,rect j)
  80. {
  81. //i didn't overlap j
  82. if (i.llx>=j.urx||i.urx<=j.llx||i.lly>=j.ury||i.ury<=j.lly)
  83. {
  84. q_end = (++q_end)%MAX_SIZE;
  85. queue[q_end] = j;
  86. return;
  87. }
  88.  
  89.  
  90. int k1,k2,k3,k4;
  91. k1 = max( i.llx, j.llx );
  92. k2 = min( i.urx, j.urx );
  93. k3 = max( i.lly, j.lly );
  94. k4 = min( i.ury, j.ury );
  95.  
  96. if (j.llx < k1)
  97. {
  98. q_end = (++q_end)%MAX_SIZE;
  99. queue[q_end].llx = j.llx;
  100. queue[q_end].lly = j.lly;
  101. queue[q_end].urx = k1;
  102. queue[q_end].ury = j.ury;
  103. queue[q_end].color = j.color;
  104. }
  105. if (j.urx>k2)
  106. {
  107. q_end = (++q_end)%MAX_SIZE;
  108. queue[q_end].llx = k2;
  109. queue[q_end].lly = j.lly;
  110. queue[q_end].urx = j.urx;
  111. queue[q_end].ury = j.ury;
  112. queue[q_end].color = j.color;
  113. }
  114. if (j.lly<k3)
  115. {
  116. q_end = (++q_end)%MAX_SIZE;
  117. queue[q_end].llx = k1;
  118. queue[q_end].lly = j.lly;
  119. queue[q_end].urx = k2;
  120. queue[q_end].ury = k3;
  121. queue[q_end].color = j.color;
  122. }
  123. if (j.ury>k4)
  124. {
  125. q_end = (++q_end)%MAX_SIZE;
  126. queue[q_end].llx = k1;
  127. queue[q_end].lly = k4;
  128. queue[q_end].urx = k2;
  129. queue[q_end].ury = j.ury;
  130. queue[q_end].color = j.color;
  131. }
  132. }
Add Comment
Please, Sign In to add comment