Java实现 洛谷 P1047 校门外的树

在这里插入图片描述

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        boolean[] b = new boolean[sc.nextInt() + 1];
        int t = sc.nextInt(), ans = 0;

        while(t-- > 0) {
            int start = sc.nextInt(), end = sc.nextInt();
            for(int i = start; i <= end; ++i)
                b[i] = true;
        }
        for(int i = 0; i < b.length; ++i)
            if(!b[i]) ans++;
        System.out.println(ans);
        sc.close();
    }
}
原文地址:https://www.cnblogs.com/a1439775520/p/13076694.html