Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAX 131072
  3.  
  4. using namespace std;
  5. const int NMAX = 30010;
  6.  
  7. FILE *IN;
  8.  
  9. int ans;
  10. int N, M, F, Q;
  11. int DX, DY;
  12.  
  13. struct per{
  14. int x, top, bottom;
  15. int index, cost;
  16. }query[2 * NMAX];
  17.  
  18. struct tree{
  19. int sum, inc, dif;
  20. }T[6 * NMAX];
  21.  
  22. int pos, sign;
  23. char f[MAX];
  24.  
  25. inline void Read(int &nr){
  26. sign = 0;
  27. nr = 0;
  28. while(f[pos] < '0' || f[pos] > '9'){
  29. if(f[pos] == '-') sign = 1;
  30. pos++;
  31. if(pos == MAX)
  32. fread(f, MAX, 1, IN), pos = 0;
  33. }
  34. while(f[pos] >= '0' && f[pos] <= '9'){
  35. nr = 10 * nr + f[pos++] - '0';
  36. if(pos == MAX)
  37. fread(f, MAX, 1, IN), pos = 0;
  38. }
  39. if(sign) nr =- nr;
  40. }
  41.  
  42. bool cmp(per a, per b){
  43. return a.x < b.x;
  44. }
  45.  
  46. inline void read(){
  47. Read(N); Read(M); Read(F);
  48. Read(DX); Read(DY);
  49. int Cost;
  50. per X1, X2;
  51. for(int i = 1; i <= F; i++){
  52. Read(X1.x); Read(X1.bottom);
  53. Read(X2.x); Read(X2.top);
  54. Read(Cost);
  55. query[++Q].x = X1.x; query[Q].top = X1.bottom + 1; query[Q].bottom = X2.top + DY - 2;
  56. query[Q].cost = Cost; query[Q].index = i;
  57. query[++Q].x = X2.x + DX - 2; query[Q].top = X1.bottom; query[Q].bottom = X2.top + DY - 2;
  58. if(query[Q].top < query[Q - 1].top) query[Q].top = query[Q - 1].top;
  59. query[Q].cost = -Cost; query[Q].index = i;
  60. }
  61. sort(query + 1, query + Q + 1, cmp);
  62. }
  63.  
  64. inline void Build(int node, int st, int nd){
  65. if(st == nd)
  66. T[node].sum = 2e9;
  67. else{
  68. int mid = (st + nd) / 2;
  69. Build(2 * node, st, mid);
  70. Build(2 * node + 1, mid + 1, nd);
  71. T[node].sum = 2e9;
  72. }
  73. }
  74.  
  75. inline void Query(int node, int st, int nd, int A, int B){
  76. if(A <= st && nd <= B){
  77. ans = min(ans, T[node].sum);
  78. } else {
  79. int mid = (st + nd) / 2, ok = 0;
  80. int x = T[node].inc + T[node].dif;
  81. if(mid >= A) {
  82. T[2 * node].sum += x;
  83. Query(2 * node, st, mid, A, B);
  84. T[2 * node].sum -= x;
  85. }
  86. if(mid + 1 <= B) {
  87. T[2 * node + 1].sum += x;
  88. Query(2 * node + 1, mid + 1, nd, A, B);
  89. T[2 * node + 1].sum -= x;
  90. }
  91. }
  92. }
  93.  
  94.  
  95.  
  96. inline void Update(int node, int st, int nd, int A, int B, int val){
  97. if(A <= st && nd <= B){
  98. //if(T[node].sum == 2e9) T[node].sum = 0;
  99. if(val < 0) T[node].dif += val;
  100. if(val > 0) T[node].inc += val;
  101. T[node].sum = T[node].inc + T[node].dif;
  102. } else {
  103. int mid = (st + nd) / 2, ok = 0;
  104. if(mid >= A)
  105. Update(2 * node, st, mid, A, B, val);
  106. if(mid + 1 <= B)
  107. Update(2 * node + 1, mid + 1, nd, A, B, val);
  108. int x = T[node].inc + T[node].dif;
  109. T[node].sum = min(T[2 * node].sum + x, T[2 * node + 1].sum + x);
  110. }
  111. }
  112.  
  113. int main(){
  114.  
  115. IN = fopen("demolish.in", "r");
  116. freopen("demolish.out", "w", stdout);
  117.  
  118. read();
  119. Build(1, 1, M + 1);
  120.  
  121. ans = 2e9;
  122. for(int i = 1; i <= Q; i++){
  123. if(query[i].x >= DX - 1)
  124. Query(1, 1, M + 1, DY - 1, M + 1);
  125. int j = i;
  126. while(query[j].x == query[i].x && j < Q){
  127. int y = query[j].bottom;
  128. int x = query[j].top;
  129. if(y > M) y = M;
  130. if(x > N) x = N;
  131. Update(1, 1, M + 1, x + 1, y + 1, query[j].cost);
  132. j++;
  133. }
  134. if(i != j) i = j - 1;
  135. }
  136. printf("%d", ans);
  137.  
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement