树状数组
View Code
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 typedef __int64 LL; 7 const int N=100010; 8 struct node 9 { 10 int nx,nh,x,h; 11 bool operator < (const node &a)const 12 { 13 return nh<a.nh; 14 } 15 }a[N]; 16 LL c1[N]; 17 int c2[N]; 18 bool cmp(const node &a,const node &b) 19 { 20 return a.nx<b.nx; 21 } 22 bool cmp2(const node &a,const node &b) 23 { 24 return a.h>b.h; 25 } 26 int lowbit(int x) 27 { 28 return x&(-x); 29 } 30 void add(int x,int p,int maxn) 31 { 32 for(int i=p;i<=maxn;i+=lowbit(i)) 33 { 34 c1[i]+=p; 35 c2[i]++; 36 } 37 } 38 void sum(int p,LL &s,int &c) 39 { 40 s=c=0; 41 for(int i=p;i>0;i-=lowbit(i)) 42 { 43 s+=c1[i]; 44 c+=c2[i]; 45 } 46 } 47 int main() 48 { 49 int n; 50 while(~scanf("%d",&n)) 51 { 52 memset(c1,0,sizeof(c1)); 53 memset(c2,0,sizeof(c2)); 54 for(int i=1;i<=n;i++) 55 scanf("%d%d",&a[i].nx,&a[i].nh); 56 sort(a+1,a+n+1,cmp); 57 a[1].x=1; 58 for(int i=2;i<=n;i++) 59 a[i].x=(a[i].nx==a[i-1].nx?a[i-1].x:i); 60 sort(a+1,a+n+1); 61 a[1].h=1; 62 for(int i=2;i<=n;i++) 63 a[i].h=(a[i].nh==a[i-1].nh?a[i-1].h:i); 64 sort(a+1,a+n+1,cmp2); 65 LL t=0; 66 add(a[1].x,a[1].x,n); 67 LL s=a[1].x; 68 int c=1; 69 for(int i=2;i<=n;i++) 70 { 71 LL s1; 72 int c1; 73 sum(a[i].x,s1,c1); 74 t+=((LL)c1*a[i].x-s1+s-s1-(c-c1)*a[i].x)*a[i].h; 75 add(a[i].x,a[i].x,n); 76 s+=a[i].x; c++; 77 } 78 printf("%I64d\n",t); 79 } 80 return 0; 81 }