1. 程式人生 > >codeforces Cloud Computing

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

consecutive days, which are numbered from 1 1 to n n . Buber requires k
k
CPU cores each day.

The cloud provider offers m m tariff plans, the i i

-th tariff plan is characterized by the following parameters:

  • l i l_i and r i r_i — the i 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 i -th tariff plan,
  • p i p_i — the price of renting one core per day on the 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 ) 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.

#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();
}