计蒜客 蓝桥杯模拟二 区间合并 打扫教室

数据范围

样例输入

10 3
1 5
3 7
9 10

样例输出

1题解：先将区间进行排序，排序后的区间只有相交和不相交两种。当前右边端点大于后面的左边端点时则两个区间相交，否则不相交然后用总数减去每个相交区间所含个数得到结果
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6+10;
const ll mod = 10086;
const double pi = acos(-1.0);
const double eps = 1e-8;
struct node {
ll le, ri;
};
node a[maxn];
bool cmp( node p, node q ) {
if( p.le == q.le ) {
return p.ri < q.ri;
}
return p.le < q.le;
}
int main() {
ll n, m;
scanf("%lld%lld",&n,&m);
for( ll i = 0; i < m; i ++ ) {
scanf("%lld%lld",&a[i].le,&a[i].ri);
}
sort(a,a+m,cmp);
ll le = a[0].le, ri = a[0].ri;
for( ll i = 1; i < m; i ++ ) {
if( ri >= a[i].le ) {
le = min(le,a[i].le);
ri = max(ri,a[i].ri);
} else {
n = n - (ri-le+1);
le = a[i].le;
ri = a[i].ri;
}
}
n = n - (ri-le+1);
printf("%lld\n",n);
return 0;
}


也可以求前缀和

0条评论