It’s well known that the best way to distract from something is to do one’s favourite thing. Job is such a thing for Leha.
So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.
At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don’t intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don’t intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.
Help Leha to choose the necessary vouchers!
Input
The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha’s vacation correspondingly.
Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.
Output
Print a single integer — a minimal amount of money that Leha will spend, or print - 1 if it’s impossible to choose two disjoint vouchers with the total duration exactly x.
Examples
input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
output
5
input
3 2
4 6 3
2 4 1
3 5 4
output
-1
Note
In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.
In the second sample the duration of each voucher is 3 therefore it’s impossible to choose two vouchers with the total duration equal to 2.
题目大意是有一个区间l,r和其对应的cost,要求选出区间l,r且区间不重复,两个区间l,r的差的和为x,如果有多个区间取最小值
AC代码:
#include<bits/stdc++.h>
using namespace std;
using LL=int64_t;
const int INF=INT_MAX;
const int maxn=2e5+5;
inline int read()
{
int x;char c;
while((c=getchar())<'0'||c>'9');
for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=(x<<3)+(x<<1)+c-'0';
return x;
}
struct Node {
int l,r,cost;
}ans[maxn],ar[maxn];
map<int,int>cnt;
bool cmp1(Node a,Node b) {return a.l<b.l;}
bool cmp2(Node a,Node b) {return a.r<b.r;}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n,x;
n=read(),x=read();
for(int i=0;i<n;i++) {
ans[i].l=read(),ans[i].r=read(),ans[i].cost=read();
ar[i]=ans[i];
}
int cost=INF;
sort(ans,ans+n,cmp1);
sort(ar,ar+n,cmp2);
for(int i=0,j=0;i<n;i++) {
for(;ar[j].r<ans[i].l;j++)
if(!cnt[ar[j].r-ar[j].l+1]||ar[j].cost<cnt[ar[j].r-ar[j].l+1])
cnt[ar[j].r-ar[j].l+1]=ar[j].cost;
if(cnt[x-(ans[i].r-ans[i].l+1)]) cost=min(cost,cnt[x-(ans[i].r-ans[i].l+1)]+ans[i].cost);
}
if(cost==INF) cout<<-1<<endl;
else cout<<cost<<endl;
return 0;
}
这里用两个结构体数组来储存,保证两个区间不重复的操作是ar[j].r < ans[i].l因为两个结构体数组分别按照l,r从小到大排序,这样写保证了cmp中的数据都是保证在ans[i].l前面的,线性扫描很快的。