「SDOI2005」区间
「SDOI2005」区间
传送门
记录每一个位置作为左端点和右端点的出现次数,然后直接考虑差分即可。
参考代码:
#include <cstdio>
#define rg register
#define file(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
template < class T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while ('0' > c || c > '9') f |= c == '-', c = getchar();
while ('0' <= c && c <= '9') s = s * 10 + c - 48, c = getchar();
s = f ? -s : s;
}
const int _ = 1e6 + 5;
int n, cnt, L[_], R[_];
int main() {
#ifndef ONLINE_JUDGE
file("cpp");
#endif
read(n);
for (rg int l, r; n--; ) read(l), read(r), ++L[l], ++R[r];
for (rg int i = 1; i <= 1000000; ++i) {
if (cnt == 0 && L[i]) printf("%d ", i);
cnt += L[i] - R[i];
if (cnt == 0 && R[i]) printf("%d\n", i);
}
return 0;
}