Codeforces-Round-422#C-Hacker, pack your bags!
Hacker, pack your bags!
time limit per test 2 second memory limit per test 256 megabytes
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!
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.
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.
那么这道题我是这样做的 区间最多不超过2e5 那么弄一个2e5的数组 每个数组i存长度为i的区间
然后开始扫一遍 遇到一个区间 l r cost 我们去数组r-l+1中找符合条件的区间的最小值
因为是一对一对的 我们可以默认只找在它前面不重合的区间 那么二分找一下小于l的最大的右端点