CodeForces - 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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
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 > C[MAXN];//第i天加入的活动
vector > 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

 

 

你可能感兴趣的:(————ACM相关————,——数据结构——,ACM,-,线段树)