|
| 1 | +``` |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | +
|
| 5 | +public class Main { |
| 6 | + private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 7 | + private static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 8 | + private static List<int[]> events; |
| 9 | + private static Map<Integer, Integer> teamCount; |
| 10 | + private static int N, M; |
| 11 | + public static void main(String[] args) throws IOException { |
| 12 | + init(); |
| 13 | + int answer = solve(); |
| 14 | +
|
| 15 | + bw.write(answer + "\n"); |
| 16 | + bw.flush(); |
| 17 | + bw.close(); |
| 18 | + br.close(); |
| 19 | + } |
| 20 | +
|
| 21 | + private static void init() throws IOException { |
| 22 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 23 | + N = Integer.parseInt(st.nextToken()); |
| 24 | + M = Integer.parseInt(st.nextToken()); |
| 25 | +
|
| 26 | + events = new ArrayList<>(); |
| 27 | +
|
| 28 | + for (int i = 0; i < N; i++) { |
| 29 | + st = new StringTokenizer(br.readLine()); |
| 30 | + int l = Integer.parseInt(st.nextToken()); |
| 31 | + int r = Integer.parseInt(st.nextToken()); |
| 32 | + int team = Integer.parseInt(st.nextToken()); |
| 33 | +
|
| 34 | + int startS = Math.max(0, l-M); |
| 35 | + int endS = r; |
| 36 | +
|
| 37 | + events.add(new int[]{startS, 0, team}); |
| 38 | + events.add(new int[]{endS, 1, team}); |
| 39 | + } |
| 40 | +
|
| 41 | + Collections.sort(events, (o1, o2) -> Integer.compare(o1[0], o2[0])); |
| 42 | +
|
| 43 | + teamCount = new HashMap<>(); |
| 44 | + } |
| 45 | +
|
| 46 | + private static int solve() { |
| 47 | + int answer = 0; |
| 48 | + int prevS = -1; |
| 49 | + int validTeams = 0; |
| 50 | +
|
| 51 | + for (int[] element : events) { |
| 52 | + if (element[0] != prevS && prevS >= 0) { |
| 53 | + answer = Math.max(answer, validTeams); |
| 54 | + } |
| 55 | +
|
| 56 | + int team = element[2]; |
| 57 | + int oldCount = teamCount.getOrDefault(team, 0); |
| 58 | +
|
| 59 | + if (element[1] == 0) { |
| 60 | + int newCount = oldCount + 1; |
| 61 | + teamCount.put(team, newCount); |
| 62 | +
|
| 63 | + if (oldCount == 1 && newCount == 2) { |
| 64 | + validTeams++; |
| 65 | + } |
| 66 | + } else { |
| 67 | + int newCount = oldCount - 1; |
| 68 | +
|
| 69 | + if (oldCount == 2 && newCount == 1) { |
| 70 | + validTeams--; |
| 71 | + } |
| 72 | +
|
| 73 | + if (newCount == 0) { |
| 74 | + teamCount.remove(team); |
| 75 | + } else { |
| 76 | + teamCount.put(team, newCount); |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | + prevS = element[0]; |
| 81 | + } |
| 82 | +
|
| 83 | + answer = Math.max(answer, validTeams); |
| 84 | + return answer; |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
0 commit comments