4163 Hzwer and reverse ordertime limit: ten sspace limit: 256000 KBtitle level: Golden GoldTitle Description
Description
Hzwer in the study of reverse order.
For the sequence {a}, if the ordinal pair (i,j) satisfies: I<j,a[i]>a[j], then (I,J) is a pair of inverse pairs.
Given a sequence {a}, the number of inverse pairs is obtained.
Input data is large, please use scanf instead of CIN to read in.
Enter a description
Input Description
The first row is a number n, which indicates that {a} has n elements.
Next n number, describe {a}.
Output description
Output Description
A number that represents the number of reverse orders.
Sample input
Sample Input
5
3 1 5) 2 4
Sample output
Sample Output
4
Data range and Tips
Data Size & Hint
For 10% data, 1<=n<=100.
For 20% data, 1<=n<=10000.
For 30% data, 1<=n<=100000.
For 100% data, 1<=n<=1000000,1<=a[i]<=10^8.
#include <cstdio>#include<iostream>using namespacestd; #include<algorithm>#include<cstring>#defineMAXN 1000001structNode {Long Longv; intID; BOOL operator< (ConstNode &p)Const{returnv<p.v;}}; Node A[MAXN+Ten];Long Long intc[maxn+Ten],b[maxn+Ten];intN;inlineintLowbit (intx) { returnx& (-x);}Long LongQueryintx) { Long Longans=0; while(x) {ans+=C[x]; X-=lowbit (x); } returnans;}voidChangeintx) { while(x<=N) {c[x]++; X+=lowbit (x); }}intMain () {scanf ("%d",&N); Memset (A,0,sizeof(a)); Memset (c,0,sizeof(c)); memset (b,0,sizeof(b)); for(intI=1; i<=n;++i) {scanf ("%d",&(A[I].V)); A[i].id=i; } sort (A+1, a+n+1); intpre=-1; intPrevalue=0; for(intI=1; i<=n;++i) {if(pre!=a[i].v) {Pre=a[i].v; A[I].V=++Prevalue; } Elsea[i].v=Prevalue; } for(intI=1; i<=n;++i) b[a[i].id]=a[i].v; Long Long ints=0; for(inti=n;i>=1;--i) {change (b[i]); S+=query (b[i]-1); } cout<<s<<Endl; return 0;}View Code
Codevs 4163 The number of reverse pairs-tree Array method