Advertisement
999ms

PROJARKA TASK L

Dec 13th, 2020
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. #pragma GCC Optimaze("Ofast")
  2.  
  3. #include<bits/stdc++.h>
  4. #define all(x) begin(x),end(x)
  5.  
  6. using namespace std;
  7.  
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. const int N = 200200;
  12.  
  13. char mem_[450 * 1024 * 1024];
  14. size_t _ptr = 0;
  15. void* operator new(size_t cnt) {
  16. _ptr += cnt;
  17. return mem_ + _ptr - cnt;
  18. }
  19. void operator delete(void *) {}
  20.  
  21.  
  22. vector<int> g[N];
  23. int val[N];
  24. int x[N];
  25. int y[N];
  26.  
  27. void Dfs(int from) {
  28. x[from] = y[from] = val[from];
  29. for (int to : g[from]) {
  30. Dfs(to);
  31. x[from] = min(x[from], x[to]);
  32. y[from] = max(y[from], y[to]);
  33. }
  34. }
  35.  
  36. using ui = int;
  37. const int MVAL = 800800 + 100;
  38.  
  39. struct SegmentTree {
  40. struct Node {
  41. Node* left = nullptr;
  42. Node* right = nullptr;
  43. int cnt = 0;
  44. Node() {}
  45. };
  46. Node* root = new Node();
  47. SegmentTree() {}
  48.  
  49. void Add(Node* v, ui tl, ui tr, ui index) {
  50. v->cnt++;
  51. if (tl + 1 < tr) {
  52. ui tm = (tl + tr) >> 1;
  53. if (index < tm) {
  54. if (v->left == nullptr) {
  55. v->left = new Node();
  56. }
  57. Add(v->left, tl, tm, index);
  58. } else {
  59. if (v->right == nullptr) {
  60. v->right = new Node();
  61. }
  62. Add(v->right, tm, tr, index);
  63. }
  64. }
  65. }
  66.  
  67. int Get(Node* v, ui tl, ui tr, ui l, ui r) {
  68. if (!v || tl >= r || tr <= l) return 0;
  69. if (tl >= l && tr <= r) return v->cnt;
  70. int tm = (tl + tr) >> 1;
  71. return Get(v->left, tl, tm, l, r) + Get(v->right, tm, tr, l, r);
  72. }
  73. };
  74.  
  75. struct SegmentTree2D {
  76. struct Node2D {
  77. Node2D* left = nullptr;
  78. Node2D* right = nullptr;
  79. SegmentTree* tree = nullptr;
  80. Node2D() {
  81. tree = new SegmentTree();
  82. }
  83. };
  84. SegmentTree2D() {
  85. }
  86. Node2D* root = new Node2D();
  87.  
  88. void Add(Node2D* v, ui tl, ui tr, ui x, ui y) {
  89. v->tree->Add(v->tree->root, 0, MVAL, y);
  90. if (tl + 1 < tr) {
  91. ui tm = (tl + tr) >> 1;
  92. if (x < tm) {
  93. if (v->left == nullptr) {
  94. v->left = new Node2D();
  95. }
  96. Add(v->left, tl, tm, x, y);
  97. } else {
  98. if (v->right == nullptr) {
  99. v->right = new Node2D();
  100. }
  101. Add(v->right, tm, tr, x, y);
  102. }
  103. }
  104. }
  105. int Get(Node2D* v, ui tl, ui tr, ui l, ui r, ui y1, ui y2) {
  106. if (!v || tl >= r || tr <= l) return 0;
  107. if (tl >= l && tr <= r) return v->tree->Get(v->tree->root, 0, MVAL, y1, y2);
  108. int tm = (tl + tr) >> 1;
  109. return Get(v->left, tl, tm, l, r, y1, y2) + Get(v->right, tm, tr, l, r, y1, y2);
  110. }
  111. };
  112.  
  113.  
  114. int main() {
  115. ios_base::sync_with_stdio(false);
  116. cin.tie(nullptr);
  117. cout.tie(nullptr);
  118. int n;
  119. cin >> n;
  120. set<int> st;
  121. for (int i = 0; i < n; i++) {
  122. int f, t;
  123. cin >> f >> t;
  124. f--, t--;
  125. cin >> val[i];
  126. if (f >= 0) g[i].push_back(f);
  127. if (t >= 0) g[i].push_back(t);
  128. }
  129.  
  130. Dfs(0);
  131. for (int i = 0; i < n; i++) {
  132. st.insert(x[i]);
  133. st.insert(y[i]);
  134.  
  135. }
  136. int q;
  137. cin >> q;
  138. vector<pair<int,int>> qu(q);
  139. for (auto& [l, r] : qu) {
  140. cin >> l >> r;
  141. st.insert(l);
  142. st.insert(r);
  143. }
  144.  
  145. {
  146. int itr = 0;
  147. map<int, int> mp;
  148. for (int val : st) mp[val] = itr++;
  149. for (int i = 0; i < n; i++) {
  150. x[i] = mp[x[i]];
  151. y[i] = mp[y[i]];
  152. }
  153. for (auto& [l, r] : qu) {
  154. l = mp[l];
  155. r = mp[r];
  156. }
  157. }
  158.  
  159.  
  160. SegmentTree2D* tree = new SegmentTree2D();
  161.  
  162. for (int i = 0; i < n; i++) {
  163. tree->Add(tree->root, 0, MVAL, x[i], y[i]);
  164. }
  165.  
  166. for (auto& [l, r] : qu) {
  167. int inside = tree->Get(tree->root, 0, MVAL, l, MVAL, 0, r + 1);
  168. int left = tree->Get(tree->root, 0, MVAL, 0, MVAL, 0, l);
  169. int right = tree->Get(tree->root, 0, MVAL, r + 1, MVAL, 0, MVAL);
  170.  
  171. int cnt = n - inside - left - right;
  172.  
  173. cout << cnt * 2 + 1 << '\n';
  174. }
  175.  
  176.  
  177.  
  178.  
  179. }
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement