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!

传送门:CF822C

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.

Sample Input

1
2
3
4
5
4 5
1 3 4
1 2 5
5 6 1
1 2 4

Sample Output

1
5

题解

比赛时都想到做法且A了..赛后因为在查询的时候做前缀T了..
以后一定先预处理= =
那么这道题我是这样做的 区间最多不超过2e5 那么弄一个2e5的数组 每个数组i存长度为i的区间
然后按右端点排序 再做一个前缀最小值(后面要用到)
然后开始扫一遍 遇到一个区间 l r cost 我们去数组r-l+1中找符合条件的区间的最小值
因为是一对一对的 我们可以默认只找在它前面不重合的区间 那么二分找一下小于l的最大的右端点
然后cost加上前缀处理的那个点的最小值就是当前区间作为后一个区间能够达到两个区间和的最小值

AC code:(不包含输入类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.io.*;  
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
while (sc.hasNext()) {
int n=sc.nextInt();
int x=sc.nextInt();
Node[]node=new Node[n];
ArrayList<Node>[]list=new ArrayList[220000]; //存长度为i的区间
ArrayList<Integer>[]min=new ArrayList[220000]; //前缀最小
for(int i=0;i<220000;i++){
list[i]=new ArrayList<Node>();
min[i]=new ArrayList<Integer>();
}
for(int i=0;i<n;i++){
int l=sc.nextInt();
int r=sc.nextInt();
int c=sc.nextInt();
node[i]=new Node(l,r,c);
list[r-l+1].add(node[i]);
}
long result=Long.MAX_VALUE;
for(int i=0;i<220000;i++)Collections.sort(list[i]);
for(int i=0;i<220000;i++){
if(list[i].size()==0)continue;
int ll=list[i].size();
min[i].add(list[i].get(0).cost);
for(int k=1;k<ll;k++){
min[i].add(Math.min(min[i].get(k-1), list[i].get(k).cost));
}
}
boolean flag=false;
for(int i=0;i<n;i++){
int l=node[i].r-node[i].l+1;
long v1=node[i].cost;
if(x-l<0)continue;
if(x-l>=0&&list[x-l].size()==0)continue;
//int[]temp=new int[list[x-l].size()];
int ll=list[x-l].size();
int start=0;
int end=ll-1;
int res=-1;
while(start<=end){
int mid=(start+end)/2;
if(list[x-l].get(mid).r<node[i].l){
res=mid;
start=mid+1;
}else{
end=mid-1;
}
}
if(res!=-1){
flag=true;
result=Math.min(result, min[x-l].get(res)+node[i].cost);
}
}
if(!flag)pw.println(-1);
else pw.println(result);
pw.flush();
}
}
}
class Node implements Comparable<Node>{
int l;
int r;
int cost;
Node(int a,int b,int c){
l=a;
r=b;
cost=c;
}
@Override
public int compareTo(Node o) {
if(this.r<o.r)return -1;
if(this.r>o.r)return 1;
if(this.l<o.l)return -1;
if(this.l>o.l)return 1;
return 0;
}
}