Advertisement
Guest User

Untitled

a guest
Feb 26th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define fr(i, n, m) for(int i = (n); i < (m); i ++)
  3. #define pb push_back
  4. #define st first
  5. #define nd second
  6. #define pq priority_queue
  7. #define all(x) begin(x), end(x)
  8.  
  9. using namespace std;
  10. typedef long long ll;
  11. typedef long double ld;
  12. typedef pair<int,int> pii;
  13. ll const inf = 1e9;
  14. ll const mod = 1e9 + 7;
  15. ld const eps = 1e-13;
  16.  
  17. int a[1000001];
  18. int idx[1000001];
  19. int n;
  20. ll bit[1000001];
  21.  
  22. ll sum(int k){
  23. ll s = 0;
  24. while(k > 0){
  25. s += bit[k];
  26. k -= k&-k;
  27. }
  28. return s;
  29. }
  30. void update(int k, int x){
  31. while(k <= n){
  32. bit[k] += x;
  33. k += k&-k;
  34. }
  35. }
  36. bool cmp(int i, int j){
  37. return a[i] < a[j];
  38. }
  39.  
  40. int main()
  41. {
  42.  
  43. cin >> n;
  44. fr(i, 0, n){
  45. cin >> a[i];
  46. idx[i] = i;
  47. }
  48. sort(idx, idx + n, cmp);
  49. ll ans = 0;
  50. fr(i, 0, n){
  51. ans += (ll)sum(n) - sum(idx[i]);
  52. update(idx[i] + 1, 1);
  53. }
  54. cout << ans << endl;
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement