【wikioi】1191 数轴染色(线段树+水题)

http://wikioi.com/problem/1191/

太水的线段树了,敲了10分钟就敲完了,但是听说还有一种并查集的做法?不明觉厉。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

#define lc x<<1
#define rc x<<1|1
#define MID (l+r)>>1
#define lson l, m, lc
#define rson m+1, r, rc
const int N=200005;
int s[N*10], L, R, n;
bool add[N*10];
inline void pushup(const int &x) { s[x]=s[lc]+s[rc]; }
inline void pushdown(const int &x) {
	if(add[x]) {
		s[lc]=0; s[rc]=0;
		add[lc]=1; add[rc]=1;
		add[x]=0;
	}
}
void build(const int &l, const int &r, const int &x) {
	if(l==r) { s[x]=1; return; }
	int m=MID;
	build(lson); build(rson);
	pushup(x);
}
void update(const int &l, const int &r, const int &x) {
	if(L<=l && r<=R) { add[x]=1; s[x]=0; return; }
	pushdown(x);
	int m=MID;
	if(L<=m) update(lson); if(m<R) update(rson);
	pushup(x);
}
int query(const int &l, const int &r, const int &x) {
	if(L<=l && r<=R) return s[x];
	pushdown(x);
	int m=MID, ret=0;
	if(L<=m) ret+=query(lson); if(m<R) ret+=query(rson);
	pushup(x);
	return ret;
}
int main() {
	read(n);
	int m=getint();
	build(1, n, 1);
	while(m--) {
		read(L); read(R);
		update(1, n, 1);
		L=1, R=n;
		printf("%d
", query(1, n, 1));
	}	
	return 0;
}

在一条数轴上有N个点,分别是1~N。一开始所有的点都被染成黑色。接着
我们进行M次操作,第i次操作将[Li,Ri]这些点染成白色。请输出每个操作执行后
剩余黑色点的个数。

输入一行为N和M。下面M行每行两个数Li、Ri

输出M行,为每次操作后剩余黑色点的个数。

10 3
3 3
5 7
2 8

9
6
3

数据限制
对30%的数据有1<=N<=2000,1<=M<=2000
对100%数据有1<=Li<=Ri<=N<=200000,1<=M<=200000

原文地址:https://www.cnblogs.com/iwtwiioi/p/3932503.html