codeforces Cloud Computing

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

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

  • l i l_i li and r i r_i ri — the i i i-th tariff plan is available only on days from l i l_i li to r i r_i ri, inclusive,
  • c i c_i ci — the number of cores per day available for rent on the i i i-th tariff plan,
  • p i p_i pi — the price of renting one core per day on the i i 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 ci) 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 n days from 1 1 1 to n n n. If on a day the total number of cores for all available tariff plans is strictly less than k 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 k cores this day.

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1000006;
const int N = 1000000;

struct Seg{
    long long cnt, sum;
} seg[MAXN*4];

void update(int o, int l, int r, int P, int C){
    if (r<P || P<l) return;
    if (l==r){
        seg[o].cnt += C;
        seg[o].sum += (long long)r*C;
        cout << o << " " << l << " " << r << " " << P << " " << C << endl;
        cout << seg[o].cnt << " " << seg[o].sum << endl;
        return;
    }
    int mid = (l+r)>>1;
    update(o<<1,l,mid,P,C);
    update(o<<1|1,mid+1,r,P,C);
    seg[o].cnt = seg[o<<1].cnt + seg[o<<1|1].cnt;
    seg[o].sum = seg[o<<1].sum + seg[o<<1|1].sum;
}

long long query(int o, int l, int r, int K){
    if ( seg[o].cnt <= K ) return seg[o].sum;
    if ( l==r ) return seg[o].sum / seg[o].cnt * K;
    int mid = (l+r)>>1;
    long long ret = query(o<<1,l,mid,K);
    if ( seg[o<<1].cnt < K ) ret += query(o<<1|1,mid+1,r,K-seg[o<<1].cnt);
    return ret;
}

int n, k, m;
vector< pair<int,int> > Events[MAXN];

void init(){
    cin >> n >> k >> m;
    for (int i=0; i<m; i++){
        int l, r, c, p;
        cin >> l >> r >> c >> p;
        Events[l].push_back( {p,c} );
        Events[r+1].push_back( {p,-c} );
    }
}

void solve(){
    long long ans = 0;
    for (int i=0; i<=n; i++){
        for (auto &e:Events[i]){
            int p = e.first;
            int c = e.second;
            update(1,0,N,p,c);
        }
        long long b;
        b = query(1,0,N,k);
        ans += b;
    }
    cout << ans << endl;
}

int main(){
    cin.tie(0); cin.sync_with_stdio(0);
    init();
    solve();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值