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 kk CPU 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 1output
Copy
44input
Copy
7 13 5 2 3 10 7 3 5 10 10 1 2 10 6 4 5 10 9 3 4 10 8output
Copy
462input
Copy
4 100 3 3 3 2 5 1 1 3 2 2 4 4 4output
Copy
64
題目大意:給出m個計劃,每個計劃都有作用的區間l-r,能提供的核心數量c和單個核心的價格p,求n天,每天買k個需要的最少花費,如果這一天所有計劃加起來能買的不足k個,就將這些全買了。
解題思路:
1.價格區間為1-1e6,所以建立一個價格線段樹。維護每個節點對應的區間所能買的數量,和將這些 全買了花費的金錢 2. 對於每個計劃,掃描線的思想,第l天加入,r+1天退出,就將一個計劃變成兩個點,第l天加入一個可 購買c個價格為p的計劃,第r+1天加入一個數量為-c價格為p的計劃,對價格線段樹進行單點修改。 3.這樣維護,計算的結果一定是最小的,對於每天要買k個,對線段樹進行一次查詢,一個節點有兩個節點, 因為是價格線段樹,左邊的價格比右邊小,左邊能買先買左節點的,然後再從右節點購買。計算出n天的答案。
#include <bits/stdc++.h>
using namespace std ;
typedef long long ll ;
const ll Maxn = 1e6 + 10 ;
const ll Max = 2e5 + 10 ;
vector < int > st[Maxn], ed[Maxn] ;
#define lson ri << 1, l, Mid
#define rson ri << 1 | 1 , Mid + 1, r
struct Node{
int l ,r ;
ll ci, pi ;
}no[Max];
ll val[Maxn << 2] , s[Maxn << 2] ;
void Pushup(int ri){
val[ri] = val[ri << 1] + val[ri << 1 | 1] ;
s[ri] = s[ri << 1] + s[ri << 1 | 1] ;
}
void Update (int ri, int l, int r, ll pos, ll v){
if (l == r){
val[ri] += v * pos ;
s[ri] += v ;
return ;
}
int Mid = (l + r) >> 1 ;
if (pos <= Mid) Update(lson, pos, v) ;
else Update(rson, pos, v) ;
Pushup(ri) ;
}
ll Query(int ri, int l, int r, int k){
if (l == r) return min(1ll * k, s[ri]) * l ;
int Mid = (l + r) >> 1 ;
if(k <= s[ri << 1]){
return Query(lson, k) ;
}
return val[ri << 1] + Query(rson, k - s[ri << 1]) ;
}
int main (){
int n, m, k ;
cin >> n >> k >> m ;
for (int i = 1 ;i <= m; i++){
cin >> no[i].l >> no[i].r >> no[i].ci >> no[i].pi ;
st[no[i].l].push_back(i) ;
ed[no[i].r].push_back(i) ;
}
ll ans = 0 ;
for (int i = 1; i <= n; i++){
for (int j = 0; j < st[i].size(); j++){
int e = st[i][j] ;
Update(1, 1, Maxn, no[e].pi, no[e].ci ) ;
}
ans += Query(1, 1, Maxn, k) ;
for (int j = 0; j < ed[i].size(); j++){
int e = ed[i][j] ;
Update (1, 1, Maxn, no[e].pi, -no[e].ci) ;
}
}
cout << ans << endl ;
return 0 ;
}