Advertisement
pasholnahuy

Untitled

Oct 3rd, 2023
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <unordered_set>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class PersistentArray {
  9. struct Node {
  10. int value = 0;
  11. Node *left = nullptr;
  12. Node *right = nullptr;
  13. };
  14.  
  15. vector<Node *> versions;
  16. int size;
  17.  
  18. void Build(Node *n, const vector<int> &arr, int vl, int vr) {
  19. if (vl + 1 == vr) {
  20. n->value = arr[vl];
  21. return;
  22. }
  23. auto vm = vl + (vr - vl) / 2;
  24. n->left = new Node;
  25. n->right = new Node;
  26. Build(n->left, arr, vl, vm);
  27. Build(n->right, arr, vm, vr);
  28. n->value = n->left->value + n->right->value;
  29. }
  30.  
  31. Node *Set(Node *cur, int vl, int vr, int index, int value) {
  32. if (vl + 1 == vr) {
  33. return new Node{value};
  34. }
  35. auto vm = vl + (vr - vl) / 2;
  36. if (index < vm) {
  37. auto *n = new Node{0, Set(cur->left, vl, vm, index, value), cur->right};
  38. n->value = n->left->value + n->right->value;
  39. return n;
  40. }
  41. auto *n = new Node{0, cur->left, Set(cur->right, vm, vr, index, value)};
  42. n->value = n->left->value + n->right->value;
  43. return n;
  44. }
  45.  
  46. int GetMinInd(Node *n, int k, int l, int r) {
  47. if (k == 0) {
  48. return l;
  49. }
  50. if (l + 1 == r) {
  51. return r;
  52. }
  53. int m = l + (r - l) / 2;
  54. if (k <= n->left->value) {
  55. return GetMinInd(n->left, k, l, m);
  56. }
  57. return GetMinInd(n->right, k - n->left->value, m, r);
  58. }
  59.  
  60. public:
  61. explicit PersistentArray(const vector<int> &arr) : size(arr.size()) {
  62. versions.push_back(new Node);
  63. Build(versions.front(), arr, 0, size);
  64. }
  65.  
  66. void Set(int version, int index, int value) {
  67. versions.push_back(Set(versions[version], 0, size, index, value));
  68. }
  69.  
  70. int GetMinInd(int version, int k) {
  71. if (versions[version]->value < k) {
  72. return 0;
  73. }
  74. return GetMinInd(versions[version], k, 0, size);
  75. }
  76.  
  77. int GetLast() const { return versions.size() - 1; }
  78. };
  79.  
  80. int main() {
  81. ios::sync_with_stdio(false);
  82. cin.tie(nullptr);
  83. int n, m;
  84. cin >> n >> m;
  85. vector<int> vec(n);
  86. unordered_set<int> used;
  87. vector<int> is_first(n);
  88. for (int i = 0; i < n; ++i) {
  89. cin >> vec[i];
  90. if (!used.contains(vec[i])) {
  91. used.insert(vec[i]);
  92. is_first[i] = 1;
  93. } else {
  94. is_first[i] = 0;
  95. }
  96. }
  97. unordered_map<int, int> last;
  98. vector<int> prev(n, 0);
  99. for (int i = 0; i < n; ++i) {
  100. if (!last.contains(vec[i])) {
  101. prev[i] = -1;
  102. } else {
  103. prev[i] = last[vec[i]];
  104. }
  105. last[vec[i]] = i;
  106. }
  107. PersistentArray arr(is_first);
  108. vector<int> min_ind(n, 0);
  109. vector<int> valid_vers(n, 0);
  110. for (int i = 0; i < n; ++i) {
  111. if (prev[i] != -1) {
  112. arr.Set(arr.GetLast(), i, 1);
  113. arr.Set(arr.GetLast(), prev[i], 0);
  114. if (i > 0) {
  115. valid_vers[i] = valid_vers[i - 1] + 2;
  116. } else {
  117. valid_vers[i] = 0;
  118. }
  119. } else {
  120. if (i > 0){
  121. valid_vers[i] = valid_vers[i-1];
  122. }
  123.  
  124. }
  125. }
  126. int t;
  127. cin >> t;
  128. int p = 0;
  129. for (int i = 0; i < t; ++i) {
  130. int x, y;
  131. cin >> x >> y;
  132. auto l = (x + p) % n;
  133. int k = (y + p) % m + 1;
  134. int ans = arr.GetMinInd(valid_vers[l], k);
  135. cout << ans << '\n';
  136. p = ans;
  137. }
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement