Guest User

Untitled

a guest
Mar 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. void build(vector<int>& pIndex, map<int, set<int>>& pSet, vector<int> input)
  9. {
  10. for (int i = 0; i < input.size(); i++)
  11. {
  12. set<int> &currentSet = pSet[input[i]];
  13.  
  14. if (!currentSet.empty())
  15. {
  16. pIndex[i] = *currentSet.rbegin();
  17. }
  18. currentSet.insert(i);
  19. }
  20. }
  21.  
  22. void del(vector<int>& pIndex, map<int, set<int>>& pSet, vector<int> input, int pos)
  23. {
  24. set<int> &currentSet = pSet[input[pos]];
  25. set<int>::iterator iter = currentSet.erase(currentSet.find(pos));
  26.  
  27. if (iter == currentSet.end()) return;
  28.  
  29. pIndex[*iter] = (iter == currentSet.begin()) ? -1 : *prev(iter);
  30. }
  31.  
  32. void update(vector<int>& pIndex, map<int, set<int>>& pSet, vector<int> input, int pos, int newValue)
  33. {
  34. set<int> &currentSet = pSet[newValue];
  35. set<int>::iterator iter = currentSet.insert(pos).first;
  36. pIndex[*iter] = (iter == currentSet.begin()) ? -1 : *prev(iter);
  37. set<int>::iterator iterNext = next(iter);
  38. if (iterNext != currentSet.end())
  39. {
  40. pIndex[*iterNext] = *iter;
  41. }
  42. }
  43.  
  44. void print(vector<int>& pIndex, int x, int y)
  45. {
  46. int cnt = 0;
  47. for (int i = x; i < y; i++)
  48. {
  49. if (pIndex[i] < x)
  50. {
  51. cnt++;
  52. }
  53. }
  54. cout << cnt << endl;
  55. }
  56.  
  57. int main()
  58. {
  59. int length, cases;
  60. cin >> length >> cases;
  61.  
  62. vector<int> input(length);
  63. for (int i = 0; i < length; i++)
  64. {
  65. cin >> input[i];
  66. }
  67.  
  68. vector<int> positionIndex(input.size(), -1);
  69. map<int, set<int>> positionSet;
  70.  
  71. build(positionIndex, positionSet, input);
  72.  
  73. while (cases-- > 0)
  74. {
  75. char op;
  76. int x, y;
  77. cin >> op >> x >> y;
  78.  
  79. if (op == 'M')
  80. {
  81. if (input[x] == y) continue;
  82.  
  83. del(positionIndex, positionSet, input, x);
  84. update(positionIndex, positionSet, input, x, y);
  85. input[x] = y;
  86. }
  87. else if (op == 'Q')
  88. {
  89. print(positionIndex, x, y);
  90. }
  91. }
  92.  
  93. return 0;
  94. }
Add Comment
Please, Sign In to add comment