5Goodluck @ 2023-03-01 19:47:57
#include <stdio.h>
int main()
{
int m,n,t,e;
scanf("%d %d",&m,&n);
int a[n];
int b[n];
for(int i=0;i<n;i++){
scanf("%d %d",&a[i],&b[i]);
}
if(n!=1){
for(int i=0;i<n;i++){
for(int j=1;j<(n-i);j++){
if(a[j-1]>a[j]){
t=a[j-1];
a[j-1]=a[j];
a[j]=t;
e=b[j-1];
b[j-1]=b[j];
b[j]=e;
}
}
}
}
int sum=0;
for(int i=1;i<n;i++){
if(a[i]<b[i-1]){
sum=b[i-1]-a[i]+1;
}
}
int all=0;
for(int i=0;i<n;i++){
all=b[i]-a[i]+1+all;
}
all=m-all+sum+1;
printf("%d",all);
return 0;
}
by Silkage_x @ 2023-03-05 09:31:12
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
int main() {
int l, m, mn, mx, sum = 0, kk[10005] = {0};
cin >> l >> m;
for (int i = 0; i < m; i++) {
cin >> mn >> mx;
for (int i = mn; i <= mx; i++) {//对被访问的树标记为1,未访问的树默认为0
kk[i] = 1;
}
}
for (int i = 0; i <= l; i++) {//统计没有被访问的树的数目
if (!kk[i])sum++;
}
cout << sum;
return 0;
}
可以参考一下