fork download
  1. #include <iostream>
  2. #include <climits>
  3. #include <unordered_map>
  4. #include <random>
  5. #include <chrono>
  6. #include <numeric>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <deque>
  13. #include <functional>
  14. #include <bitset>
  15. #include <string>
  16. #include <sstream>
  17. #include <fstream>
  18. #include <iomanip>
  19. #include <cmath>
  20. #include <cassert>
  21. #include <list>
  22. #include <forward_list>
  23. #include <set>
  24. #include <unordered_set>
  25. #include <cstdint>
  26.  
  27. using namespace std;
  28.  
  29. struct node {
  30. vector<pair<int, int>> sect;
  31. node(vector<pair<int, int>> sectt = vector<pair<int, int>>{}) {
  32. this->sect = sectt;
  33. }
  34. };
  35.  
  36. vector<node> tree;
  37. vector<pair<int, int>> v;
  38.  
  39. node mergee(node a, node b) {
  40. vector<pair<int, int>> ea, eb;
  41. for (int i = 0; i < (int)a.sect.size(); i++) {
  42. ea.push_back({ a.sect[i].first, -1 });
  43. ea.push_back({ a.sect[i].second, 1 });
  44. }
  45. for (int i = 0; i < (int)b.sect.size(); i++) {
  46. eb.push_back({ b.sect[i].first, -1 });
  47. eb.push_back({ b.sect[i].second, 1 });
  48. }
  49. vector<pair<int, int>> e;
  50. merge(ea.begin(), ea.end(), eb.begin(), eb.end(), back_inserter(e));
  51. vector<pair<int, int>> ans;
  52. int openn = 0;
  53. int pred = -1;
  54. for (auto [x, tip] : e) {
  55. if (tip == -1) {
  56. if (openn == 0) {
  57. pred = x;
  58. }
  59. openn++;
  60. }
  61. else {
  62. openn--;
  63. if (openn == 0) {
  64. ans.push_back({ pred, x });
  65. pred = -1;
  66. }
  67. }
  68. }
  69. node aaa(ans);
  70. return aaa;
  71. }
  72.  
  73. void build(int i, int l, int r) {
  74. if (l + 1 == r) {
  75. tree[i].sect = { v[l] };
  76. return;
  77. }
  78. int mid = (l + r) / 2;
  79. build(2 * i + 1, l, mid);
  80. build(2 * i + 2, mid, r);
  81. tree[i] = mergee(tree[2 * i + 1], tree[2 * i + 2]);
  82. }
  83.  
  84. int findd(int i, int l, int r, int ql, int qr, int x) {
  85. if (qr <= l || r <= ql) {
  86. return -1;
  87. }
  88. if (l + 1 == r) {
  89. if (tree[i].sect[0].first <= x && x <= tree[i].sect[0].second) {
  90. return l;
  91. }
  92. return -1;
  93. }
  94. if (ql <= l && r <= qr) {
  95. int mid = (l + r) / 2;
  96. int j = upper_bound(tree[2 * i + 1].sect.begin(), tree[2 * i + 1].sect.end(), pair<int, int>{x, 1e9}) - tree[2 * i + 1].sect.begin() - 1;
  97. if (j != -1 && tree[2 * i + 1].sect[j].second >= x) {
  98. return findd(2 * i + 1, l, mid, ql, qr, x);
  99. }
  100. j = upper_bound(tree[2 * i + 2].sect.begin(), tree[2 * i + 2].sect.end(), pair<int, int>{x, 1e9}) - tree[2 * i + 2].sect.begin() - 1;
  101. if (j != -1 && tree[2 * i + 2].sect[j].second >= x) {
  102. return findd(2 * i + 2, mid, r, ql, qr, x);
  103. }
  104. return -1;
  105. }
  106. int mid = (l + r) / 2;
  107. int ansk = -1;
  108. if (!(qr <= l || mid <= ql)) {
  109. ansk = findd(2 * i + 1, l, mid, ql, qr, x);
  110. }
  111. if (ansk != -1) {
  112. return ansk;
  113. }
  114. if (!(qr <= mid || r <= ql)) {
  115. ansk = findd(2 * i + 2, mid, r, ql, qr, x);
  116. }
  117. if (ansk != -1) {
  118. return ansk;
  119. }
  120. return -1;
  121. }
  122.  
  123. int main() {
  124. ios_base::sync_with_stdio(false);
  125. cin.tie(nullptr);
  126. int n;
  127. cin >> n;
  128. v.resize(n);
  129. for (int i = 0; i < n; i++) {
  130. cin >> v[i].first >> v[i].second;
  131. }
  132. tree.resize(4 * n);
  133. build(0, 0, n);
  134. vector<int> outt(n);
  135. for (int i = n - 1; i >= 0; i--) {
  136. int indx = findd(0, 0, n, i + 1, n, v[i].second);
  137. if (indx == -1) {
  138. outt[i] = v[i].second;
  139. }
  140. else {
  141. outt[i] = outt[indx];
  142. }
  143. }
  144. int m;
  145. cin >> m;
  146. for (int i = 0; i < m; i++) {
  147. int x, t;
  148. cin >> x >> t;
  149. x--;
  150. int indx = findd(0, 0, n, x, n, t);
  151. if (indx == -1) {
  152. cout << 0 << '\n';
  153. }
  154. else {
  155. cout << outt[indx] - t << '\n';
  156. }
  157. }
  158. return 0;
  159. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Standard output is empty