Poj 1990 MooFest (tree array code)
MooFest
Time Limit:1000 MS |
|
Memory Limit:30000 K |
Total Submissions:6265 |
|
Accepted:2765 |
Description
Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest", a social gathering of cows from around the world. mooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. when the cows all stand in line for a special event, they moo so loudly that the roar is practically deafening. after participating in this event year after year, some of the cows have in fact lost a bit of their hearing.
Each cow I has 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 two cows in order to be heard by cow I. if two 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 is standing in a straight line (each cow at some unique x coordinate in the range 1 .. 20,000), and every pair of cows is carrying on a conversation using the smallest possible volume.
Compute the sum of all 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 two cows will stand at the same location.
Output
* Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows.
Sample Input
43 12 52 64 3
Sample Output
57
Source
USACO 2004 u s Open
A group of cows have different degrees of hearing loss after the festival. When I heard others' speeches, the volume of others must be higher than v [I], when two cattle I and j communicate, the minimum communication sound is max {v [I], v [j]} * the distance between them. Now there are nheaded cows, please ask them to have the least volume and communication between them.
Solution: sort the volume f first, so that you can optimize it. That is, the volume at the top of a bull I is lower than that at the top of him, it must be I's own volume * the distance between the two. The calculation formula is as follows: (solving all sums smaller than the current x)
(X-x1) * f;
(X-x2) * f;
(X-x3) * f ........
In summary: (n * x (x1 + x2 + x3 + .....)) * f; Note: x indicates the current position of a cow, x1, x2, x3 ...... it indicates the place in front of him. f indicates the volume of the Current Cow because it is the maximum volume. (We need to use a tree array to solve the distance here)
Here, we need to explain that the volume in front of this ox I must be smaller than him, but the coordinate value x is not necessarily smaller than him, so we can only process it separately.
We can create a number axis and place it at a certain point. The volume of the first point must be small, so we can place it at a fixed position, in this way, if all the x above him is smaller than him, he can use the tree array method to sum the range. (Here we need to put a solution to the left and right values. The left is smaller than the current x, and the right is bigger than the current x ).
The calculation method is as follows:
(X1-x) * f;
(X2-x) * f;
(X3-x) * f ........
In summary: (x1 + x2 + x3 +...)-n * x) * f;
The n value here should be equal to the number of all cows that are put in. The previously calculated number is smaller than x: I-nn;
(X1 + x2 + x3 +...) is equal to the sum minus the sum of x smaller than him.
Use long !!!
This is complex. For more information, see code implementation ~~~~ Comments without understanding
For details, see the code.
# Include
# Include
# Include
# Include using namespace std; # define ll long longstruct node {ll f, x;} s [20010]; int n; ll c1 [20010], c2 [20010]; ll a1 [20010], a2 [20010]; bool cmp (node a, node B) {return. f