URAL 1019 - Line Painting(线段树)

思路

被区间表示整了一个晚上。

和POJ那题一样,离散化,插点

代码

 
 
  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <ctime>
  13. #include <cstdlib>
  14. #include <fstream>
  15. #include <string>
  16. #include <sstream>
  17. #include <map>
  18. #include <cmath>
  19. #define LL long long
  20. #define SZ(x) (int)x.size()
  21. #define Lowbit(x) ((x) & (-x))
  22. #define MP(a, b) make_pair(a, b)
  23. #define MS(arr, num) memset(arr, num, sizeof(arr))
  24. #define PB push_back
  25. #define F first
  26. #define S second
  27. #define ROP freopen("input.txt", "r", stdin);
  28. #define MID(a, b) (a + ((b - a) >> 1))
  29. #define LC rt << 1, l, mid
  30. #define RC rt << 1|1, mid + 1, r
  31. #define LRT rt << 1
  32. #define RRT rt << 1|1
  33. #define BitCount(x) __builtin_popcount(x)
  34. const double PI = acos(-1.0);
  35. const int INF = 0x3f3f3f3f;
  36. using namespace std;
  37. const int MAXN = 10000 + 5;
  38. const int MOD = 20071027;
  39. typedef pair<int, int> pii;
  40. typedef vector<int>::iterator viti;
  41. typedef vector<pii>::iterator vitii;
  42. struct POINT
  43. {
  44. int l, r;
  45. char ch;
  46. }pit[MAXN];
  47. int col[MAXN << 4], cnt, vis[MAXN << 1];
  48. vector<int> num;
  49. void PushDown(int rt)
  50. {
  51. if (col[rt] != -1)
  52. {
  53. col[LRT] = col[RRT] = col[rt];
  54. col[rt] = -1;
  55. }
  56. }
  57. void Query(int rt, int l, int r)
  58. {
  59. if (col[rt] != -1)
  60. {
  61. if (col[rt] == 0)
  62. fill(vis + l, vis + r + 1, 1);
  63. return;
  64. }
  65. if (l == r) return;
  66. int mid = MID(l, r);
  67. Query(LC); Query(RC);
  68. }
  69. void Update(int rt, int l, int r, int L, int R, int val)
  70. {
  71. if (L <= l && r <= R)
  72. {
  73. col[rt] = val;
  74. return;
  75. }
  76. if (col[rt] == val) return;
  77. PushDown(rt);
  78. int mid = MID(l, r);
  79. if (L <= mid) Update(LC, L, R, val);
  80. if (R > mid) Update(RC, L, R, val);
  81. }
  82. int main()
  83. {
  84. //ROP;
  85. int n, i, j;
  86. while (~scanf("%d", &n))
  87. {
  88. MS(vis, 0);
  89. num.clear();
  90. char ch[2];
  91. for (i = 0; i < n; i++)
  92. {
  93. scanf("%d%d%s", &pit[i].l, &pit[i].r, ch);
  94. pit[i].ch = ch[0];
  95. num.PB(pit[i].l); num.PB(pit[i].r);
  96. }
  97. num.PB(0); num.PB(1e9);
  98. sort(num.begin(), num.end());
  99. int m = unique(num.begin(), num.end()) - num.begin();
  100. num.resize(m);
  101. for (i = 0; i < m - 1; i++)
  102. if (num[i] + 1 != num[i + 1]) num.PB(num[i] + 1);
  103. sort(num.begin(), num.end());
  104. int curSize = SZ(num);
  105. for (i = 0; i < n; i++)
  106. {
  107. int l = lower_bound(num.begin(), num.end(), pit[i].l) - num.begin();
  108. int r = lower_bound(num.begin(), num.end(), pit[i].r) - num.begin();
  109. if (pit[i].ch == 'w') Update(1, 0, curSize - 1, l + 1, r, 0);
  110. else Update(1, 0, curSize - 1, l + 1, r, 1);
  111. }
  112. Query(1, 0, curSize - 1);
  113. int ans = 1, L = 0, R = 1;
  114. for (i = 1; i < curSize; i++)
  115. {
  116. if (vis[i])
  117. {
  118. for (j = i + 1; j < curSize && vis[j]; j++);
  119. int cnt = num[j - 1] - num[i - 1];
  120. if (cnt > ans)
  121. {
  122. ans = cnt;
  123. L = i - 1, R = j - 1;
  124. }
  125. i = j - 1;
  126. }
  127. }
  128. printf("%d %d\n", num[L], num[R]);
  129. }
  130. return 0;
  131. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值