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
题目链接:http://codeforces.com/problemset/problem/1070/C?csrf_token=6ecdb891bbe05d3fcb418c51559acb0f
题目大意:给出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 ;
}