import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int L = sc.nextInt();
int M = sc.nextInt();
int[] arr = new int[L + 1];
// 把0到L的树种上
for (int i = 0; i < arr.length; i++) {
arr[i] = 1;
}
// 开始移除树
for (int i = 0; i < M; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
for (int j = a; j <= b; j++) {
arr[j] = 2;
}
}
// 遍历还剩几棵树
int g = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 2) {
g++;
}
}
//注意距离是0到L,一共有L+1棵树
System.out.println(L - g + 1);
}
}
Java实现 洛谷 P1047 校门外的树
最新推荐文章于 2022-02-24 23:10:28 发布