output
standard output
There are nn TV shows you want to watch. Suppose the whole time is split into equal parts called "minutes". The ii-th of the shows is going from lili-th to riri-th minute, both ends inclusive.
You need a TV to watch a TV show and you can't watch two TV shows which air at the same time on the same TV, so it is possible you will need multiple TVs in some minutes. For example, if segments [li,ri][li,ri] and [lj,rj][lj,rj] intersect, then shows ii and jj can't be watched simultaneously on one TV.
Once you start watching a show on some TV it is not possible to "move" it to another TV (since it would be too distracting), or to watch another show on the same TV until this show ends.
There is a TV Rental shop near you. It rents a TV for xx rupees, and charges yy (y<xy<x) rupees for every extra minute you keep the TV. So in order to rent a TV for minutes [a;b][a;b] you will need to pay x+y⋅(b−a)x+y⋅(b−a).
You can assume, that taking and returning of the TV doesn't take any time and doesn't distract from watching other TV shows. Find the minimum possible cost to view all shows. Since this value could be too large, print it modulo 109+7109+7.
Input
The first line contains integers nn, xx and yy (1≤n≤1051≤n≤105, 1≤y<x≤1091≤y<x≤109) — the number of TV shows, the cost to rent a TV for the first minute and the cost to rent a TV for every subsequent minute.
Each of the next nn lines contains two integers lili and riri (1≤li≤ri≤1091≤li≤ri≤109) denoting the start and the end minute of the ii-th TV show.
Output
Print exactly one integer — the minimum cost to view all the shows taken modulo 109+7109+7.
Examples
input
5 4 3 1 2 4 10 2 4 10 11 5 9
output
60
input
6 3 2 8 20 6 22 4 15 20 28 17 25 20 27
output
142
input
2 1000000000 2 1 2 2 3
output
999999997
Note
In the first example, the optimal strategy would be to rent 33 TVs to watch:
- Show [1,2][1,2] on the first TV,
- Show [4,10][4,10] on the second TV,
- Shows [2,4],[5,9],[10,11][2,4],[5,9],[10,11] on the third TV.
This way the cost for the first TV is 4+3⋅(2−1)=74+3⋅(2−1)=7, for the second is 4+3⋅(10−4)=224+3⋅(10−4)=22 and for the third is 4+3⋅(11−2)=314+3⋅(11−2)=31, which gives 6060 int total.
In the second example, it is optimal watch each show on a new TV.
In third example, it is optimal to watch both shows on a new TV. Note that the answer is to be printed modulo 109+7109+7.
思路:首先排序是肯定可以想到的,但关键是用什么来维护比较你一个节目接在另一个下面的费用小还是直接在开一台电视的费用小。大佬们又multiset,也有用set加map来维护的。我就使用set加map维护的(注意比较的时候有个二分找结束的时间用set本身的二分函数效率快很多)。用到二分查找的原因是我们记录结束的时间那么新的时间加入就要找离开始时间最近的结束时间。复杂度是nlogn
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#include<map>
#define LL long long
using namespace std;
const int maxn=1e5+100;
const int mod=1e9+7;
struct node
{
LL a,b;
friend bool operator <(node p,node q)
{
if(p.a==q.a)
{
return p.b<q.b;
}
return p.a<q.a;
}
};
node c[maxn];
set<LL>st;
map<LL,int>mp;
int main()
{
int n;
LL x,y;
scanf("%d%lld%lld",&n,&x,&y);
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&c[i].a,&c[i].b);
}
sort(c+1,c+1+n);
LL ans=0;
st.insert(c[1].b);
mp[c[1].b]++;
ans=(ans+x+(c[1].b-c[1].a)*y%mod)%mod;
set<LL>::iterator it;
for(int i=2;i<=n;i++)
{
it=st.lower_bound(c[i].a);
if(it==st.begin())
{
ans=(ans+x+(c[i].b-c[i].a)*y%mod)%mod;
mp[c[i].b]++;
}
else
{
it--;
LL value=*it;
if(c[i].a>value&&(c[i].a-value)*y<=x)
{
mp[value]--;
if(mp[value]==0)
st.erase(value);
ans=(ans+(c[i].b-value)*y%mod)%mod;
mp[c[i].b]++;
}
else
{
ans=(ans+x+(c[i].b-c[i].a)*y%mod)%mod;
mp[c[i].b]++;
}
}
st.insert(c[i].b);
}
printf("%lld\n",ans);
return 0;
}