1. 程式人生 > >1070C Cloud Computing(線段樹二分)

1070C Cloud Computing(線段樹二分)

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:

  • lili and riri — the ii-th tariff plan is available only on days from lili to riri, inclusive,
  • cici — the number of cores per day available for rent on the ii-th tariff plan,
  • pipi — the price of renting one core per day on the ii-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 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 <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
#include <memory.h>
#include <bitset>
#include <map>
#include <deque>
#include <math.h>
#include <stdio.h>
using namespace std;
typedef long long int ll;
const int MAXN = 1000005;

ll num[MAXN<<2];
ll sum[MAXN<<2];
int N;
void pushup(int rt){
    num[rt]=num[rt<<1]+num[rt<<1|1];
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}

void update(int P,int C,int l,int r,int rt){
    if(l==r){
        num[rt]+=C;
        sum[rt]+=1ll*P*C;
        return;
    }

    int m=(l+r)/2;

    if(P<=m)
        update(P,C,l,m,rt<<1);
    else
        update(P,C,m+1,r,rt<<1|1);
    pushup(rt);
}

ll query(int K,int l,int r,int rt){

    if(l==r){
        //不到K個
        if(l==MAXN){
            return 0;
        }
        if(K>0)
        {
            return 1ll*K*l;
        }
        else
            return 0;
    }
    int m=(l+r)/2;
    if(num[rt<<1]>=K){
        return query(K,l,m,rt<<1);
    }
    else{
        return sum[rt<<1]+query(K-num[rt<<1],m+1,r,rt<<1|1);
    }
}

vector<pair<int,int> > C[MAXN];//第i天加入的活動
vector<pair<int,int> > O[MAXN];//第i天結束的活動

int main()
{
    int K,M;
    scanf("%d%d%d",&N,&K,&M);

    int l,r,c,p;
    for(int i=0;i<M;i++){
        scanf("%d%d%d%d",&l,&r,&c,&p);
        C[l].push_back(make_pair(p,c));//加入的活動
        O[r].push_back(make_pair(p,c));//退出的活動
    }

    ll ans=0;
    for(int i=1;i<=N;i++){
        //新活動加入
        for(int j=0;j<C[i].size();j++)
            update(C[i][j].first,C[i][j].second,1,MAXN,1);
        ans+=query(K,1,MAXN,1);
        //活動結束
        for(int j=0;j<O[i].size();j++)
            update(O[i][j].first,-O[i][j].second,1,MAXN,1);
    }
    cout<<ans<<endl;

    return 0;
}