fork download
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. #pragma pack(1)
  5.  
  6. using namespace std;
  7. using namespace __gnu_pbds;
  8.  
  9. tree<
  10. pair<int,int>,
  11. null_type,
  12. less<pair<int,int>>,
  13. rb_tree_tag,
  14. tree_order_statistics_node_update> me;
  15.  
  16. #define TASK "test"
  17.  
  18. int main()
  19. {
  20. #ifndef ONLINE_JUDGE
  21. freopen(TASK".in","r",stdin);
  22. freopen(TASK".out","w",stdout);
  23. #endif // ONLINE_JUDGE
  24. ios::sync_with_stdio(0);
  25. cin.tie(0);
  26. map<int,int> cnt[2];
  27. int n;
  28. cin>>n;
  29. vector<int> a(n);
  30. for(auto &it:a) cin>>it;
  31. vector<int> pre(n),suf(n);
  32. for(int i=0;i<n;i++)
  33. {
  34. pre[i]=cnt[0][a[i]]++;
  35. suf[n-i-1]=cnt[1][a[n-i-1]]++;
  36. }
  37. long long ans=0;
  38. for(int i=1;i<n;i++)
  39. {
  40. me.insert({pre[i-1],i-1});
  41. ans+=i-me.order_of_key({suf[i],i});
  42. }
  43. cout<<ans<<endl;
  44. }
Runtime error #stdin #stdout #stderr 0s 3432KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc