https://www.luogu.com.cn/problem/P1047
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
long long qmi(int m, int k)
{
int res = 1, t = m;
while (k)
{
if (k&1) res = res * t;
t = t * t;
k >>= 1;
}
return res;
}
int read(){
int x = 0,f = 1;
char c = getchar();
while (c<'0'||c>'9') {
if (c=='-') {
f = -1;
}
c = getchar();
}
while (c>='0'&&c<='9') {
x = x*10+c-'0';
c = getchar();
}
return x*f;
}
#define fi(a,b) for(int i = a; i <= b; i++)
#define fj(a,b) for(int j = a; j >= b; j--)
//以上不解释
int L,M;
//变量名称与原题相同
int cnt = 0;
//剩余树的数目
int vis[10000 + 10];
//用来判断树木有没有被访问过
int main(){
L = read();
M = read();//标准输入,不解释
for(int i=1 ; i<=M; i++){
//定义区间的头和尾
int head = read();
int tail = read();
for(int j= head ; j <= tail; j++)
if(vis[j] == 0)
vis[j] = 1;//从这个区间的头和尾开始,将所有区间内的树标记为访问过
}
for(int i=0; i<= L;i++){
if(vis[i] == 0)
cnt++;//计算未被访问过的树的数目
}
cout<<cnt<<endl;
return 0;
}
/*
500 3
150 300
100 200
470 471
============
298
*/
网友评论