矩形面积并、矩形面积交、矩形周长并(线段树、扫描线总结)

浅浅的花香味﹌ 2022-06-14 09:08 392阅读 0赞

转自:http://blog.csdn.net/lwt36/article/details/48908031

HDU 1542 [POJ 1151] Atlantis (矩形面积并)

  • 题意:

    求N<=100个矩形的面积并

  • 分析:

    • 离散化: 这些技巧都是老生常谈的了, 不然浮点数怎么建树, 离散化x坐标就可以了
    • 扫描线: 首先把矩形按y轴分成两条边, 上边和下边, 对x轴建树, 扫描线可以看成一根平行于x轴的直线.
      从y=0开始往上扫, 下边表示要计算面积+1, 上边表示已经扫过了−1, 直到扫到最后一条平行于x轴的边
      但是真正在做的时候, 不需要完全模拟这个过程, 一条一条边地插入线段树就好了
    • 线段树: 用于动态维护扫描线在往上走时, x轴哪些区域是有合法面积的
    • ps:这种线段树是不用lazy的, 因为不用push_down, 为啥不用push_down, 因为没有查询操作
  • 扫描线扫描的过程(建议配合代码模拟)

    ps:无论说的再好,都不如自己在纸上模拟一遍扫描的过程,我自己学的时候模拟了很多遍
    以下图转载自@kk303的博客

初始状态

初始状态

这里写图片描述

扫到最下边的线, 点1→3更新为1

这里写图片描述

扫到第二根线, 此时S=lcnt!=0∗h两根线之间, 得到绿色的面积, 加到答案中去, 随后更新计数

这里写图片描述

同上, 将黄色的面积加到答案中去

这里写图片描述

同上, 将灰色的面积加到答案中去

这里写图片描述

同上, 将紫色的面积加到答案中去

这里写图片描述

同上, 将蓝色的面积加到答案中去

  • 代码

    //
    // Created by TaoSama on 2015-07-14
    // Copyright (c) 2015 TaoSama. All rights reserved.
    //
    //#pragma comment(linker, “/STACK:1024000000,1024000000”)

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    using namespace std;

    define pr(x) cout << #x << “ = “ << x << “ “

    define prln(x) cout << #x << “ = “ << x << endl

    const int N = 205, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

    int n;
    struct Seg {

    1. double l, r, h; int d;
    2. Seg() {}
    3. Seg(double l, double r, double h, int d): l(l), r(r), h(h), d(d) {}
    4. bool operator< (const Seg& rhs) const {

    return h < rhs.h;}
    } a[N];

    int cnt[N << 2]; //根节点维护的是[l, r+1]的区间
    double sum[N << 2], all[N];

    define lson l, m, rt << 1

    define rson m + 1, r, rt << 1 | 1

    void push_up(int l, int r, int rt) {

    1. if(cnt[rt]) sum[rt] = all[r + 1] - all[l];
    2. else if(l == r) sum[rt] = 0; //leaves have no sons
    3. else sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];

    }

    void update(int L, int R, int v, int l, int r, int rt) {

    1. if(L <= l && r <= R) {
    2. cnt[rt] += v;
    3. push_up(l, r, rt);
    4. return;
    5. }
    6. int m = l + r >> 1;
    7. if(L <= m) update(L, R, v, lson);
    8. if(R > m) update(L, R, v, rson);
    9. push_up(l, r, rt);

    }

    int main() {

    ifdef LOCAL

    1. freopen("in.txt", "r", stdin);

    // freopen(“out.txt”,”w”,stdout);

    endif

    1. ios_base::sync_with_stdio(0);
    2. int kase = 0;
    3. while(scanf("%d", &n) == 1 && n) {
    4. for(int i = 1; i <= n; ++i) {
    5. double x1, y1, x2, y2;
    6. scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
    7. a[i] = Seg(x1, x2, y1, 1);
    8. a[i + n] = Seg(x1, x2, y2, -1);
    9. all[i] = x1; all[i + n] = x2;
    10. }
    11. n <<= 1;
    12. sort(a + 1, a + 1 + n);
    13. sort(all + 1, all + 1 + n);
    14. int m = unique(all + 1, all + 1 + n) - all - 1;
    15. memset(cnt, 0, sizeof cnt);
    16. memset(sum, 0, sizeof sum);
    17. double ans = 0;
    18. for(int i = 1; i < n; ++i) {
    19. int l = lower_bound(all + 1, all + 1 + m, a[i].l) - all;
    20. int r = lower_bound(all + 1, all + 1 + m, a[i].r) - all;
    21. if(l < r) update(l, r - 1, a[i].d, 1, m, 1);
    22. ans += sum[1] * (a[i + 1].h - a[i].h);
    23. }
    24. printf("Test case #%d\nTotal explored area: %.2f\n\n", ++kase, ans);
    25. }
    26. return 0;

    }

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 1
  93. 2
  94. 3
  95. 4
  96. 5
  97. 6
  98. 7
  99. 8
  100. 9
  101. 10
  102. 11
  103. 12
  104. 13
  105. 14
  106. 15
  107. 16
  108. 17
  109. 18
  110. 19
  111. 20
  112. 21
  113. 22
  114. 23
  115. 24
  116. 25
  117. 26
  118. 27
  119. 28
  120. 29
  121. 30
  122. 31
  123. 32
  124. 33
  125. 34
  126. 35
  127. 36
  128. 37
  129. 38
  130. 39
  131. 40
  132. 41
  133. 42
  134. 43
  135. 44
  136. 45
  137. 46
  138. 47
  139. 48
  140. 49
  141. 50
  142. 51
  143. 52
  144. 53
  145. 54
  146. 55
  147. 56
  148. 57
  149. 58
  150. 59
  151. 60
  152. 61
  153. 62
  154. 63
  155. 64
  156. 65
  157. 66
  158. 67
  159. 68
  160. 69
  161. 70
  162. 71
  163. 72
  164. 73
  165. 74
  166. 75
  167. 76
  168. 77
  169. 78
  170. 79
  171. 80
  172. 81
  173. 82
  174. 83
  175. 84
  176. 85
  177. 86
  178. 87
  179. 88
  180. 89
  181. 90
  182. 91

HDU 1255 覆盖的面积 (矩形面积交)

  • 题意:

    求N<=1000个矩形覆盖至少两次区域的面积,也就是矩形面积交

  • 分析

    • 前面的与矩形面积并类似, 不同的是push_up的时候要考虑至少覆盖一次one和至少覆盖两次two的更新
      尤其是当前被覆盖了一次的时候, 由于没有push_down操作, 父亲节点的信息是没有同步到儿子节点的, 这样的话push_up就要考虑了.
    • 父亲被记录覆盖了一次, 但是如果儿子被覆盖过, 这些操作都是在这个父亲这个大区间上的, 就相当于父亲区间被覆盖了至少两次, 所以two和one都要更新
  • 代码

    //
    // Created by TaoSama on 2015-10-04
    // Copyright (c) 2015 TaoSama. All rights reserved.
    //
    //#pragma comment(linker, “/STACK:1024000000,1024000000”)

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    using namespace std;

    define pr(x) cout << #x << “ = “ << x << “ “

    define prln(x) cout << #x << “ = “ << x << endl

    const int N = 2e3 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

    int n;
    struct Seg {

    1. double l, r, h; int d;
    2. Seg() {}
    3. Seg(double l, double r, double h, double d): l(l), r(r), h(h), d(d) {}
    4. bool operator< (const Seg& rhs) const {
    5. return h < rhs.h;
    6. }

    } a[N];

    int cnt[N << 2];
    double one[N << 2], two[N << 2], all[N];

    define lson l, m, rt << 1

    define rson m + 1, r, rt << 1 | 1

    void push_up(int l, int r, int rt) {

    1. if(cnt[rt] >= 2) two[rt] = one[rt] = all[r + 1] - all[l];
    2. else if(cnt[rt] == 1) {
    3. one[rt] = all[r + 1] - all[l];
    4. if(l == r) two[rt] = 0;
    5. else two[rt] = one[rt << 1] + one[rt << 1 | 1];
    6. } else {
    7. if(l == r) one[rt] = two[rt] = 0;
    8. else {
    9. one[rt] = one[rt << 1] + one[rt << 1 | 1];
    10. two[rt] = two[rt << 1] + two[rt << 1 | 1];
    11. }
    12. }

    }

    void update(int L, int R, int v, int l, int r, int rt) {

    1. if(L <= l && r <= R) {
    2. cnt[rt] += v;
    3. push_up(l, r, rt);
    4. return;
    5. }
    6. int m = l + r >> 1;
    7. if(L <= m) update(L, R, v, lson);
    8. if(R > m) update(L, R, v, rson);
    9. push_up(l, r, rt);

    }

    int main() {

    ifdef LOCAL

    1. freopen("in.txt", "r", stdin);

    // freopen(“out.txt”,”w”,stdout);

    endif

    1. ios_base::sync_with_stdio(0);
    2. int t; scanf("%d", &t);
    3. while(t--) {
    4. scanf("%d", &n);
    5. for(int i = 1; i <= n; ++i) {
    6. double x1, y1, x2, y2;
    7. scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
    8. a[i] = Seg(x1, x2, y1, 1);
    9. a[i + n] = Seg(x1, x2, y2, -1);
    10. all[i] = x1; all[i + n] = x2;
    11. }
    12. n <<= 1;
    13. sort(a + 1, a + 1 + n);
    14. sort(all + 1, all + 1 + n);
    15. int m = unique(all + 1, all + 1 + n) - all - 1;
    16. memset(cnt, 0, sizeof cnt);
    17. memset(one, 0, sizeof one);
    18. memset(two, 0, sizeof two);
    19. double ans = 0;
    20. for(int i = 1; i < n; ++i) {
    21. int l = lower_bound(all + 1, all + 1 + m, a[i].l) - all;
    22. int r = lower_bound(all + 1, all + 1 + m, a[i].r) - all;
    23. if(l < r) update(l, r - 1, a[i].d, 1, m, 1);
    24. ans += two[1] * (a[i + 1].h - a[i].h);
    25. }
    26. printf("%.2f\n", ans);
    27. }
    28. return 0;

    }

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 1
  106. 2
  107. 3
  108. 4
  109. 5
  110. 6
  111. 7
  112. 8
  113. 9
  114. 10
  115. 11
  116. 12
  117. 13
  118. 14
  119. 15
  120. 16
  121. 17
  122. 18
  123. 19
  124. 20
  125. 21
  126. 22
  127. 23
  128. 24
  129. 25
  130. 26
  131. 27
  132. 28
  133. 29
  134. 30
  135. 31
  136. 32
  137. 33
  138. 34
  139. 35
  140. 36
  141. 37
  142. 38
  143. 39
  144. 40
  145. 41
  146. 42
  147. 43
  148. 44
  149. 45
  150. 46
  151. 47
  152. 48
  153. 49
  154. 50
  155. 51
  156. 52
  157. 53
  158. 54
  159. 55
  160. 56
  161. 57
  162. 58
  163. 59
  164. 60
  165. 61
  166. 62
  167. 63
  168. 64
  169. 65
  170. 66
  171. 67
  172. 68
  173. 69
  174. 70
  175. 71
  176. 72
  177. 73
  178. 74
  179. 75
  180. 76
  181. 77
  182. 78
  183. 79
  184. 80
  185. 81
  186. 82
  187. 83
  188. 84
  189. 85
  190. 86
  191. 87
  192. 88
  193. 89
  194. 90
  195. 91
  196. 92
  197. 93
  198. 94
  199. 95
  200. 96
  201. 97
  202. 98
  203. 99
  204. 100
  205. 101
  206. 102
  207. 103
  208. 104

HDU 1828 [POJ 1177] Picture(矩形周长并)

  • 题意:

    求N<=5000个矩形的轮廓长度,也就是矩形周长并

  • 分析一:

    可以用类似矩形面积并的办法, 不过这次我们不乘高, 不算面积罢了.
    需要注意的是, 由于周长的线会被重复覆盖, 我们每次需要和上一次的作差.
    但是这样仅仅是x轴的, 不过我可以再y轴做一次加起来就可以了

  • 演示x轴求长度和的部分
    这里写图片描述
  • 代码一:

    //
    // Created by TaoSama on 2015-07-15
    // Copyright (c) 2015 TaoSama. All rights reserved.
    //
    //#pragma comment(linker, “/STACK:1024000000,1024000000”)

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    using namespace std;

    define pr(x) cout << #x << “ = “ << x << “ “

    define prln(x) cout << #x << “ = “ << x << endl

    const int N = 1e4 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

    int n, m[2];
    int sum[N << 2], cnt[N << 2], all[2][N];
    struct Seg {

    1. int l, r, h, d;
    2. Seg() {}
    3. Seg(int l, int r, int h, int d): l(l), r(r), h(h), d(d) {}
    4. bool operator< (const Seg& rhs) const {

    return h < rhs.h;}
    } a[2][N];

    define lson l, m, rt << 1

    define rson m + 1, r, rt << 1 | 1

    void push_up(int p, int l, int r, int rt) {

    1. if(cnt[rt]) sum[rt] = all[p][r + 1] - all[p][l];
    2. else if(l == r) sum[rt] = 0;
    3. else sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];

    }

    void update(int p, int L, int R, int v, int l, int r, int rt) {

    1. if(L <= l && r <= R) {
    2. cnt[rt] += v;
    3. push_up(p, l, r, rt);
    4. return;
    5. }
    6. int m = l + r >> 1;
    7. if(L <= m) update(p, L, R, v, lson);
    8. if(R > m) update(p, L, R, v, rson);
    9. push_up(p, l, r, rt);

    }

    int main() {

    ifdef LOCAL

    1. freopen("in.txt", "r", stdin);

    // freopen(“out.txt”,”w”,stdout);

    endif

    1. ios_base::sync_with_stdio(0);
    2. while(scanf("%d", &n) == 1) {
    3. for(int i = 1; i <= n; ++i) {
    4. int x1, y1, x2, y2;
    5. scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    6. all[0][i] = x1, all[0][i + n] = x2;
    7. all[1][i] = y1, all[1][i + n] = y2;
    8. a[0][i] = Seg(x1, x2, y1, 1);
    9. a[0][i + n] = Seg(x1, x2, y2, -1);
    10. a[1][i] = Seg(y1, y2, x1, 1);
    11. a[1][i + n] = Seg(y1, y2, x2, -1);
    12. }
    13. n <<= 1;
    14. sort(all[0] + 1, all[0] + 1 + n);
    15. m[0] = unique(all[0] + 1, all[0] + 1 + n) - all[0] - 1;
    16. sort(all[1] + 1, all[1] + 1 + n);
    17. m[1] = unique(all[1] + 1, all[1] + 1 + n) - all[1] - 1;
    18. sort(a[0] + 1, a[0] + 1 + n);
    19. sort(a[1] + 1, a[1] + 1 + n);

    // for(int i = 0; i < 2; ++i){
    // for(int j = 1; j <= m[i]; ++j) cout << all[i][j] <<’ ‘; cout << endl;
    // } cout << endl;

    1. int ans = 0;
    2. for(int i = 0; i < 2; ++i) {
    3. int t = 0, last = 0;
    4. memset(cnt, 0, sizeof cnt);
    5. memset(sum, 0, sizeof sum);
    6. for(int j = 1; j <= n; ++j) {
    7. int l = lower_bound(all[i] + 1, all[i] + 1 + m[i], a[i][j].l) - all[i];
    8. int r = lower_bound(all[i] + 1, all[i] + 1 + m[i], a[i][j].r) - all[i];
    9. if(l < r) update(i, l, r - 1, a[i][j].d, 1, m[i], 1);
    10. t += abs(sum[1] - last);
    11. last = sum[1];
    12. }
    13. ans += t;
    14. }
    15. printf("%d\n", ans);
    16. }
    17. return 0;

    }

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 1
  105. 2
  106. 3
  107. 4
  108. 5
  109. 6
  110. 7
  111. 8
  112. 9
  113. 10
  114. 11
  115. 12
  116. 13
  117. 14
  118. 15
  119. 16
  120. 17
  121. 18
  122. 19
  123. 20
  124. 21
  125. 22
  126. 23
  127. 24
  128. 25
  129. 26
  130. 27
  131. 28
  132. 29
  133. 30
  134. 31
  135. 32
  136. 33
  137. 34
  138. 35
  139. 36
  140. 37
  141. 38
  142. 39
  143. 40
  144. 41
  145. 42
  146. 43
  147. 44
  148. 45
  149. 46
  150. 47
  151. 48
  152. 49
  153. 50
  154. 51
  155. 52
  156. 53
  157. 54
  158. 55
  159. 56
  160. 57
  161. 58
  162. 59
  163. 60
  164. 61
  165. 62
  166. 63
  167. 64
  168. 65
  169. 66
  170. 67
  171. 68
  172. 69
  173. 70
  174. 71
  175. 72
  176. 73
  177. 74
  178. 75
  179. 76
  180. 77
  181. 78
  182. 79
  183. 80
  184. 81
  185. 82
  186. 83
  187. 84
  188. 85
  189. 86
  190. 87
  191. 88
  192. 89
  193. 90
  194. 91
  195. 92
  196. 93
  197. 94
  198. 95
  199. 96
  200. 97
  201. 98
  202. 99
  203. 100
  204. 101
  205. 102
  206. 103

  • 分析二:

    当然我们也可只对x轴做一次扫描线, 只要同时维护y轴竖线(就是求矩形面积并的时候的高)的个数, vtl记录竖线的个数
    需要的注意的是竖线重合的情况, 需要再开变量lbd,rbd来判断重合, 避免重复计算

  • 代码二:

    //
    // Created by TaoSama on 2015-07-15
    // Copyright (c) 2015 TaoSama. All rights reserved.
    //
    //#pragma comment(linker, “/STACK:1024000000,1024000000”)

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    include

    using namespace std;

    define pr(x) cout << #x << “ = “ << x << “ “

    define prln(x) cout << #x << “ = “ << x << endl

    const int N = 2e4 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

    int n;
    int sum[N << 2], cnt[N << 2], vtl[N << 2];
    bool lbd[N << 2], rbd[N << 2];
    struct Seg {

    1. int l, r, h, d;
    2. Seg() {}
    3. Seg(int l, int r, int h, int d): l(l), r(r), h(h), d(d) {}
    4. bool operator< (const Seg& rhs) const {

    return h < rhs.h;}
    } a[N];

    define lson l, m, rt << 1

    define rson m + 1, r, rt << 1 | 1

    void push_up(int l, int r, int rt) {

    1. if(cnt[rt]) {
    2. lbd[rt] = rbd[rt] = true;
    3. sum[rt] = r + 1 - l;
    4. vtl[rt] = 2;
    5. }

    //叶子节点的下面的节点也是0 不这样也可以(那就要数组开大 小心RE)

    1. else if(l == r) sum[rt] = vtl[rt] = lbd[rt] = rbd[rt] = 0;
    2. else {
    3. lbd[rt] = lbd[rt << 1];
    4. rbd[rt] = rbd[rt << 1 | 1];
    5. sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
    6. vtl[rt] = vtl[rt << 1] + vtl[rt << 1 | 1];
    7. if(rbd[rt << 1] && lbd[rt << 1 | 1]) vtl[rt] -= 2; //两条线重合
    8. }

    }

    void update(int L, int R, int v, int l, int r, int rt) {

    1. if(L <= l && r <= R) {
    2. cnt[rt] += v;
    3. push_up(l, r, rt);
    4. return;
    5. }
    6. int m = l + r >> 1;
    7. if(L <= m) update(L, R, v, lson);
    8. if(R > m) update(L, R, v, rson);
    9. push_up(l, r, rt);

    }

    int main() {

    ifdef LOCAL

    1. freopen("in.txt", "r", stdin);

    // freopen(“out.txt”,”w”,stdout);

    endif

    1. ios_base::sync_with_stdio(0);
    2. while(scanf("%d", &n) == 1) {
    3. int Min = 1e4, Max = -1e4;
    4. for(int i = 1; i <= n; ++i) {
    5. int x1, y1, x2, y2;
    6. scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    7. Min = min(Min, x1);
    8. Max = max(Max, x2);
    9. a[i] = Seg(x1, x2, y1, 1);
    10. a[i + n] = Seg(x1, x2, y2, -1);
    11. }
    12. n <<= 1;
    13. sort(a + 1, a + 1 + n);

    // memset(sum, 0, sizeof sum); 所有覆盖最后都被清除了 不需要初始化了
    // memset(cnt, 0, sizeof cnt);
    // memset(lbd, false, sizeof lbd);
    // memset(rbd, false, sizeof rbd);

    1. int ans = 0, last = 0;
    2. for(int i = 1; i <= n; ++i) {
    3. if(a[i].l < a[i].r) update(a[i].l, a[i].r - 1, a[i].d, Min, Max - 1, 1);
    4. ans += vtl[1] * (a[i + 1].h - a[i].h);
    5. ans += abs(sum[1] - last);
    6. last = sum[1];
    7. }
    8. printf("%d\n", ans);
    9. }
    10. return 0;

    }

发表评论

表情:
评论列表 (有 0 条评论,392人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java 矩形面积

    矩形面积交 问题描述   平面上有两个矩形,它们的边平行于直角坐标系的X轴或Y轴。对于每个矩形,我们给出它的一对相对顶点的坐标,请你编程算出两个矩形的交的面积。 输