Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <map>
  5. #include <vector>
  6. #include <unordered_map>
  7. #include <numeric>
  8. #include <random>
  9. #include <set>
  10.  
  11. //#define FAST_ALLOCATOR_MEMORY 1e8
  12. #include "optimization.h"
  13.  
  14. using namespace std;
  15.  
  16. struct S {
  17.     int val, cnt, index;
  18.  
  19.     S() {}
  20.     S(int _val, int _cnt, int _index) : val(_val), cnt(_cnt), index(_index) {}
  21.  
  22.     bool operator < (const S& s) const {
  23.         if (cnt == s.cnt)
  24.             return index < s.index;
  25.         return cnt < s.cnt;
  26.     }
  27. };
  28.  
  29. const int MAXN = 3e5 + 7;
  30.  
  31. int ms[MAXN];
  32.  
  33. signed main(void) {
  34.     #ifdef LOCAL
  35.         freopen("a.in", "r", stdin);
  36.         freopen("a.out", "w", stdout);
  37.     #endif
  38.     ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  39.     int x, n = readInt();
  40.     unordered_map<int, set<int>> mp(5 * n);
  41.     set<S> s;
  42.     for (int i = 0; i < n; ++i) {
  43.         x = readInt();
  44.         mp[x].insert(i);
  45.         ms[i] = x;
  46.     }
  47.     for (auto it = mp.begin(); it != mp.end(); ++it) {
  48.         s.insert({it->first, (int)it->second.size(), *it->second.begin()});
  49.     }
  50.     int q = readInt(), y;
  51.     char command;
  52.     set<S>::iterator it;
  53.     unordered_map<int, set<int>>::iterator mit;
  54.     while (q --> 0) {
  55.         command = readChar();
  56.         if (command == '?') {
  57.             it = s.begin();
  58.             writeInt(it->index, '\n');
  59.         } else {
  60.             x = readInt(), y = readInt();
  61.             mit = mp.find(ms[x]);
  62.             s.erase({ms[x], (int)mit->second.size(), *mit->second.begin()});
  63.             mit->second.erase(x);
  64.             if ((int)mit->second.size() == 0)
  65.                 mp.erase(ms[x]);
  66.             else
  67.                 s.insert({ms[x], (int)mit->second.size(), *mit->second.begin()});
  68.             mit = mp.find(y);
  69.             if (mit != mp.end())
  70.                 s.erase({y, (int)mit->second.size(), *mit->second.begin()});
  71.             else
  72.                 mp.insert({y, {}}), mit = mp.find(y);
  73.             mit->second.insert(x);
  74.             ms[x] = y;
  75.             s.insert({y, (int)mit->second.size(), *mit->second.begin()});
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement