题解 | #值周#
值周
https://ac.nowcoder.com/acm/problem/24636
暴力会超时,在这里使用前缀和。
import java.util.Scanner;
public class Main{
public static void main(String []args){
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int n = sc.nextInt();
int []a = new int[N+1];
int []sum = new int[N+1];
for(int i = 0;i<n;i++){
int start = sc.nextInt();
int end = sc.nextInt();
a[start]++;
if(end+1<N+1) a[end+1]--;
}
sum[0]=a[0];
int ans = 0;
for(int i=1;i<N+1;i++){
sum[i] = sum[i-1]+a[i];
if(sum[i]==0){
ans++;
}
}
if(a[0]==0){
ans++;
}
System.out.println(ans);
}
}