Advertisement
bibaboba12345

до для B

Oct 24th, 2021
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <cmath>
  6. #include<iomanip>
  7. using namespace std;
  8. const int INF = 1e9, N = 6e5;
  9. int n, t, I, totalsum, a[N];
  10.  
  11. struct Node {
  12. vector<int> cont;
  13. int moreThan(int l) {
  14. int h = cont.end() - upper_bound(cont.begin(), cont.end(), l);
  15. return h;
  16. }
  17. int lessThan(int l) {
  18. int h = cont.begin() - lower_bound(cont.begin(), cont.end(), l);
  19. return -h;
  20. }
  21. };
  22.  
  23. struct SegmentTree {
  24. vector<Node> tree;
  25. void build(int n) {
  26. int h = 1;
  27. while (h < n) {
  28. h *= 2;
  29. }
  30. h *= 2;
  31. tree.resize(h);
  32. for (int i = h - 1; i > 0; i--) {
  33. tree[i].cont.clear();
  34. if (i >= h / 2) {
  35. tree[i].cont.push_back(a[i - h / 2]);
  36. }
  37. else {
  38. for (int j = 0; j < tree[i * 2].cont.size(); j++) {
  39. tree[i].cont.push_back(tree[i * 2].cont[j]);
  40. }
  41. for (int j = 0; j < tree[i * 2 + 1].cont.size(); j++) {
  42. tree[i].cont.push_back(tree[i * 2 + 1].cont[j]);
  43. }
  44. sort(tree[i].cont.begin(), tree[i].cont.end());
  45. }
  46. }
  47. }
  48.  
  49. int FindMore(int v, int l, int r, int L, int R, long long x) {
  50. if (L >= l && R <= r) {
  51. return tree[v].moreThan(x);
  52. }
  53. if (L > r || R < l) {
  54. return 0;
  55. }
  56. return FindMore(v * 2, l, r, L, (L + R) / 2, x) + FindMore(v * 2 + 1, l, r, (L + R) / 2 + 1, R, x);
  57. }
  58. int FindLess(int v, int l, int r, int L, int R, long long x) {
  59. if (L >= l && R <= r) {
  60. return tree[v].lessThan(x);
  61. }
  62. if (L > r || R < l) {
  63. return 0;
  64. }
  65. return FindLess(v * 2, l, r, L, (L + R) / 2, x) + FindLess(v * 2 + 1, l, r, (L + R) / 2 + 1, R, x);
  66. }
  67.  
  68. int More(int l, int r, int x) {
  69. return FindMore(1, l, r, 0, tree.size() / 2 - 1, x);
  70. }
  71. int Less(int l, int r, int x) {
  72. return FindLess(1, l, r, 0, tree.size() / 2 - 1, x);
  73. }
  74. };
  75. SegmentTree ST;
  76. int main()
  77. {
  78. ios::sync_with_stdio(false);
  79. cin.tie(0);
  80. cin >> n;
  81. for (int i = 0; i < n; i++) {
  82. cin >> a[i];
  83. }
  84. ST.build(n);
  85. int q;
  86. cin >> q;
  87. int u, v, x;
  88. for (int i = 0; i < q; i++) {
  89. cin >> u >> v >> x;
  90. cout << ST.More(u, v, x) << " " << ST.Less(u,v,x) << "\n";
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement