POJ1990--POJ 1990 Moofest (tree-like array)

Source: Internet
Author: User

Time Limit: 1000MS
Memory Limit: 30000K

Total Submissions: 8141
Accepted: 3674

Description

Every year, Farmer John's n (1 <= n <= 20,000) Cows Attend "moofest", a social gathering of cows from around the worl D. Moofest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of Cour SE, mooing. When the cows all stand on line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows has in fact lost a bit of their hearing.
Each cow I have an associated "hearing" threshold V (i) (in the range 1..20,000). If a cow moos to cow I, she must use a volume of at least V (i) times the distance between the both cows in order to be hear D by cow I. If the cows I and J wish to converse, they must speak at a volume level equal to the distance between them times Max (V (i), V (j)).
Suppose each of the N cows are standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), a nd every pair of cows is carrying on a conversation using the smallest possible volume.
Compute the sum of the volumes produced by All N (N-1)/2 pairs of mooing cows.

Input

* Line 1: A single integer, N
* Lines 2..n+1:two integers:the volume threshold and x coordinate for a cow. Line 2 represents the first cow; Line 3 represents the second cow; And so on. No. Cows'll stand at the same.

Output

* Line 1: A single line with a single integer this is the sum of the volumes of the conversing cows.

Sample Input

43 12 52) 64 3

Sample Output

57

Source

Usaco 2004 U S Open


Test instructions

Given a sequence, the cost of the sequence between two points is the distance between two points * (value between two points), and the sum of all N (N-1)/2 point pairs is calculated

Ideas:

For a point, its rank can be used as a multiplier, and it is the same as the two sides of it that are smaller than its rank .

Set a point, the coordinate is Xc, its rank is Val, its left is smaller than it has a N1, the right is smaller than it has N2, the left is smaller than its coordinates of XI (1<=I<=N1), the right is smaller than its coordinates for Xj (1<=j <=N2)

It is deduced that we can get its contribution to the answer as:

(N1-N2) * Xc * val + val (segma (Xi) –segma (Xj))

So we need to count two types of values: the number of points on either side of the left or right, which is smaller than its rank, and the coordinates of the points that are smaller than its rank.

These two types of values need to be calculated separately, but as long as you know one of them can get another (know the left side can be pushed to export to the right)

We use a tree-like array to count these values, as follows:

Sorting all points by rank, the current label for a point is the total number of all points (including left and right) that are smaller than it.

We use two tree-like arrays, one at a time from left to right, and a tree-like array to count the number of points to the left that are smaller than its rank, the coordinates of another statistic point and

The first tree array is good to understand, the main difficulty is to construct a second tree-like array

Only need every update (X[i], x[i])

In this case, X[i] getsum (X[i]) is the coordinates of the points on the left that are smaller than the I point level and

Code:

1 /*  2 * @FileName: D:\ code and algorithm \2017 training game \ July Training four \1013.cpp 3 * @Author: Pic 4 * @Date: 2017-08 -04 21:31:01 5 * @Last Modified time:2017-08-05 20:21:10 6 * *7 8#include <iostream>9#include <algorithm>Ten#include <cstdio> One#include <string.h> A#include <queue> - using namespaceStd - typedef__int64 ll; the Const intmaxn=20000+30; - //Note that the vol of this problem appears in the same way. In this case, you cannot first sort by x-coordinate, statistics about the number of smaller than this point, and then according to the Vol sort statistics about the dot smaller than the vol and - the two volumes should be counted synchronously to prevent the same situation - structbit{ + voidInit () -{ +memset (tree_sum,0,sizeof(Tree_sum)); A} atll TREE_SUM[MAXN];//Stores An array of tree-like arrays - //int maxn=20000+30; Subscript maximum of a tree array -ll Lowbit (ll x)//lowbit function, find x with the nearest last one consecutive 0 more than he number * distance -{ -     returnx& (-X); -} inll Getsum (ll x)//Get 0 to x interval and -{ toll Sum=0; +      for(; x>0;x-=lowbit (x)) { -SUM+=TREE_SUM[X]; the} *     returnSum $}Panax Notoginseng voidUpdate (ll X,ll v)//Update the value of a point -{ the      for(; x<=maxn;x+=lowbit (x)) { +Tree_sum[x]+=v; A} the} +}; -BIT tr,tr2; $ structNode ${ -ll Vol,x,id; -}A[MAXN]; thell N1[MAXN]; - BOOLCMP (node A,node b)Wuyi{ the     returna.vol<b.vol; -} Wu BOOLCMP1 (node A,node b) -{ About returna.x<b.x; $} - intMain () { -     //freopen ("data.in", "R", stdin); -ll N; A  while(~SCANF ("%i64d", &n)) { +  for(inti=0;i<n;i++) { the //scanf ("%d%d", &a[i].vol,&a[i].x); - //cin>>a[i].vol>>a[i].x; $scanf ("%i64d%i64d", &a[i].vol,&a[i].x); theA[i].id=i; the} theTr.init (); theTr2.init (); -Sort (A,A+N,CMP1); inSort (a,a+n,cmp); thell Res=0,sum=0,sumn=0; the          for(inti=0;i<n;i++) { AboutSumn=tr.getsum (a[i].x); thell Suma=tr2.getsum (a[i].x); theres+= ((Sum-suma-suma) *a[i].vol); theres+= ((2*sumn-i) *a[i].vol*a[i].x); +             //cout<<res<<endl; -Tr2.update (a[i].x,a[i].x); theTr.update (a[i].x,1);Bayisum+=a[i].x; the} the         //printf ("%lld\n", res); -         //cout<<res<<endl; -printf ("%i64d\n", RES); the} the     return0; the}
View Code

POJ1990--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.