Test instructions: The cow's hearing is V, the i,j between the two cows is exchanged, and the volume of Max (V[i],v[j]) *dist (I,J) is required. Ask all 22 cows to talk when the volume sum ∑ (max (v[i],v[j)) *abs (X[j]-x[i])), X[i] represents the coordinates of I
Solution: The cow according to the hearing from small to large order, so that each time a cow and other cattle to the value of hearing always take their own hearing.
Build two tree-like arrays, one to maintain the coordinates of the cattle, and one to maintain the number of cows.
When traversing to a cow I, find out how many cows on the left side of it, recorded as L, the right is recorded as R, and calculate the left coordinate of the sum of lsum, right coordinate values and rsum.
So at this time total and to add (L*x[i]-lsum) *v[i] (left) + (Rsum-r*x[i]) *v[i] (to the right), think carefully to know.
Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<algorithm>using namespacestd;#defineN 20007intC[n],cnt[n],n,maxi;structnode{intval,x;} P[n];intCMP (Node Ka,node KB) {returnKa.val <Kb.val;}intLowbit (intx) {returnx&-x;}voidModifyint*c,intXintval) { while(x <=Maxi) c[x]+ = val, x + =lowbit (x);}intGetsum (int*c,intx) { intres =0; while(X >0) {res + = c[x]; x-=lowbit (x);} returnRes;}intMain () {inti,j; while(SCANF ("%d", &n)! =EOF) {Maxi=0; for(i=1; i<=n;i++) scanf ("%d%d", &p[i].val,&p[i].x), Maxi =Max (maxi,p[i].x); Sort (P+1, p+n+1, CMP); Memset (c,0,sizeof(c)); memset (CNT,0,sizeof(CNT)); LLL Sum=0; for(i=1; i<=n;i++) { intL =getsum (cnt,p[i].x); intR = Getsum (Cnt,maxi)-Getsum (cnt,p[i].x-1); intLsum =getsum (c,p[i].x); intRsum = Getsum (C,maxi)-Getsum (c,p[i].x-1); Sum+ = 1ll* (p[i].x*l-lsum) *P[i].val; Sum+ = 1ll* (rsum-p[i].x*r) *P[i].val; Modify (c,p[i].x,p[i].x); Modify (cnt,p[i].x,1); } cout<<sum<<Endl; } return 0;}
View Code
POJ 1990 Moofest--a tree-like array