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 <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;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值