Advertisement
tiom4eg

pbds::Tree example

Dec 7th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. // Problem: https://sirius0621.contest.codeforces.com/group/a7fj9QBnKu/contest/332231/problem/C
  2. #include <bits/stdc++.h>
  3. // tiom4eg's precompiler options
  4. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  5. // IO settings
  6. #define fastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
  7. // Quick types
  8. #define ll long long
  9. #define ull unsigned long long
  10. #define pii pair <int, int>
  11. #define pll pair <ll, ll>
  12. #define vi vector <int>
  13. #define mi vector <vector <int> >
  14. // Quick functions
  15. #define endl "\n"
  16. #define F first
  17. #define S second
  18. #define all(a) a.begin(), a.end()
  19. #define pb push_back
  20. #define fint(n) int n; cin >> n
  21. #define fstr(s) string s; cin >> s
  22. #define farr(a, n) int a[n]; for (int pog = 0; pog < n; ++pog) cin >> a[pog]
  23. // Quick fors
  24. #define FOR1(s, n) for (int i = s; i < n; ++i)
  25. #define FOR2(s, n) for (int j = s; j < n; ++j)
  26. #define FOR3(s, n) for (int k = s; k < n; ++k)
  27. #define RFOR(n, s) for (int l = n; l >= s; --l)
  28. #define lk(x) (x << 1)
  29. #define rk(x) (x << 1) + 1
  30. // Pragmas
  31. #pragma GCC optimize("Ofast")
  32. #pragma target("avx,fma")
  33. #pragma comment(linker, "/stack:200000000")
  34. #include <ext/pb_ds/assoc_container.hpp>
  35. #include <ext/pb_ds/tree_policy.hpp>
  36. #define ordered_multiset tree <pii, null_type, less <pii>, rb_tree_tag, tree_order_statistics_node_update>
  37. // POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS POGGERS
  38. using namespace std;
  39. using namespace __gnu_pbds;
  40.  
  41. int main() {
  42.     fastIO;
  43.     map <int, pii> used;
  44.     ordered_multiset ordset;
  45.     fint(q);
  46.     FOR1(0, q) {
  47.         fstr(op);
  48.         if (op == "insert") {
  49.             fint(x);
  50.             if (used.find(x) == used.end()) {
  51.                 ordset.insert({x, 1});
  52.                 used[x] = {2, 1};
  53.             }
  54.             else {
  55.                 ordset.insert({x, used[x].F});
  56.                 used[x] = {used[x].F + 1, used[x].S};
  57.             }
  58.         }
  59.         else if (op == "erase") {
  60.             fint(x);
  61.             ordset.erase(ordset.find({x, used[x].S}));
  62.             used[x] = {used[x].F, used[x].S + 1};
  63.         }
  64.         else if (op == "count") {
  65.             fint(x);
  66.             cout << used[x].F - used[x].S << endl;
  67.         }
  68.         else if (op == "kth") {
  69.             fint(k);
  70.             cout << (*ordset.find_by_order(k - 1)).F << endl;
  71.         }
  72.         else {
  73.             fint(x);
  74.             cout << ordset.order_of_key({x, used[x].S}) << endl;
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement