1. 程式人生 > >【Codeforces Round #523(Div. 2)】TV Shows(貪心+map)

【Codeforces Round #523(Div. 2)】TV Shows(貪心+map)

題目連結

D. TV Shows

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

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

Copy

5 4 3
1 2
4 10
2 4
10 11
5 9

output

Copy

60

input

Copy

6 3 2
8 20
6 22
4 15
20 28
17 25
20 27

output

Copy

142

input

Copy

2 1000000000 2
1 2
2 3

output

Copy

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.

 

【題意】

有n部片子,給出每部片子的起始時間a和結束時間b,一部片子只能在一部TV上看,並且只有在一部片子結束之後才能在這個TV上放另一部片子,已知租用TV的費用為x,每分鐘需要增收租金y,可以同時看很多片子,問最少需要的租金。

【解題思路】

首先還是很明確的,先給這n部片子的放映時間排序。用map儲存同一結束時間的片子的數量。

用二分查詢是否存在比當前片子開始時間小一點的結束時間,如果不存在說明需要再租一臺TV。如果存在,就要分兩類情況,第一類是(當前片子的開始時間-之前的片子的結束時間)*y<=租金,說明可以用之前的TV繼續看,反之需要再租一臺TV。

【程式碼】

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
const LL mod=1e9+7;
struct Node
{
    LL l,r;
}node[maxn];
bool cmp(Node a,Node b)
{
    return (a.l!=b.l)?a.l<b.l:a.r<b.r;
}
int main()
{
    LL n,x,y,ans=0;
    map<LL,int>mp;
    map<LL,int>::iterator it;
    scanf("%lld%lld%lld",&n,&x,&y);
    for(int i=0;i<n;i++)
        scanf("%lld%lld",&node[i].l,&node[i].r);
    sort(node,node+n,cmp);
    mp[node[0].r]++;
    ans=(ans+x+y*(node[0].r-node[0].l)%mod)%mod;
    for(int i=1;i<n;i++)
    {
        it=mp.lower_bound(node[i].l);
        if(it==mp.begin())
        {
            ans=(ans+x+y*(node[i].r-node[i].l)%mod)%mod;
            mp[node[i].r]++;
        }
        else
        {
            it--;
            LL t=it->first;
            if(node[i].l>t && (node[i].l-t)*y<=x)
            {
                ans=(ans+y*(node[i].r-t)%mod)%mod;
                mp[t]--;
                if(mp[t]==0)mp.erase(it);
                mp[node[i].r]++;
            }
            else
            {
                ans=(ans+x+y*(node[i].r-node[i].l)%mod)%mod;
                mp[node[i].r]++;
            }
        }
    }
    printf("%lld\n",ans%mod);
}