Traveling Merchant
Time limit10sMemory limit1024 MB
Given weekly price cycles at n towns, answer q queries for the max profit from buying and later selling during a trip from town s to town t.
- Level
Hard9 of 10
- Topics
- Segment tree, Divide and conquer, Math, Array
- Solved
- No attempts yet
Problem
There is a long east-west road with n towns along it, numbered 1 to n from west to east. All towns buy and sell the same kind of goodie. The value of a goodie changes according to a weekly schedule. A town buys and sells a goodie at its value in that town on that day. At town i, the value of a goodie changes by di every day in the first half of a week and by −di every day in the second half of a week. In other words, the value of a goodie at town i is vi on Mondays and Sundays, vi + di on Tuesdays and Saturdays, vi + 2di on Wednesdays and Fridays, and vi + 3di on Thursdays.
A merchant is making a business travel plan. His trip begins at a starting town s and ends at a destination town t, visiting each town from s to t (inclusive) exactly once. The merchant starts the trip on a Monday. It takes him exactly one day to travel between adjacent towns, and every day he travels to the next town on the path to the destination. He may buy exactly one goodie at a town along the trip and sell that goodie at a town he visits later. He can buy once and sell once. For q travel plans with different choices of starting town s and destination town t, the merchant wants to know the maximum possible profit.
Input
The first line of the input has a single integer n (2 ≤ n ≤ 105). The next n lines each have two integers. The ith line has vi (1 ≤ vi ≤ 109) and di (1 ≤ vi + 3di ≤ 109). The next line has a single integer q (1 ≤ q ≤ 105). The following q lines each give a pair of integers s and t (1 ≤ s, t ≤ n, s ≠ t), representing one travel plan from town s to town t. If s < t, the merchant travels west to east, otherwise he travels east to west.
Output
For each travel plan, output the maximum profit the merchant can make on a single line. If the merchant cannot make any profit, output 0.