Luogu1382 楼房 (线段树 扫描线)

忘是亡心i 2021-11-17 05:10 413阅读 0赞

各种低级错误.jpg,数组开大就过.jpg
线段树离散化扫描线

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
  7. #define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
  8. #define Max(a,b) ((a) > (b) ? (a) : (b))
  9. #define Min(a,b) ((a) < (b) ? (a) : (b))
  10. #define Fill(a,b) memset(a, b, sizeof(a))
  11. #define Swap(a,b) a^=b^=a^=b
  12. #define ll long long
  13. //#define ON_DEBUG
  14. #ifdef ON_DEBUG
  15. #define D_e_Line printf("\n\n----------\n\n")
  16. #define D_e(x) cout << #x << " = " << x << endl
  17. #define Pause() system("pause")
  18. #else
  19. #define D_e_Line ;
  20. #endif
  21. struct ios{
  22. template<typename ATP>ios& operator >> (ATP &x){
  23. x = 0; int f = 1; char c;
  24. for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
  25. while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
  26. x*= f;
  27. return *this;
  28. }
  29. }io;
  30. using namespace std;
  31. #define lson rt << 1, l, mid
  32. #define rson rt << 1 | 1, mid + 1, r
  33. const int N = 400007;
  34. int t[N << 2];
  35. // do not need pushup
  36. inline void Pushdown(int rt){
  37. t[rt << 1] = Max(t[rt << 1], t[rt]);
  38. t[rt << 1 | 1] = Max(t[rt << 1 | 1], t[rt]);
  39. }
  40. inline void Updata(int rt, int l, int r, int L, int R, int w){
  41. if(L <= l && r <= R){
  42. t[rt] = Max(t[rt], w);
  43. return;
  44. }
  45. Pushdown(rt);
  46. int mid = (l + r) >> 1;
  47. if(L <= mid) Updata(lson, L, R, w);
  48. if(R > mid) Updata(rson, L, R, w);
  49. }
  50. inline int Query(int rt, int l, int r, int x){
  51. if(l == r) return t[rt];
  52. Pushdown(rt);
  53. int mid = (l + r) >> 1;
  54. if(x <= mid) return Query(lson, x);
  55. else return Query(rson, x);
  56. }
  57. int x[N], y[N], h[N];
  58. int a[N << 1];
  59. int main(){
  60. int n;
  61. io >> n;
  62. R(i,1,n){
  63. io >> h[i] >> x[i] >> y[i];
  64. a[(i << 1) - 1] = x[i];
  65. a[i << 1] = y[i];
  66. }
  67. sort(a + 1, a + (n << 1) + 1);
  68. int m = unique(a + 1, a + (n << 1) + 1) - a - 1;
  69. R(i,1,n){
  70. x[i] = lower_bound(a + 1, a + m + 1, x[i]) - a;
  71. y[i] = lower_bound(a + 1, a + m + 1, y[i]) - a;
  72. Updata(1, 1, m, x[i], y[i] - 1, h[i]); // how can I miss m as n, how can I ! how can I ! Ahhhhhhhhhhhhhhhhhhh
  73. }
  74. int tot = 0, last = 0;
  75. x[++tot] = a[1];
  76. y[tot] = 0;
  77. R(i,1,m - 1){
  78. int H = Query(1, 1, m, i);
  79. if(H != last){
  80. x[++tot] = a[i];
  81. y[tot] = H;
  82. x[++tot] = a[i + 1];
  83. y[tot] = H;
  84. }
  85. else{
  86. x[tot] = a[i + 1];
  87. }
  88. last = H;
  89. }
  90. printf("%d\n", tot + 1);
  91. R(i,1,tot){
  92. printf("%d %d\n", x[i], y[i]);
  93. }
  94. printf("%d 0", a[m]);
  95. return 0;
  96. }

1570282-20190720215425206-599658474.png

转载于:https://www.cnblogs.com/bingoyes/p/11219466.html

发表评论

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

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

相关阅读