C. Cloud Computing (线段树)

Posted zhangbuang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C. 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 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, inclusiv

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 (1n,k106,1m21051≤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 (1lirin1≤li≤ri≤n , 1ci,pi1061≤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 1
Output
Copy
44
Input
Copy
7 13 5
2 3 10 7
3 5 10 10
1 2 10 6
4 5 10 9
3 4 10 8
Output
Copy
462
Input
Copy
4 100 3
3 3 2 5
1 1 3 2
2 4 4 4
Output
Copy
64




首先贪心的想就是无论如何都要先选便宜的,会想到先排序,
然后给的区间有点区间覆盖的意思,也就是往线段树那边去想了,然后发现对一个区间进行覆盖之后,不好从某一个点里取出最小的k个
所以就尬住了qwq
然后题解是用了扫面线的思想,到了第i天,对当前可用的plan建立线段树,那k去线段树上找前k个,这样思路瞬间就清晰了




 1 #include"bits/stdc++.h"
 2 using namespace std;
 3 #define int long long
 4 #define IO ios::sync_with_stdio(0);
 5 vector<int > add[2000000],del[2000000];
 6 
 7 struct pp
 8 {
 9    int size,v;
10 }tr[4000000];
11 
12 struct aa
13 {
14     int l,r,c,p;
15     bool operator<(const aa b)const
16     {
17         return c<b.c;
18     }
19 }edge[300000];
20 int n,m,k;
21 
22 void upd(int rt,int l,int r,int pos,int c)
23 {
24    if(l==r)
25    {
26        tr[rt].size += c;
27        tr[rt].v += pos*c;
28        return ;
29    }int mid = l+r>>1;
30    if(pos<=mid)upd(rt<<1,l,mid,pos,c); else upd(rt<<1|1,mid+1,r,pos,c);
31 
32    tr[rt].size = tr[rt<<1].size + tr[rt<<1|1].size;
33    tr[rt].v = tr[rt<<1].v + tr[rt<<1|1].v;
34 
35 }
36 
37 int que(int rt,int l,int r,int k)
38 {
39     if(l==r)
40     {
41         return min(tr[rt].size,k)*l;
42     }
43     int mid=l+r>>1;
44     if(k<=tr[rt<<1].size)return que(rt<<1,l,mid,k);
45     else return tr[rt<<1].v + que(rt<<1|1,mid+1,r,k-tr[rt<<1].size);
46 }
47 
48 signed main()
49 {
50     IO
51     cin>>n>>k>>m; int maxn=1000;
52    for(int i=1;i<=m;++i)
53    {
54        cin>>edge[i].l>>edge[i].r>>edge[i].c>>edge[i].p;
55        add[edge[i].l].push_back(i);   maxn=max(maxn,edge[i].p);
56        del[edge[i].r].push_back(i);
57    }
58   int ans=0;
59   for (int i=1;i<=n;i++)
60   {
61 
62       for(auto j:add[i])upd(1,1,maxn,edge[j].p,edge[j].c);
63 
64       ans += que(1,1,maxn,k);
65       for (auto j:del[i])upd(1,1,maxn,edge[j].p,-edge[j].c);
66 
67   }
68   cout<<ans;
69 
70 
71 
72 }

 

以上是关于C. Cloud Computing (线段树)的主要内容,如果未能解决你的问题,请参考以下文章

C. Destroying Array 并查集/线段树 Intel Code Challenge Elimination Round (Div. 1 + Div. 2, combined)(示例代

A Busiest Computing Nodes(线段树+优先队列)

A Busiest Computing Nodes(线段树+优先队列)

C. DZY Loves Fibonacci Numbers(线段树&fibonacci)

Codeforces1158 C. Permutation recovery(拓扑排序,线段树优化建图)

Codeforces718 C. Sasha and Array(线段树维护矩阵,矩阵快速幂求斐波那契数列,矩阵乘法结合律)