Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <set>
- #include <map>
- using namespace std;
- void build(vector<int>& pIndex, map<int, set<int>>& pSet, vector<int> input)
- {
- for (int i = 0; i < input.size(); i++)
- {
- set<int> ¤tSet = pSet[input[i]];
- if (!currentSet.empty())
- {
- pIndex[i] = *currentSet.rbegin();
- }
- currentSet.insert(i);
- }
- }
- void del(vector<int>& pIndex, map<int, set<int>>& pSet, vector<int> input, int pos)
- {
- set<int> ¤tSet = pSet[input[pos]];
- set<int>::iterator iter = currentSet.erase(currentSet.find(pos));
- if (iter == currentSet.end()) return;
- pIndex[*iter] = (iter == currentSet.begin()) ? -1 : *prev(iter);
- }
- void update(vector<int>& pIndex, map<int, set<int>>& pSet, vector<int> input, int pos, int newValue)
- {
- set<int> ¤tSet = pSet[newValue];
- set<int>::iterator iter = currentSet.insert(pos).first;
- pIndex[*iter] = (iter == currentSet.begin()) ? -1 : *prev(iter);
- set<int>::iterator iterNext = next(iter);
- if (iterNext != currentSet.end())
- {
- pIndex[*iterNext] = *iter;
- }
- }
- void print(vector<int>& pIndex, int x, int y)
- {
- int cnt = 0;
- for (int i = x; i < y; i++)
- {
- if (pIndex[i] < x)
- {
- cnt++;
- }
- }
- cout << cnt << endl;
- }
- int main()
- {
- int length, cases;
- cin >> length >> cases;
- vector<int> input(length);
- for (int i = 0; i < length; i++)
- {
- cin >> input[i];
- }
- vector<int> positionIndex(input.size(), -1);
- map<int, set<int>> positionSet;
- build(positionIndex, positionSet, input);
- while (cases-- > 0)
- {
- char op;
- int x, y;
- cin >> op >> x >> y;
- if (op == 'M')
- {
- if (input[x] == y) continue;
- del(positionIndex, positionSet, input, x);
- update(positionIndex, positionSet, input, x, y);
- input[x] = y;
- }
- else if (op == 'Q')
- {
- print(positionIndex, x, y);
- }
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment