C. Cloud Computing
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Buber is a Berland technology company that specializes in waste of investor's money. Recently Buber decided to transfer its infrastructure to a cloud. The company decided to rent CPU cores in the cloud for nn consecutive days, which are numbered from 11 to nn. Buber requires kkCPU cores each day.
The cloud provider offers mm tariff plans, the ii-th tariff plan is characterized by the following parameters:
Buber can arbitrarily share its computing core needs between the tariff plans. Every day Buber can rent an arbitrary number of cores (from 0 to cici) on each of the available plans. The number of rented cores on a tariff plan can vary arbitrarily from day to day.
Find the minimum amount of money that Buber will pay for its work for nn days from 11 to nn. If on a day the total number of cores for all available tariff plans is strictly less than kk, then this day Buber will have to work on fewer cores (and it rents all the available cores), otherwise Buber rents exactly kk cores this day.
Input
The first line of the input contains three integers nn, kk and mm (1≤n,k≤106,1≤m≤2⋅1051≤n,k≤106,1≤m≤2⋅105) — the number of days to analyze, the desired daily number of cores, the number of tariff plans.
The following mm lines contain descriptions of tariff plans, one description per line. Each line contains four integers lili, riri, cici, pipi (1≤li≤ri≤n1≤li≤ri≤n, 1≤ci,pi≤1061≤ci,pi≤106), where lili and riri are starting and finishing days of the ii-th tariff plan, cici — number of cores, pipi — price of a single core for daily rent on the ii-th tariff plan.
Output
Print a single integer number — the minimal amount of money that Buber will pay.
Examples
input
Copy
5 7 3 1 4 5 3 1 3 5 2 2 5 10 1
output
Copy
44
input
Copy
7 13 5 2 3 10 7 3 5 10 10 1 2 10 6 4 5 10 9 3 4 10 8
output
Copy
462
input
Copy
4 100 3 3 3 2 5 1 1 3 2 2 4 4 4
output
Copy
64
题意:有很多个活动,每个活动有持续天数,每个活动会在每天提供C个CPU每个CPU价格为P,问需要工作N天,每天需要K个CPU的最少花费。
解题思路:首先价格越低的活动,肯定是要选的。因此我们对于每一天记录有哪些新活动 加入,哪些活动结束。然后维护一个线段树,线段树的下标是价格。即价格为i的活动,一共能提供多少个CPU,然后加入和删除活动就相当于update(C,+-P,1,MAXC,1)。 然后我们顺便维护一下价格*数量的和。然后利用线段树天然的二分性,快速求出前缀数量和为K的价格和。
#include
#include
#include
#include
#include
#include
#include