Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
1.3 kB
1
Indexable
Never
class Main {
    public static void main(String[] args) {
    
        Scanner in = new Scanner(System.in);
       
        int numOfWorker = in.nextInt();
        int lenOfStreet = in.nextInt();
        List<List<Integer>> cleanedSegments = new ArrayList<>();
        List<List<Integer>> mergedSegments = new ArrayList<>();
        while(numOfWorker > 0) {
            int start = in.nextInt();
            int end = in.nextInt();
            cleanedSegments.add(Arrays.asList(start, end));
            numOfWorker--;
        }
       
        Collections.sort(cleanedSegments,(arr1, arr2) -> arr1.get(0) - arr2.get(0));
        mergedSegments.add(cleanedSegments.get(0));
        for(int i = 1; i < cleanedSegments.size(); i++) {
            int size = mergedSegments.size();
            if(mergedSegments.get(size-1).get(1) >= cleanedSegments.get(i).get(0))
                mergedSegments.get(size-1).set(1, cleanedSegments.get(i).get(1));
            else
                mergedSegments.add(cleanedSegments.get(i));
        }
       
        int numOfCoveredStreets = 0;
        for(List<Integer> l: mergedSegments) {
            numOfCoveredStreets += l.get(1) - l.get(0) + 1;
        }
       
        System.out.println(lenOfStreet - numOfCoveredStreets);
    }
}