2018-2019 ICPC, NEERC, Southern Subregional Contest C题 线段树

题目描述:

Description

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.

Sample Input

Input

5 7 3
1 4 5 3
1 3 5 2
2 5 10 1

Output

44

Input

7 13 5
2 3 10 7
3 5 10 10
1 2 10 6
4 5 10 9
3 4 10 8

Output

462

Input

4 100 3
3 3 2 5
1 1 3 2
2 4 4 4

Output

64

唉,本来想先按价格排序再用线段树维护的,结果写着写着就崩盘了,后来看了学长的代码才知道怎么写:以价格为值域建立线段树,遍历1到n,当一个区间开始时,加上对应的值和芯片个数,当这个区间结束了删除,再二分查一下就行了。


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+10;
const int M = 1e6;
typedef long long ll;
typedef pair<int,int> P;
vector<pair<int,int> > add[maxn];
vector<pair<int,int> > sub[maxn];
#define lson id<<1,l,mid
#define rson id<<1|1,mid+1,r
int n,m,k;
int l,r,c,p;
ll sum[maxn<<2],sum2[maxn<<2];//sum表示个数和,sum2表示价格和 
void push_up(int id){
	sum[id] = sum[id<<1]+sum[id<<1|1];
	sum2[id] = sum2[id<<1]+sum2[id<<1|1];
}
void update(int id,int l,int r,int pos,int v){
	//printf("id = %d l = %d r= %d\n",id,l,r);
	if(l==r){
		sum[id]+=1ll*v;sum2[id]+=1ll*l*v;
		return;	
	}
	int mid = l+r>>1;
	if(pos<=mid)update(lson,pos,v);
	else update(rson,pos,v);
	push_up(id); 
}
ll query(int id,int l,int r,int k){
	if(l == r) return min(sum[id],1LL*k)*l;
	int mid = l+r>>1;
	if(sum[id<<1]>=k) return query(lson,k);
	else return query(rson,k-sum[id<<1])+sum2[id<<1];
}
int main(){
	scanf("%d%d%d",&n,&k,&m);
	for(int i = 1; i <= m; i++){
		scanf("%d%d%d%d",&l,&r,&c,&p);
		add[l].push_back(P(c,p));
		sub[r].push_back(P(c,p));
	}
	ll ans = 0;
	for(int i = 1; i <= n; i++){
		for(auto v:add[i])
			update(1,1,M,v.second,v.first);
		if(sum[1]<=k) ans+=sum2[1];
		else ans+=query(1,1,M,k);
		for(auto v:sub[i]) update(1,1,M,v.second,-v.first);
	}printf("%lld\n",ans);
	return 0;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值