Advertisement
a53

Cambridge

a53
Nov 23rd, 2019
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cassert>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int n, q, t[100010], d[100010], leftt[100010];
  8.  
  9. struct arbint
  10. {
  11. int ap;
  12. long long dif, lazy;
  13. arbint *le, *ri;
  14.  
  15. arbint ()
  16. {
  17. ap = 0;
  18. dif = 2000000000000000LL;
  19. lazy = 0LL;
  20.  
  21. le = ri = NULL;
  22. }
  23. } *root, *nul;
  24.  
  25. void propag (arbint *node, int a, int b)
  26. {
  27. node->dif += node->lazy;
  28.  
  29. if (a != b)
  30. {
  31. if (node->le == NULL) node->le = new arbint;
  32. if (node->ri == NULL) node->ri = new arbint;
  33.  
  34. node->le->lazy += node->lazy;
  35. node->ri->lazy += node->lazy;
  36. }
  37.  
  38. node->lazy = 0LL;
  39. }
  40.  
  41. void update (int a, int b, int x, int y, int val, arbint *node)
  42. {
  43. if (a != b)
  44. {
  45. if (node->le == NULL) node->le = new arbint;
  46. if (node->ri == NULL) node->ri = new arbint;
  47. }
  48.  
  49. propag (node, a, b);
  50.  
  51. if (x <= a && b <= y)
  52. {
  53. node->lazy += 1LL * val;
  54. propag (node, a, b);
  55. return;
  56. }
  57.  
  58. int mij = (a + b) >> 1;
  59. if (x <= mij) update (a, mij, x, y, val, node->le);
  60. else propag (node->le, a, mij);
  61.  
  62. if (y > mij) update (mij + 1, b, x, y, val, node->ri);
  63. else propag (node->ri, mij + 1, b);
  64.  
  65. node->dif = min (node->le->dif, node->ri->dif);
  66. }
  67.  
  68. bool query ()
  69. {
  70. if (root->le == NULL) root->le = new arbint;
  71. if (root->ri == NULL) root->ri = new arbint;
  72.  
  73. propag (root, 1, 1000000000);
  74.  
  75. return (root->dif > 0LL);
  76. }
  77.  
  78. void change (int a, int b, int x, int val, arbint *node)
  79. {
  80. if (a != b)
  81. {
  82. if (node->le == NULL) node->le = new arbint;
  83. if (node->ri == NULL) node->ri = new arbint;
  84. }
  85.  
  86. propag (node, a, b);
  87.  
  88. if (x <= a && b <= x)
  89. {
  90. if (node->ap == 0) node->dif -= 2000000000000000LL - x;
  91.  
  92. node->ap += val;
  93.  
  94. if (node->ap == 0) node->dif += 2000000000000000LL - x;
  95.  
  96. return;
  97. }
  98.  
  99. int mij = (a + b) >> 1;
  100. if (x <= mij) change (a, mij, x, val, node->le), propag (node->ri, mij + 1, b);
  101. else if (x > mij) change (mij + 1, b, x, val, node->ri), propag (node->le, a, mij);
  102.  
  103. node->dif = min (node->le->dif, node->ri->dif);
  104. }
  105.  
  106. int main ()
  107. {
  108. // freopen ("file.in", "r", stdin);
  109.  
  110. assert (scanf ("%d %d", &n, &q) == 2);
  111.  
  112. assert (1 <= n && n <= 100000);
  113. assert (1 <= q && q <= 100000);
  114.  
  115. for (int i = 1; i <= n; ++i)
  116. {
  117. assert (scanf ("%d %d", &t[i], &d[i]) == 2);
  118.  
  119. assert (1 <= t[i] && t[i] <= 1000000000);
  120. assert (1 <= d[i] && d[i] <= 1000000000);
  121. }
  122.  
  123. root = new arbint;
  124.  
  125.  
  126. int p = 1;
  127. for (int i = 1; i <= n; ++i)
  128. {
  129. change (1, 1000000000, d[i], 1, root);
  130. update (1, 1000000000, d[i], 1000000000, -t[i], root);
  131.  
  132. for (; !query ();)
  133. {
  134. update (1, 1000000000, d[p], 1000000000, t[p], root);
  135. change (1, 1000000000, d[p], -1, root);
  136.  
  137. ++p;
  138. }
  139.  
  140. leftt[i] = p;
  141. }
  142.  
  143. for (int i = 1; i <= q; ++i)
  144. {
  145. int st, dr;
  146. assert (scanf ("%d %d", &st, &dr) == 2);
  147.  
  148. assert (1 <= st);
  149. assert (st <= dr);
  150. assert (dr <= n);
  151.  
  152. if (leftt[dr] <= st) printf ("1\n");
  153. else printf ("0\n");
  154. }
  155.  
  156. return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement