题解:P14234 [COI 2011] 河流 / RIJEKA
GeorgeDeng · · 题解
显然,我们不管怎么样都要从
把
然后这样就做完了,时间复杂度
AC 代码:
#include <iostream>
#include <utility>
#include <algorithm>
#define int long long
#define endl '\n'
using namespace std;
int n,m;
pair<int,int> pp[300000];
int len;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i = 1;i<=n;i++){
int l,r;
cin>>l>>r;
if(l>r){
pp[++len] = make_pair(r,l);
}
}
// cout<<endl;
sort(pp+1,pp+1+len);//给线段按照左端点排序
int r = 1;
int l = 1;//左端点和右端点
int ans = 0;
while(l<=len){
int maxr = pp[l].second;
r = l;
while(r<=len){//一直往右找,直到到头或者不重叠了
// cout<<"Chk: "<<r<<' '<<maxr<<endl;
if(pp[r].first>maxr) break;
maxr = max(maxr,pp[r].second);//注意这里是取max,因为有完全重叠的情况
r++;
}
ans+=(maxr-pp[l].first)*2;
// cout<<pp[l].first<<' '<<maxr<<endl;
l = r;
}
cout<<ans+m;
return 0;
}