#P6932. [ICPC2017 WF] Money for Nothing

[ICPC2017 WF] Money for Nothing

题目描述

In this problem you will be solving one of the most profound challenges of humans across the world since the beginning of time - how to make lots of money.

You are a middleman in the widget market. Your job is to buy widgets from widget producer companies and sell them to widget consumer companies. Each widget consumer company has an open request for one widget per day, until some end date, and a price at which it is willing to buy the widgets. On the other hand, each widget producer company has a start date at which it can start delivering widgets and a price at which it will deliver each widget.

Due to fair competition laws, you can sign a contract with only one producer company and only one consumer company. You will buy widgets from the producer company, one per day, starting on the day it can start delivering, and ending on the date specified by the consumer company. On each of those days you earn the difference between the producer's selling price and the consumer's buying price.

Your goal is to choose the consumer company and the producer company that will maximize your profits.

输入格式

The first line of input contains two integers mm and n(1m,n500000)n (1 \le m , n \le 500 000) denoting the number of producer and consumer companies in the market, respectively. It is followed by mm lines, the ii th of which contains two integers pip_{i} and di(1pi,di109),d_{i} (1 \le p_{i}, d_{i} \le 10^{9}), the price (in dollars) at which the ii th producer sells one widget and the day on which the first widget will be available from this company. Then follow nn lines, the jj th of which contains two integers qjq_{j} and ej(1qj,ej109),e_{j} (1 \le q_{j}, e_{j} \le 10^{9}), the price (in dollars) at which the jj th consumer is willing to buy widgets and the day immediately after the day on which the last widget has to be delivered to this company.

输出格式

Display the maximum total number of dollars you can earn. If there is no way to sign contracts that gives you any profit, display 00 .

题目大意

一共有 mm 个生产商和 nn 个消费商。第 ii 个生产商从第 did_i 个时刻开始销售物品,每个物品卖 pip_i 元。第 ii 个消费商从第 11 个时刻开始需要消费,他的消费欲望一直到第 ei1e_i-1 的时刻,每个物品出价 qiq_i 元。你作为中间商,只能与一间生产商和一间消费商签订合约,可以选择在生产商那里买下一些物品,也可以选择卖向消费商,你的目的就是赚中间的差价。

你想知道最多能赚到多少差价。

1m,n5×1051 \le m,n \le 5\times 10^51pi,di,qi,ei1091 \le p_i,d_i,q_i,e_i \le 10^9

翻译者:一只书虫仔

2 2
1 3
2 1
3 5
7 2

5

1 2
10 10
9 11
11 9

0

提示

Time limit: 5 s, Memory limit: 512 MB.