Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 34679 | Accepted: 12466 |
Description
Input
Output
Sample Input
5 9 1 0 5 4 3 1 2 3 0
Sample Output
6 0
Source
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=500000+100; int da[MAXN]; int tda[MAXN]; //********************************************** int C[MAXN]; int Lowbit[MAXN]; //C[i] = a[i-lowbit(i)+1] + …+ a[i],下表从1开始 //Lowbit[i]=i&(i^(i-1));或Lowbit[i]=i&(-i); //1.查询 int QuerySum(int p) //查询原数组中下标1-p的元素的和 { int nSum=0; while(p>0) { nSum+=C[p]; p-=Lowbit[p]; } return nSum; } //2.修改+初始化 void Modify(int p,int val) //原数组中下表为p的元素+val,导致C[]数组中部分元素值的改变 { while(p<=MAXN-10) { C[p]+=val; p+=Lowbit[p]; } } //************************************************************ bool cmp(int a,int b) { return a<b; } int Binary_research(int tmp,int n) { int mid,low=1,high=n; while(low<=high) { mid=(low+high)>>1; if(tda[mid]<=tmp)low=mid+1; else high=mid-1; } return high; } int main() { int n,i; __int64 ans; for(i=1;i<MAXN;i++) Lowbit[i]=i&(-i); while(~scanf("%d",&n),n) { for(i=1;i<=n;i++) { scanf("%d",&da[i]); tda[i]=da[i]; } sort(tda+1,tda+n+1,cmp); memset(C,0,sizeof(C)); ans=0; for(i=1;i<=n;i++) { int id=Binary_research(da[i],n); ans+=(__int64)(QuerySum(n)-QuerySum(id)); Modify(id,1); } printf("%I64d\n",ans); } return 0; }