Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2. #define gc getchar_unlocked
  3.  
  4. using namespace std;
  5.  
  6. inline void wpisz(int &x)
  7. {
  8. x = 0;
  9. long long mnoz = 1;
  10. register char c = gc();
  11. if(c == '-')
  12. {
  13. mnoz = -1;
  14. c = gc();
  15. }
  16. for(;c<48||c>57;c=gc());
  17. for(;c>47&&c<58;c=gc())
  18. x = ((x<<3) + (x<<1) + c-48);
  19. x *= mnoz;
  20. }
  21.  
  22. void update(int* tree, int leaf_index, int value, bool node = false) {
  23. if (leaf_index < 1) return;
  24. if(node)
  25. {
  26. tree[leaf_index] = tree[leaf_index * 2] + tree[leaf_index * 2 + 1];
  27. }
  28. else
  29. {
  30. tree[leaf_index] = value;
  31. }
  32. update(tree, leaf_index/2, value, true);
  33. }
  34.  
  35.  
  36. int query(int v, int L, int R , int a, int b, int* tree) //'v' jest numerem węzła w drzewie
  37. {
  38. int k;
  39. if (L == a && R == b) {
  40. return tree[v];
  41. }
  42.  
  43. int mid = (a + b) / 2;
  44. if (R <= mid) {
  45. return query(2 * v, L, R, a, mid, tree);
  46. } else if (L > mid) {
  47. return query(2 * v + 1, L, R, mid + 1, b, tree);
  48. } else {
  49. return query(2 * v, L, mid, a, mid, tree) + query(2 * v + 1, mid + 1, R, mid + 1, b, tree);
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. std::ios_base::sync_with_stdio(0);
  56. int q, k, x, v, L, R, n, value, operation;
  57. wpisz(n);
  58. wpisz(q);
  59. wpisz(k);
  60. const int LEAF_COUNT = pow(2,ceil(log2(n))); // ile lisci
  61. const int TREE_SIZE = 2 * LEAF_COUNT; // ile nodeow w drzewie
  62. int* tree = new int[TREE_SIZE]; // robimy drzewo
  63. for(int i = 0; i < TREE_SIZE; i++)
  64. {
  65. tree[i] = 0;
  66. }
  67. for(int i = 0; i < n; i++) // tutaj wbijamy nasze pierwsze liczby
  68. {
  69. wpisz(x); // tu x uzywamy do wartosci
  70. if(x <= k) value = 0;
  71. else value = 1;
  72. update(tree, i + LEAF_COUNT, value);
  73. }
  74.  
  75. for(int i = 0; i < q; i++) // operacje
  76. {
  77. wpisz(operation);
  78. if(operation == 0)
  79. {
  80. wpisz(x);
  81. wpisz(v);
  82. v = v <= k ? 0 : 1;
  83. x += (LEAF_COUNT-1);
  84. update(tree, x, v);
  85. }
  86. else
  87. {
  88. wpisz(L);
  89. wpisz(R);
  90. cout << query(1, L, R, 1, LEAF_COUNT, tree) << "\n";
  91. }
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement