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 consecutive days, which are numbered from 1 to n. Buber requires k CPU cores each day.
The cloud provider offers m tariff plans, the i-th tariff plan is characterized by the following parameters:
li and ri — the i-th tariff plan is available only on days from li to ri, inclusive,
ci — the number of cores per day available for rent on the i-th tariff plan,
pi — the price of renting one core per day on the 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 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 days from 1 to n. If on a day the total number of cores for all available tariff plans is strictly less than k, then this day Buber will have to work on fewer cores (and it rents all the available cores), otherwise Buber rents exactly k cores this day.
Input
The first line of the input contains three integers n, k and m (1≤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 m lines contain descriptions of tariff plans, one description per line. Each line contains four integers li, ri, ci, pi (1≤li≤ri≤n, 1≤ci,pi≤106), where li and ri are starting and finishing days of the i-th tariff plan, ci — number of cores, pi — price of a single core for daily rent on the i-th tariff plan.
Output
Print a single integer number — the minimal amount of money that Buber will pay.
Examples
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
有一个活动总共n天,总共有m个物品,每种物品从li到ri供应,共c件,每一件价格p。每天需要k种物品,要是不足k个物品就凑活着用。问最小花费是多少。
我们把按着价值在权值线段树里,当某个物品供应时,就直接插入权值线段树,物品不供应时,就在线段树里删除这个物品。
代码如下:
#include
#define ll long long
using namespace std;
const int maxx=1e6+100;
vector > in[maxx],out[maxx];
struct node{
int l;
int r;
ll sum;
ll num;
}p[maxx<<2];
int n,m,k;
void init()
{
for(int i=0;i<=n;i++)
{
in[i].clear();
out[i].clear();
}
}
inline void pushup(int cur)
{
p[cur].num=p[cur<<1].num+p[cur<<1|1].num;
p[cur].sum=p[cur<<1].sum+p[cur<<1|1].sum;
}
inline void build(int l,int r,int cur)
{
p[cur].l=l;
p[cur].r=r;
p[cur].num=0;
p[cur].sum=0;
if(l==r) return ;
int mid=l+r>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
}
inline void update(int num,int pos,int cur)
{
int L=p[cur].l;
int R=p[cur].r;
if(L==R)
{
p[cur].num+=num;
p[cur].sum+=(ll)num*(ll)L;
return ;
}
int mid=L+R>>1;
if(pos<=mid) update(num,pos,cur<<1);
else update(num,pos,cur<<1|1);
pushup(cur);
}
inline ll query(int num,int cur)
{
int L=p[cur].l;
int R=p[cur].r;
if(L==R) return (ll)L*min(p[cur].num,(ll)num);
if(num<=p[cur<<1].num) return query(num,cur<<1);
else return (p[cur<<1].sum+query(num-p[cur<<1].num,cur<<1|1));
}
int main()
{
int l,r,x,y;
while(~scanf("%d%d%d",&n,&k,&m))
{
init();
for(int i=1;i<=m;i++)
{
scanf("%d%d%d%d",&l,&r,&x,&y);
in[l].push_back(make_pair(x,y));
out[r].push_back(make_pair(x,y));
}
ll ans=0;
build(1,maxx,1);
for(int i=1;i<=n;i++)
{
for(int j=0;j
努力加油a啊,(o)/~