# Codeforces1061d tv shows

## 线段相交问题

Posted by yjjr's blog on December 1, 2018 3 minutes to read

# 题目

[题目传送门](http://codeforces.com/contest/1061/problem/D）

# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=1e5+6;
const int mod=1e9+7;
struct node{ll a,b;}c[maxn];
inline bool operator < (node p,node q){return p.a==q.a?p.b<q.b:p.a<q.a;}
set<ll>st;
map<ll,int>mp;
int n;
ll X,Y,ans=0;
int main(){
sort(c+1,c+1+n);
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;
rep(i,2,n){
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);
}
cout<<ans<<endl;
return 0;
}