POJ 1990 Moofest--a tree-like array

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.