【逆序对】【线段树】【树状数组】Data Structure Special Training 1 T1 rotinv 题解

来源:互联网 发布:淘宝关键词热度 编辑:程序博客网 时间:2024/06/06 15:39

这里写图片描述

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <set>#include <queue>#include <algorithm>#include <vector>#include <cstdlib>#include <cmath>#include <ctime>#include <stack>#define INF 2100000000#define LL long long#define clr(x) memset(x,0,sizeof(x))#define ms(a,x) memset(x,a,sizeof(x))#ifdef win32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;const int maxx = 1000005;int n,c[maxx],a[maxx];template <class T> inline void read(T &x) {    int flag = 1; x = 0;    char ch = getchar();    while(ch <  '0' || ch >  '9') { if(ch == '-')  flag = -1; ch = getchar(); }    while(ch >= '0' && ch <= '9') { x = (x<<1)+(x<<3)+ch-'0'; ch = getchar(); }    x *= flag;}inline int lowbit(int x) { return x & (-x); }inline void add( int x ) { for(register int i = x; i <= n; i += lowbit(i)) c[i] += 1; }inline int query(int x) {    int ans = 0;    for(register int i = x; i; i -= lowbit(i)) ans += c[i];    return ans;}int main() {    freopen("rotinv.in","r",stdin);    freopen("rotinv.out","w",stdout);    scanf("%d",&n);    LL ans = 0, cnt = 0;    for(register int i = 1; i <= n; i++) {        scanf("%d",&a[i]);        add(a[i]);        cnt += i-query(a[i]);    }       for(register int i = 1; i <= n; i++) {        cnt += (n-query(a[i]))-query(a[i]-1);        ans += cnt;    }    cout << ans << endl;    return 0;}
0 0