#C11926. Taxi Fare Calculation Service
Taxi Fare Calculation Service
Taxi Fare Calculation Service
You are tasked with designing a taxi service system which processes a series of events. There are two types of events:
- Fare Update Event: Format:
1 x1 y1 x2 y2 f
. This event indicates that a fare of f is added to every road segment on the straight path from (x1, y1) to (x2, y2). If the road is oriented vertically (i.e.x1 == x2
), the update applies to each segment between the two endpoints vertically. If the road is horizontal (y1 == y2
), the update applies horizontally. For each segment the update is applied in both directions. - Fare Query Event: Format:
2 x1 y1 x2 y2
. This event represents a taxi ride request from (x1, y1) to (x2, y2). The fare is calculated using a simple greedy strategy: at each step, move one unit closer to the destination. The total fare is the sum of the individual road segment fares along the path. Mathematically, if the path consists of segments with fare values \(f_1, f_2, \dots, f_n\), then the total fare is given by \[ \text{Total Fare} = \sum_{i=1}^{n} f_i. \]
Your program should process a sequence of these events and output the fare for each taxi ride request.
inputFormat
The first line contains an integer q
, representing the number of events. The following q
lines each contain an event in one of the two formats:
1 x1 y1 x2 y2 f
: Update fare event.2 x1 y1 x2 y2
: Taxi ride query event.
All values are integers and are separated by spaces.
outputFormat
For each taxi ride query event, output a single line containing an integer, the calculated fare for that ride.
## sample6
1 0 0 1 0 5
1 1 0 1 1 3
2 0 0 1 1
1 0 0 0 1 2
2 0 0 0 1
2 1 0 1 1
8
2
3
</p>