fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define mtv(kit) cout<<#kit<<" - "<<kit<<"\n";
  6. #define ff first
  7. #define ss second
  8. #define pb push_back
  9. #define rep(i,a,b) for(i=a;i<b;i++)
  10. #define pii pair<ll , ll>
  11. #define all(x) x.begin(),x.end()
  12. #define nl "\n"
  13. #define ump unordered_map
  14.  
  15. const ll N = 1e6 + 5;
  16. ll ft[N];
  17.  
  18. void add(ll in , ll val){
  19. for(;in <= N; in += in&-in)ft[in] += val;
  20. }
  21.  
  22. ll query(ll in){
  23. ll ans = 0;
  24. for(;in > 0; in -= in&-in)ans += ft[in];
  25. return ans;
  26. }
  27.  
  28. void doit(){
  29. ll n, i, s = 0, x, ans = 0;
  30. cin >> n;
  31. rep(i,0,n){
  32. cin >> x;
  33. ans += query(x - 1);
  34. add(x,x);
  35. }
  36. cout << ans << nl;
  37. }
  38.  
  39. int main() {
  40. // #ifndef ONLINE_JUDGE
  41. // freopen("input1.txt","r",stdin);
  42. // freopen("output1.txt","w",stdout);
  43. // #endif
  44. // ios_base::sync_with_stdio(false);
  45. cin.tie(NULL);
  46. ll t = 1;
  47. // cin >> t;
  48. for(ll i = 1; i <= t; i++){
  49. doit();
  50. }
  51. }
  52.  
Success #stdin #stdout 0s 4384KB
stdin
5
1 4 3 5 3
stdout
11