1. 程式人生 > >[codeforces1070C]Cloud Computing

[codeforces1070C]Cloud Computing

time limit per test : 3 seconds
memory limit per test : 256 megabytes

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 n
consecutive days, which are numbered from 1 to n. Buber requires k

CPU cores each day.

The cloud provider offers m
tariff plans, the i t h i-th tariff plan is characterized by the following parameters: l

i li and r i ri — the i t
h i-th
tariff plan is available only on days from l i l_i to r i r_i , inclusive,
c i c_i — the number of cores per day available for rent on the i t h i-th tariff plan,
p i p_i — the price of renting one core per day on the i t h i-th tariff plan.
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 c i c_i ) 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 n n days from 1 1 to n n . If on a day the total number of cores for all available tariff plans is strictly less than k k , then this day Buber will have to work on fewer cores (and it rents all the available cores), otherwise Buber rents exactly k k cores this day.

Input

The first line of the input contains three integers
n n , k k and m m ( 1 n , k 1 0 6 , 1 m 2 1 0 5 ) (1≤n,k≤10^6,1≤m≤2⋅10^5) — the number of days to analyze, the desired daily number of cores, the number of tariff plans.

The following m m lines contain descriptions of tariff plans, one description per line. Each line contains four integers l i , r i , c i , p i ( 1 l i r i n , 1 c i , p i 1 0 6 ) l_i, r_i, c_i, p_i (1≤l_i≤r_i≤n, 1≤c_i,p_i≤10^6) , where l i l_i and r i r_i are starting and finishing days of the i t h i-th tariff plan, c i c_i — number of cores, p i pi — price of a single core for daily rent on the i t h i-th tariff plan.
Output

Print a single integer number — the minimal amount of money that Buber will pay.

Examples
Input

5 7 3
1 4 5 3
1 3 5 2
2 5 10 1

Output

44

Input

7 13 5
2 3 10 7
3 5 10 10
1 2 10 6
4 5 10 9
3 4 10 8

Output

462

Input

4 100 3
3 3 2 5
1 1 3 2
2 4 4 4

Output

64

題意:
m m 種借用雲端計算核心的方案,第 i i 種方案由 l i l_i , r i r_i , c i c_i , p i p_i 來描述。
表示這種方案的可借用時間是從第 l i l_i 天到第 r i r_i 天,每天這種方案提供至多 c i c_i 個核心,每個核心所要的租金是 p i p_i
公司在這 n n 天內,每天如果能租到的核心個數小於 k k ,則全部租掉,否則租剛好 k k 個核心,請問過完這 n n 天,公司最少要花掉多少錢。
題解:
差分+線段樹。
對於每一個核心使用方案分成( l i l_i , c i c_i , p i p_i )和( r i + 1 r_i+1 , c i -c_i , p i p_i )兩個方案,記為對 l i l_i 有影響和對 r i + 1 r_i+1 有影響。
然後順著從 1 1 n n 掃一遍,對每個點插入所有有影響該點的方案。然後查詢整個線段樹,計算當前點的最小答案,這個只需要一個線段樹查詢即可,線段樹每個結點記錄當前單價在 L L R R 的核心的數量,還有總價格。

#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
#define pa pair<int,int>
using namespace std;
int n,ks,m;
vector<pa>oc[1000004];
struct tree{
    int l,r;
    ll cn,cm;
}tr[4000004];
void build(int k,int l,int r){
     tr[k].cn=0;tr[k].cm=0;
     tr[k].l=l;tr[k].r=r;
     if(l==r)return ;
     int mid=(l+r)>>1;
     build(k<<1,l,mid);
     build(k<<1|1,mid+1,r);
}
void add(int k,int c,int p){
     int l=tr[k].l,r=tr[k].r;
     tr[k].cn+=c