fork(2) download
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4. #include <climits>
  5. #include <vector>
  6. #include <queue>
  7. using namespace std;
  8.  
  9. int n;
  10. vector<pair<int, int> > v;
  11. int arr[1002][366];
  12. bool visited[1002][366];
  13. int dx[8] = {-1, -1, 0, 1, 1, 1 ,0, -1};
  14. int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
  15. queue<pair<int, int> > q;
  16. int sum = 0;
  17.  
  18. bool camp(const pair<int, int> &a, const pair<int, int> &b){
  19. if(a.first == b.first){
  20. return a.second > b.second;
  21. }
  22. return a.first < b.first;
  23. }
  24.  
  25. bool cango(int x, int y){
  26. return 0 <= x && 0 <= y && x < 366 && y < 366 && arr[x][y] == 1 && !visited[x][y];
  27. }
  28.  
  29. void func(){
  30. int min_x = q.front().first;
  31. int min_y = q.front().second;
  32. int max_x = 0;
  33. int max_y = 0;
  34. int flag = 0;
  35. while(!q.empty()){
  36. int x = q.front().first;
  37. int y = q.front().second;
  38. q.pop();
  39.  
  40. for(int i = 0; i < 8; i++){
  41. int nx = x + dx[i];
  42. int ny = y + dy[i];
  43.  
  44. if(cango(nx, ny)){
  45. flag = 1;
  46. q.push(make_pair(nx, ny));
  47. visited[nx][ny] = 1;
  48. max_x = max(max_x, nx);
  49. max_y = max(max_y, ny);
  50. }
  51. }
  52. }
  53. if(flag == 0){
  54. sum += 1;
  55. return;
  56. }
  57. max_x++;
  58. max_y++;
  59. sum += (max_x - min_x) * (max_y - min_y);
  60. //cout << sum << '\n';
  61. return;
  62. }
  63.  
  64. int main() {
  65. cin >> n;
  66. for(int i = 0; i < n; i++){
  67. int start, end;
  68. cin >> start >> end;
  69. v.push_back(make_pair(start, end));
  70. }
  71. sort(v.begin(), v.end(), camp);
  72.  
  73. int max_x = 0;
  74. for(int i = 0; i < n; i++){
  75. int x = v[i].first;
  76. int y = v[i].second;
  77. max_x = max(max_x, y);
  78. for(int j = 0; j < n; j++){
  79. if(arr[j][x] == 0){
  80. for(int k = x; k <= y; k++){
  81. arr[j][k] = 1;
  82. }
  83. break;
  84. }
  85. }
  86. }
  87.  
  88. // for(int i = 0; i < n; i++){
  89. // for(int j = 0; j <= max_x; j++){
  90. // cout << arr[i][j] << ' ';
  91. // }
  92. // cout << '\n';
  93. // }
  94.  
  95. for(int i = 0; i < n; i++){
  96. for(int j = 0; j <= max_x; j++){
  97. if(arr[i][j] == 1 && !visited[i][j]){
  98. q.push(make_pair(i, j));
  99. visited[i][j] = 1;
  100. func();
  101. }
  102. }
  103. }
  104.  
  105. cout << sum;
  106. }
Success #stdin #stdout 0.01s 5300KB
stdin
5
1 5
2 4
3 3
4 6
7 10
stdout
22