主要的问题在离散化上
关于区间端点的离散化,需要考虑端点开闭,以及题目对区间长度的要求。
这题线段树维护的是题目所给区间的端点,离散化完了 range update 端点下标所确定的区间。
答案是有多少不同的叶节点。
区间离散化不同之处:
画图会更好理解
假设我们现在要覆盖一个区间,相当于将区间所有数 range update $+1$。
- 考虑区间 $\left[1, n\right]$ ,将其分成$\left[1,\ \left\lceil\dfrac{n}{2}\right\rceil\right] \sim \left[\left\lceil\dfrac{n}{2}\right\rceil + 1, \ n\right]$。
- 覆盖前一半,考虑里面所有数,共 $\left\lceil\dfrac{n}{2}\right\rceil$ 个。
- 覆盖后一半,同理。$\left\lfloor\dfrac{n}{2}\right\rfloor$ 个数。
此时区间里面所有数都被我们更新了,按道理应该认为这个区间是被覆盖了的,但是其实还漏出来了一节。存在一个相邻两个数构成的,长度为 $1$ 的区间
$$
\left[\left\lceil\dfrac{n}{2}\right\rceil, \left\lceil\dfrac{n}{2}\right\rceil + 1\right]
$$
这个东西在线段树上是没法维护的,所以在离散化的时候,将所有的右端点 $+1$,相当于把上面这个区间增加一节,就可以维护了。最后在query答案的时候记得右端点 $-1$,因为答案是叶节点。
/***
* author: wrz
* created: 04.20.2022 16:21:51
*/
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "../template/debug.hpp"
#else
#define debug(...) 42
#endif
class segtree {
public:
struct node {
// don't forget to set default value (used for leaves)
// not necessarily neutral element!
int add = -1;
void apply(int l, int r, int v) {
add = v;
}
};
node unite(const node &a, const node &b) const {
node res;
return res;
}
inline void push(int x, int l, int r) {
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
// push from x into (x + 1) and z
if (tree[x].add != -1) {
tree[x + 1].apply(l, y, tree[x].add);
tree[z].apply(y + 1, r, tree[x].add);
tree[x].add = -1;
}
}
inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); }
int n;
vector<node> tree;
void build(int x, int l, int r) {
if (l == r) {
return;
}
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
build(x + 1, l, y);
build(z, y + 1, r);
pull(x, z);
}
template <typename M>
void build(int x, int l, int r, const vector<M> &v) {
if (l == r) {
tree[x].apply(l, r, v[l]);
return;
}
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
build(x + 1, l, y, v);
build(z, y + 1, r, v);
pull(x, z);
}
node get(int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
return tree[x];
}
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
push(x, l, r);
node res{};
if (rr <= y) {
res = get(x + 1, l, y, ll, rr);
} else {
if (ll > y) {
res = get(z, y + 1, r, ll, rr);
} else {
res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));
}
}
pull(x, z);
return res;
}
template <typename... M>
void modify(int x, int l, int r, int ll, int rr, const M &...v) {
if (ll <= l && r <= rr) {
tree[x].apply(l, r, v...);
return;
}
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
push(x, l, r);
if (ll <= y) {
modify(x + 1, l, y, ll, rr, v...);
}
if (rr > y) {
modify(z, y + 1, r, ll, rr, v...);
}
pull(x, z);
}
int find_first_knowingly(int x, int l, int r,
const function<bool(const node &)> &f) {
if (l == r) {
return l;
}
push(x, l, r);
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
int res;
if (f(tree[x + 1])) {
res = find_first_knowingly(x + 1, l, y, f);
} else {
res = find_first_knowingly(z, y + 1, r, f);
}
pull(x, z);
return res;
}
int find_first(int x, int l, int r, int ll, int rr,
const function<bool(const node &)> &f) {
if (ll <= l && r <= rr) {
if (!f(tree[x])) {
return -1;
}
return find_first_knowingly(x, l, r, f);
}
push(x, l, r);
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
int res = -1;
if (ll <= y) {
res = find_first(x + 1, l, y, ll, rr, f);
}
if (rr > y && res == -1) {
res = find_first(z, y + 1, r, ll, rr, f);
}
pull(x, z);
return res;
}
int find_last_knowingly(int x, int l, int r,
const function<bool(const node &)> &f) {
if (l == r) {
return l;
}
push(x, l, r);
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
int res;
if (f(tree[z])) {
res = find_last_knowingly(z, y + 1, r, f);
} else {
res = find_last_knowingly(x + 1, l, y, f);
}
pull(x, z);
return res;
}
int find_last(int x, int l, int r, int ll, int rr,
const function<bool(const node &)> &f) {
if (ll <= l && r <= rr) {
if (!f(tree[x])) {
return -1;
}
return find_last_knowingly(x, l, r, f);
}
push(x, l, r);
int y = (l + r) >> 1;
int z = x + ((y - l + 1) << 1);
int res = -1;
if (rr > y) {
res = find_last(z, y + 1, r, ll, rr, f);
}
if (ll <= y && res == -1) {
res = find_last(x + 1, l, y, ll, rr, f);
}
pull(x, z);
return res;
}
segtree(int _n) : n(_n) {
assert(n > 0);
tree.resize(2 * n - 1);
build(0, 0, n - 1);
}
template <typename M>
segtree(const vector<M> &v) {
n = v.size();
assert(n > 0);
tree.resize(2 * n - 1);
build(0, 0, n - 1, v);
}
node get(int ll, int rr) {
assert(0 <= ll && ll <= rr && rr <= n - 1);
return get(0, 0, n - 1, ll, rr);
}
node get(int p) {
return get(0, 0, n - 1, p, p);
}
template <typename... M>
void modify(int ll, int rr, const M &...v) {
assert(0 <= ll && ll <= rr && rr <= n - 1);
modify(0, 0, n - 1, ll, rr, v...);
}
// find_first and find_last call all FALSE elements
// to the left (right) of the sought position exactly once
int find_first(int ll, int rr, const function<bool(const node &)> &f) {
assert(0 <= ll && ll <= rr && rr <= n - 1);
return find_first(0, 0, n - 1, ll, rr, f);
}
int find_last(int ll, int rr, const function<bool(const node &)> &f) {
assert(0 <= ll && ll <= rr && rr <= n - 1);
return find_last(0, 0, n - 1, ll, rr, f);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
while (tt--) {
int n;
cin >> n;
vector<pair<int, int>> p(n);
for (int i = 0; i < n; ++i) {
cin >> p[i].first >> p[i].second;
++p[i].second;
}
vector<int> a;
for (int i = 0; i < n; ++i) {
a.push_back(p[i].first);
a.push_back(p[i].second);
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
segtree st((int) a.size());
for (int i = 0; i < n; ++i) {
auto [l, r] = p[i];
auto ll = lower_bound(a.begin(), a.end(), l) - a.begin();
auto rr = lower_bound(a.begin(), a.end(), r) - a.begin();
st.modify((int) ll, (int) rr - 1, i + 123);
}
set<int> ans;
for (int i = 0; i < (int) a.size(); ++i) {
ans.insert(st.get(i).add);
}
cout << ans.size() - 1 << '\n';
}
return 0;
}