A:逆序数

写过一篇原理,这里不想多说。 传送门

代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define LL long long
 4 #define ULL unsigned LL
 5 #define fi first
 6 #define se second
 7 #define lson l,m,rt<<1
 8 #define rson m+1,r,rt<<1|1
 9 #define max3(a,b,c) max(a,max(b,c))
10 const int INF = 0x3f3f3f3f;
11 const LL mod = 1e9+7;
12 typedef pair<int,int> pll;
13 const int N = 100000+10;
14 int tree[N];
15 int lowbit(int x)
16 {
17     return x&(-x);
18 }
19 void add(int x)
20 {
21     while(x < N)
22     {
23         tree[x]++;
24         x += lowbit(x);
25     }
26 }
27 int Query(int x)
28 {
29     int ret = 0;
30     while(x)
31     {
32         ret += tree[x];
33         x -= lowbit(x);
34     }
35     return ret;
36 }
37 int main()
38 {
39     ios::sync_with_stdio(false);
40     cin.tie(0);
41     cout.tie(0);
42     int n;
43     cin >> n;
44     LL ans = 0;
45     while(n--)
46     {
47         int tmp;
48         cin >> tmp;
49         tmp++;
50         ans = ans + Query(N-1) - Query(tmp);
51         add(tmp);
52     }
53     cout << ans << endl;
54     return 0;
55 }
View Code

相关文章:

  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
猜你喜欢
  • 2021-10-15
  • 2022-12-23
  • 2021-12-29
  • 2021-11-12
  • 2021-06-20
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案