标签:
Time Limit: 1000MS | Memory Limit: 30000KB | 64bit IO Format: %lld & %llu |
Description
Input
Output
Sample Input
4 3 1 2 5 2 6 4 3
Sample Output
57
Source
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #define LL long long 8 using namespace std; 9 const int mxn=30010; 10 struct node{ 11 int v; 12 int x; 13 }c[mxn]; 14 int cmpv(const node a,const node b){ 15 return a.v<b.v; 16 } 17 int n; 18 LL ans=0; 19 // 20 LL t[2][mxn+1]; 21 inline int lowbit(int x){return x&-x;} 22 void add(int k,int p,int v){//k==0 个数 k==1 距离 23 while(p<=mxn){t[k][p]+=v;p+=lowbit(p);} 24 } 25 LL smm(int k,int x){ 26 LL res=0; 27 while(x){res+=t[k][x];x-=lowbit(x);} 28 return res; 29 } 30 // 31 int main(){ 32 scanf("%d",&n); 33 int i,j; 34 for(i=1;i<=n;i++) scanf("%lld%lld",&c[i].v,&c[i].x); 35 sort(c+1,c+n+1,cmpv); 36 for(i=1;i<=n;i++){ 37 int num=smm(0,c[i].x); 38 LL dis=smm(1,c[i].x); 39 ans+=c[i].v*(num*c[i].x-dis);//算x小于当前值的牛 40 ans+=(smm(1,mxn)-dis-(i-1-num)*c[i].x)*c[i].v;//算x大于当前值的牛 41 add(0,c[i].x,1); 42 add(1,c[i].x,c[i].x); 43 } 44 printf("%lld\n",ans); 45 return 0; 46 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5885353.html