解题思路
我开始以为是排序,然后把重合的部分拼接起来,最后用总的长度减去所占有效区间的长度。可想而知,麻烦的要死。然后看了一下书发现这个题可以用哈希,然后愉快的解决了。
注意点:所以方法很重要,骚想法并没有什么卵用。0要考虑在内。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main(){
int L,M;
int Hash[10005];
while(scanf("%d%d",&L,&M) != EOF){
memset(Hash,0,sizeof(Hash));
int from,to;
for(int i = 0;i < M;i++){
scanf("%d%d",&from,&to);
for(int j = from;j <= to;j++){
Hash[j]++;
}
}
int ans=0;
for(int i = 0;i <= L;i++){
if(Hash[i] == 0){
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}