Advertisement
Guest User

Untitled

a guest
Nov 14th, 2016
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <cstring>
  6. #include <deque>
  7. #include <stack>
  8. #include <stdio.h>
  9. #include <map>
  10. #include <set>
  11. #include <time.h>
  12. #include <string>
  13. #include <fstream>
  14. #include <queue>
  15. #include <bitset>
  16. #include <cstdlib>
  17. #define X first
  18. #define Y second
  19. #define mp make_pair
  20. #define pb push_back
  21. #define pdd pair<double,double>
  22. #define pii pair<ll,ll>
  23. #define PI 3.14159265358979323846
  24. #define MOD 1000000007
  25. #define MOD2 1000000009
  26. #define INF ((ll)1e+18)
  27. #define x1 fldgjdflgjhrthrl
  28. #define x2 fldgjdflgrtyrtyjl
  29. #define y1 fldggfhfghjdflgjl
  30. #define y2 ffgfldgjdflgjl
  31. #define N 500500
  32. #define SUM 23423
  33. #define MAG 1048576
  34. #define OPEN 0
  35. #define CLOSE 1
  36. typedef long long ll;
  37. typedef long double ld;
  38. using namespace std;
  39. ll i,j,n,k,l,m,x,y,tot, flag,h,r,ans,z, K,x1,y1,x2,y2;
  40. string s;
  41. vector<vector<ll> > a;
  42. vector<vector<ll> > t;
  43. vector<pii> scol[1005000];
  44. ll dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
  45. bool good(ll x, ll y, ll val)
  46. {
  47. if (x < 0 || x >= n || y < 0 || y >= m)
  48. return false;
  49. if (val == (a[x][y]&(MAG-1)))
  50. return true;
  51. return false;
  52. }
  53. void dfs(ll x, ll y, ll col)
  54. {
  55. if (a[x][y] == col)
  56. return;
  57. a[x][y] = col;
  58. scol[col].push_back(mp(x,y));
  59. for (int i = 0; i < 4; i++)
  60. if (good(x+dir[i][0], y+dir[i][1], 1))
  61. dfs(x+dir[i][0], y+dir[i][1], col);
  62. }
  63. void print()
  64. {
  65. for (int i = 0; i < n; i++)
  66. {
  67. for (int j = 0; j < m; j++)
  68. cout << a[i][j] << " ";
  69. cout << endl;
  70. }
  71. }
  72. int main() {
  73. //freopen("input.txt","r",stdin);
  74. //freopen("output.txt","w",stdout);
  75. ll tests;
  76. cin >> tests;
  77. while (tests--)
  78. {
  79. cin >> n >> m >> x >> k;
  80. a.clear();
  81. t.clear();
  82. a.resize(n);
  83. for (i = 0; i < n; i++)
  84. a[i].resize(m);
  85. t.resize(n+1);
  86. for (i = 0; i < n+1; i++)
  87. t[i].resize(m+1);
  88. for (i = 0; i < x; i++)
  89. {
  90. cin >> x1 >> y1 >> x2 >> y2;
  91. if (n<m)
  92. {
  93. for (j = x1; j <= x2; j++)
  94. {
  95. t[j][y1]++;
  96. t[j][y2+1]--;
  97. }
  98. } else
  99. {
  100. for (j = y1; j <= y2; j++)
  101. {
  102. t[x1][j]++;
  103. t[x2+1][j]--;
  104. }
  105. }
  106. }
  107. if (n < m)
  108. {
  109. for (i = 0; i < n; i++)
  110. {
  111. ll sum = 0;
  112. for (j = 0; j < m; j++)
  113. {
  114. sum += t[i][j];
  115. a[i][j] = sum;
  116. }
  117. }
  118. } else
  119. {
  120. for (j = 0; j < m; j++)
  121. {
  122. ll sum = 0;
  123. for (i = 0; i < n; i++)
  124. {
  125. sum += t[i][j];
  126. a[i][j] = sum;
  127. }
  128. }
  129. }
  130. for (i = 0; i < n; i++)
  131. {
  132. for (j = 0; j < m; j++)
  133. if (a[i][j] >= k)
  134. a[i][j] = 1;
  135. else
  136. a[i][j] = 0;
  137. }
  138. ll starting_color = 2;
  139. for (i = 0; i < n; i++)
  140. for (j = 0; j < m; j++)
  141. if (a[i][j] == 1)
  142. {
  143. scol[starting_color].clear();
  144. dfs(i,j,starting_color);
  145. starting_color++;
  146. }
  147. /*for (i = 0; i < n; i++)
  148. {
  149. for (j = 0; j < m; j++)
  150. cout << a[i][j];
  151. cout << endl;
  152. }*/
  153. ll ans = 0;
  154. for (i = 2; i < starting_color; i++)
  155. {
  156. ll sum = 0;
  157. for (j = 0; j < scol[i].size(); j++)
  158. {
  159. x = scol[i][j].X, y = scol[i][j].Y;
  160. if (a[x][y] > MAG)
  161. continue;
  162. sum++;
  163. if (!good(x-1,y,i) && !good(x+1,y,i))
  164. {
  165. a[x][y] += MAG;
  166. k = y;
  167. while (good(x,k+1,i))
  168. {
  169. k++;
  170. a[x][k] += MAG;
  171. }
  172. k = y;
  173. while (good(x,k-1,i))
  174. {
  175. k--;
  176. a[x][k] += MAG;
  177. }
  178. } else
  179. if (!good(x,y-1,i) && !good(x,y+1,i))
  180. {
  181. a[x][y] += MAG;
  182. k = x;
  183. while (good(k+1,y,i))
  184. {
  185. k++;
  186. a[k][y] += MAG;
  187. }
  188. k = x;
  189. while (good(k-1,y,i))
  190. {
  191. k--;
  192. a[k][y] += MAG;
  193. }
  194. } else
  195. sum--;
  196. //print();
  197. }
  198. ans += sum;
  199. }
  200. cout << ans << endl;
  201. }
  202. return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement