-
Notifications
You must be signed in to change notification settings - Fork 0
/
AirConditioner.java
61 lines (54 loc) · 1.49 KB
/
AirConditioner.java
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
/**
* https://codeforces.com/problemset/problem/1304/C #implementation #two-pointer find the range can
* change: ownRange.l -= gap; ownRange.r += gap;
*
* <p>=> find common segment between ownRange and target range.
*/
import java.util.Scanner;
public class AirConditioner {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int q = sc.nextInt();
for (int test = 0; test < q; test++) {
int n = sc.nextInt();
int m = sc.nextInt();
Range ownRange = new Range(m, m);
int preMinute = 0;
int t, l, r;
boolean possible = true;
int gap = 0;
for (int i = 0; i < n; i++) {
t = sc.nextInt();
l = sc.nextInt();
r = sc.nextInt();
if (!possible) continue;
gap = t - preMinute;
if (gap != 0) {
preMinute = t;
ownRange.l -= gap;
ownRange.r += gap;
}
// find comment segment between ownRange and [l, r];
if (ownRange.l > r || ownRange.r < l) {
possible = false;
} else if (ownRange.l <= l && ownRange.r >= r) {
ownRange.l = l;
ownRange.r = r;
} else if (l < ownRange.l && ownRange.r < r) {
continue;
} else {
ownRange.l = Math.max(ownRange.l, l);
ownRange.r = Math.min(ownRange.r, r);
}
}
System.out.println(possible ? "YES" : "NO");
}
}
}
class Range {
int l, r;
Range(int l, int r) {
this.l = l;
this.r = r;
}
}