fork download
  1. /**
  2.  * author: orzvanh14 ( Độc cô cầu đặc )
  3.  * created: 18.04.2026 03:56:02
  4.  * too lazy to update time
  5. **/
  6. // i wants to take ioi
  7. //binhtinhtutinkhongcaycunhungmotkhikhongcontutinnualatuyetvong
  8. #include <bits/stdc++.h>
  9.  
  10. using namespace std;
  11.  
  12. #define int long long
  13. #define nn "\n"
  14. #define pi pair<int, int>
  15. #define ti tuple<int, int, int>
  16. #define fi first
  17. #define se second
  18. #define lb lower_bound
  19. #define ub upper_bound
  20. #define eb emplace_back
  21. #define pb push_back
  22. #define TASK " "
  23.  
  24. #define ms(a, x) memset(a, x, sizeof(a))
  25. #define all(a) a.begin(), a.end()
  26. #define All(a, n) a + 1, a + 1 + n
  27.  
  28. #define LOG 19
  29.  
  30. const int INF = 1e18;
  31. const int N = 1e5 + 5;
  32. const int maxn = 100 + 5;
  33. const int mod = 1e9 + 7;
  34.  
  35.  
  36. struct node{
  37. int kc, u;
  38. bool operator<(const node& other) const {
  39. return kc > other.kc;
  40. }
  41. };
  42. struct edge{
  43. int v, w, h;
  44. };
  45. int n, a[N];
  46. int kq = 0;
  47. int bit[N];
  48. void nhap(){
  49. cin >> n;
  50. for(int i = 1 ; i <= n; i++){
  51. cin >> a[i];
  52. }
  53. }
  54. void update(int x, int val){
  55. for(; x < N; x += x&-x) bit[x] += val;
  56. }
  57. int get(int x){
  58. int ans =0;
  59. for(; x >= 1; x -= x&-x) ans += bit[x];
  60. return ans;
  61. }
  62. void solve(){
  63. for(int i = 1; i <= n; i++){
  64. kq += (i - 1) - get(a[i]);
  65. update(a[i], 1);
  66. }
  67. cout << kq << nn;
  68. }
  69. signed main(){
  70. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  71. nhap();
  72. solve();
  73. return 0;
  74. }
  75.  
Success #stdin #stdout 0s 5316KB
stdin
4
2 4 1 3
stdout
3