POJ 1990 Moofest Tree-like array

Source: Internet
Author: User

The problem is that I saw the big Baishushu-shaped array after the first question of the brush, it is really difficult, so have to find the solution on the Internet, the practice of online is really wonderful. The main test instructions of this problem is n cattle, the cost of exchange between two cows is the maximum of their distance by the volume of both (that is, Max (V (i), V (j))), and then the total cost of all cattle 22 exchange. The first thing to think of is O (N2) of the violence enumerated, then I can only think of such a complexity, it is very curious how to catch up with the tree array edge? Then after looking at the other people's solution to marvel at its ideas.

In the blog http://www.cnblogs.com/Fatedayt/archive/2011/10/08/2202439.html very detailed, attached its main ideas:

has been described in detail, that is, the statistics of a cow I, by sequencing and preprocessing can be the original O (n) query down to O (Logn), is really very powerful, it is difficult to think. I combined with the other people on the Internet half copy half of the following code rewrite:

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5typedefLong LongLL;6 Const intmaxn=20020;7 8inline ll Lowbit (ll x) {returnx& (-x); }9 Ten structtreearray{ One LL C[MAXN], N; ATreearray (LL n=0): N (n) {memset (c,0,sizeof(c)); } - ll sum (ll x) { -LL ans=0; the          while(x) { -ans+=C[x]; -x-=lowbit (x); -         } +         returnans; -     } +     voidAdd (ll x, ll D) { A          while(x<=N) { atc[x]+=D; -x+=lowbit (x); -         } -     } -} Count (20003), Dist (20003); -  in structcow{ - LL v,x; to     BOOL operator< (ConstCow C2)Const { +         returnv<c2.v; -     } the } COW[MAXN]; *  $ intMain () {Panax Notoginseng     intn,i; -scanf"%d",&n); the      for(i=1; i<=n; ++i) +scanf"%lld%lld",&cow[i].v,&cow[i].x); ASort (cow+1, cow+n+1); theLL ans=0, alldist=0; +      for(i=1; i<=n; ++i) { -LL x =cow[i].x; $LL num =count.sum (x); $LL lessdist =dist.sum (x); -Ans + = cow[i].v* (num*x-lessdist+alldist-lessdist-(i-1-num) *x); -Count.add (x,1); the Dist.add (x,x); -Alldist + =x;Wuyi     } theprintf"%lld\n", ans); -     return 0; Wu}

Among them, Count.sum (x) indicates the number of cattle with a small amount of x (in the middle is a), Dist.sum (x) indicates the sum of the positions of the cattle (in B), which is smaller than x, and alldist is the total distance of the former I cattle (which can be considered as the standard for the origin), and the remaining variable names are After the code to feel the tree array is really too powerful, have to praise!

POJ 1990 Moofest 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.