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

>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 > Events[MAXN]; void init(){ cin >> n >> k >> m; for (int i=0; i> 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(); }

你可能感兴趣的:(线段树,差分)