日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

模板——树状数组求逆序对

發(fā)布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模板——树状数组求逆序对 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接:https://www.luogu.org/problemnew/show/P1908

1 #include <map> 2 #include <set> 3 #include <cmath> 4 #include <queue> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <iostream> 9 #include <algorithm> 10 #define forn(i, n) for (int i = 0; i < (n); i++) 11 #define forab(i, a, b) for (int i = (a); i <= (b); i++) 12 #define forba(i, b, a) for (int i = (b); i >= (a); i--) 13 #define mset(a, n) memset(a, n, sizeof(a)) 14 #define fast ios::sync_with_stdio(0), cin.tie(0), cout.tie(0) 15 #define P pair<int,int> 16 #define fi first 17 #define se second 18 using namespace std; 19 #define N 500005 20 #define maxn 1005 21 #define inf 0x3f3f3f3f 22 #define ll long long 23 ll a[N], c[N * 4]; 24 ll b[N]; 25 int n; 26 int sum; 27 inline int lowbit(int x) { return x & (-x); } 28 void add(int x,int y) 29 { 30 for (; x <= N;x+=lowbit(x)) 31 c[x] += y; 32 } 33 int ask(int x) 34 { 35 int ans = 0; 36 for (; x;x-=lowbit(x)) 37 ans += c[x]; 38 return ans; 39 } 40 bool cmp(int x,int y) 41 { 42 return b[x] > b[y]; 43 } 44 int main() 45 { 46 scanf("%d", &n); 47 forab(i, 1, n) 48 { 49 scanf("%d", b + i); 50 a[i] = i; 51 } 52 sort(a + 1, a + 1 + n, cmp); 53 // forab(i, 1, n) cout << a[i] << " "; 54 // cout << endl; 55 forab(i, 1, n) 56 { 57 58 // cout << "sum=" << sum << endl; 59 add(a[i], 1); 60 sum += ask(a[i] - 1); 61 } 62 cout << sum << endl; 63 system("pause"); 64 } 65 /* 66 6 67 5 4 2 6 3 1 68 69 */ View Code

轉(zhuǎn)載于:https://www.cnblogs.com/zssst/p/11120988.html

總結(jié)

以上是生活随笔為你收集整理的模板——树状数组求逆序对的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。