Advertisement
pasholnahuy

Untitled

Oct 3rd, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 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> next(n, 0);
  99. for (int i = n - 1; i >= 0; --i) {
  100. if (!last.contains(vec[i])) {
  101. next[i] = 0;
  102. } else {
  103. next[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 (next[i] != 0) {
  112. arr.Set(arr.GetLast(), i, 0);
  113. arr.Set(arr.GetLast(), next[i], 1);
  114. if (i > 0) {
  115. valid_vers[i] = valid_vers[i - 1] + 2;
  116. } else {
  117. valid_vers[i] = 0;
  118. }
  119. } else {
  120. arr.Set(arr.GetLast(), i, 0);
  121. if (i > 0) {
  122. valid_vers[i] = valid_vers[i - 1] + 1;
  123. } else {
  124. valid_vers[i] = 0;
  125. }
  126. }
  127. }
  128. int t;
  129. cin >> t;
  130. int p = 0;
  131. for (int i = 0; i < t; ++i) {
  132. int x, y;
  133. cin >> x >> y;
  134. auto l = (x + p) % n;
  135. int k = (y + p) % m + 1;
  136. int ans = arr.GetMinInd(valid_vers[l], k);
  137. cout << ans << '\n';
  138. p = ans;
  139. }
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement