Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

http://acm.hdu.edu.cn/showproblem.php?pid=2838

View Code
 1 #include <cstdio>
2 #include <cstring>
3 using namespace std;
4
5 const int N=100100,maxn=100000;
6 int ccnt[N];
7 __int64 csum[N];
8 int lowbit(int x)
9 {
10 return x&(x^(x-1));
11 }
12 int main()
13 {
14 int n,x;
15 while (~scanf("%d",&n))
16 {
17 memset(ccnt,0,sizeof(ccnt));
18 memset(csum,0,sizeof(csum));
19 int scnt=0;
20 __int64 ssum=0,ans=0;
21 while (n--)
22 {
23 scanf("%d",&x);
24 scnt++; ssum+=x;
25 for(int i=x;i<=maxn;i+=lowbit(i))
26 ccnt[i]++, csum[i]+=x;
27 int cnt=0;
28 __int64 sum=0;
29 for(int i=x;i>0;i-=lowbit(i))
30 cnt+=ccnt[i], sum+=csum[i];
31 ans+=ssum-sum+(__int64)x*(scnt-cnt);
32 }
33 printf("%I64d\n",ans);
34 }
35 return 0;
36 }

 

posted on 2012-03-27 15:42  Qiuqiqiu  阅读(265)  评论(0编辑  收藏  举报