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 consecutive days, which are numbered from to . Buber requires CPU cores each day.
The cloud provider offers tariff plans, the -th tariff plan is characterized by the following parameters:
- and — the -th tariff plan is available only on days from to , inclusive,
- — the number of cores per day available for rent on the -th tariff plan,
- — the price of renting one core per day on the -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 ) 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 days from to . If on a day the total number of cores for all available tariff plans is strictly less than , then this day Buber will have to work on fewer cores (and it rents all the available cores), otherwise Buber rents exactly 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();
}