用题目描述 Description
在一条数轴上有N个点,分别是1~N。一开始所有的点都被染成黑色。接着
我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色。请输出每个操作执行后
剩余黑色点的个数。
输入描述 Input Description
输入一行为N和M。下面M行每行两个数Li、Ri
输出描述 Output Description
输出M行,为每次操作后剩余黑色点的个数。
样例输入 Sample Input
10 3
3 3
5 7
2 8
样例输出 Sample Output
9
6
3
用线段树也可以做的,不过并查集更简单。每个点指向右边第一个未染色的点。若都染色,则指向n+1。
#include<set>
#include<iostream>
#include<sstream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 10;
const int INF = 1e9 + 10;
int fa[maxn];
int find(int x) {
return fa[x] == x ? fa[x] : fa[x] = find(fa[x]);
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n+1; i++) fa[i] = i;
int ans = n;
while(m--) {
int l, r;
scanf("%d%d", &l, &r);
l = find(l);
while(l <= r) {
fa[l] = fa[l+1];
ans--;
l = find(l);
}
printf("%d\n", ans);
}
return 0;
}