fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cassert>
  5.  
  6. struct DataStruct {
  7. std::vector<int> arr, add;
  8.  
  9. const int GSIZE = 128;
  10.  
  11. DataStruct(int size, int value) {
  12. arr.assign(size, value);
  13. add.assign(size / GSIZE + 1, 0);
  14. }
  15.  
  16. void push(int g) {
  17. if (add[g] == 0) {
  18. return;
  19. }
  20. const int begin = GSIZE * g;
  21. const int after = std::min(begin + GSIZE, (int)arr.size());
  22. int a = add[g];
  23. for (int i = begin; i < after; ++i) {
  24. arr[i] += a;
  25. }
  26. add[g] = 0;
  27. }
  28.  
  29. void push_all() {
  30. for (int g = 0; g < (int)add.size(); ++g) {
  31. push(g);
  32. }
  33. }
  34.  
  35. void inc(int left, int right) {
  36. assert(right < (int)arr.size());
  37. int gl = left / GSIZE;
  38. int gr = right / GSIZE;
  39. if (gl == gr) {
  40. push(gl);
  41. for (int i = left; i <= right; ++i) {
  42. arr[i]++;
  43. }
  44. } else {
  45. push(gl);
  46. push(gr);
  47. for (int i = left, after = (gl+1) * GSIZE; i < after; ++i) {
  48. arr[i]++;
  49. }
  50. for (int g = gl+1; g < gr; ++g) {
  51. add[g]++;
  52. }
  53. for (int i = gr * GSIZE; i <= right; ++i) {
  54. arr[i]++;
  55. }
  56. }
  57. }
  58. };
  59.  
  60.  
  61. int main() {
  62. std::ios_base::sync_with_stdio(false);
  63. std::cin.tie(0); std::cout.tie(0); std::cerr.tie(0);
  64.  
  65. int n, last;
  66. std::cin >> n >> last;
  67.  
  68. DataStruct ds(last+1, 0);
  69.  
  70. for (int i = 0; i < n; ++i) {
  71. int x, p; std::cin >> x >> p;
  72. int l = std::max(0, x-p);
  73. int r = std::min(last, x+p);
  74. ds.inc(l, r);
  75. }
  76. ds.push_all();
  77. for (auto& it : ds.arr) if (it > 1) it = 0;
  78. int answ = 0;
  79. for (int i = 0; i <= last;) {
  80. while (i <= last && ds.arr[i] == 1) ++i;
  81. if (i > last) break;
  82. int j = i;
  83. while (j <= last && ds.arr[j] == 0) ++j;
  84. answ = std::max(answ, j-i);
  85. i = j;
  86. }
  87. std::cout << answ;
  88. return 0;
  89. }
Success #stdin #stdout 0s 4364KB
stdin
4 4
1 2
3 0
0 2
3 0
stdout
5