标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2164 Accepted Submission(s): 838
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 #define mem(x,y) memset(x,y,sizeof(x)) 8 typedef long long LL; 9 const int MAXN=1e5+100; 10 const int MOD=100000007; 11 LL tree[MAXN+1]; 12 int lowbit(int x){return x&(-x);} 13 void update(int x,int y){ 14 while(x<=MAXN){ 15 tree[x]++; 16 x+=lowbit(x); 17 } 18 } 19 LL sum(int x){ 20 LL sum=0; 21 while(x>0){ 22 sum+=tree[x]; 23 x-=lowbit(x); 24 } 25 return sum; 26 } 27 int main(){ 28 int T,N,temp,flot=0; 29 scanf("%d",&T); 30 while(T--){ 31 scanf("%d",&N); 32 mem(tree,0); 33 LL n1,n2; 34 LL ans=0; 35 for(int i=1;i<=N;i++){ 36 scanf("%d",&temp); 37 update(temp,1); 38 n1=sum(temp-1);//比temp小的; 39 n2=N-temp-(i-n1-1); 40 // printf("%d %d\n",n1,n2); 41 ans+=n2*(n2-1)/2; 42 ans-=n1*n2; 43 } 44 printf("Case #%d: %lld\n",++flot,ans%MOD); 45 } 46 return 0; 47 }
标签:
原文地址:http://www.cnblogs.com/handsomecui/p/4966686.html