nrk009

ORDERED SET POLICY BASED DATA STRUCTURE

Jul 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #include <ext/pb_ds/assoc_container.hpp>
  4. #include <ext/pb_ds/tree_policy.hpp>
  5. using namespace __gnu_pbds;
  6. #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  7. #define ll long long
  8. #define ld long double
  9. #define pb push_back
  10. #define fe first
  11. #define se second
  12. #define st(x) sort(x.begin(),x.end())
  13. #define rst(x) sort(x.rbegin(), x.rend())
  14. #define all(x) x.begin(),x.end()
  15. long double pi = 3.14159265358979323;
  16.  
  17. const double EPS = 1e-12;
  18. const int N = 1e6 + 5;
  19. const int mod = 1e9 + 7;
  20. #define ordered_set tree < pair < ll , ll > ,null_type,less < pair < ll , ll > >,rb_tree_tag,tree_order_statistics_node_update >
  21.  
  22. //using policy based data structure
  23.  
  24. int main()
  25. {
  26.     fast;
  27.     ll n;
  28.     cin >> n ;
  29.     ll a[n];
  30.     ll l[n] , r[n];
  31.     ordered_set s;
  32.     map < ll , ll > mp;
  33.     for(int i = 0 ; i < n ;i++ ){
  34.         cin >> a[i];
  35.         mp[a[i]]++;
  36.         if(i < n - 1)
  37.         l[i+1] = mp[a[i]];
  38.     }
  39.     mp.clear();
  40.     /*for(int i = 1 ; i < n ;i++ ){
  41.             cout << l[i] << " ";
  42.     }*/
  43.     for(int i = n - 1 ; i > 0 ;i-- ){
  44.             mp[a[i]]++;
  45.             r[n - i] = mp[a[i]];
  46.     }
  47.     ll l1[n];
  48.     for(int i = 1 ; i < n  ;i++)
  49.         l1[i] = l[n-i];
  50.     ll ans = 0 ;
  51.    
  52.     for(int i = 1 ; i < n ; i++ )
  53.     {
  54.         s.insert({r[i],i});
  55.     //Strickly less than
  56.         ans += s.order_of_key({l1[i],-1});
  57.     }
  58.     cout << ans;
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.    
  82.     return 0;
  83.    
  84. }
Add Comment
Please, Sign In to add comment