1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; const int N = 10010; int c, n, l, r; pair<int, int> pos[N]; vector<int> s; int get(int x) { return lower_bound(s.begin(), s.end(), x) - s.begin(); } struct Node { int l, r, lazy; } t[N << 3];
void build(int u, int l, int r) { t[u] = {l, r, 0}; if (l == r) return; int mid = l + r >> 1; build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r); } bool update(int u, int l, int r) { if (t[u].lazy) return false; if (t[u].l == t[u].r) return t[u].lazy = true; bool res = false; int mid = t[u].l + t[u].r >> 1; if (l <= mid) res |= update(u << 1, l, r); if (r > mid) res |= update(u << 1 | 1, l, r); if (t[u << 1].lazy && t[u << 1 | 1].lazy) t[u].lazy = true; return res; } int main() { scanf("%d", &c); while (c--) { s.clear(); memset(t, 0, sizeof(t)); scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d%d", &l, &r); ++r; pos[i] = {l, r}; s.push_back(l); s.push_back(r); } sort(s.begin(), s.end()); s.erase(unique(s.begin(), s.end()), s.end()); int len = s.size(); build(1, 0, s.size() - 2); int ans = 0; for (int i = n; i; --i) { l = get(pos[i].first), r = get(pos[i].second) - 1; if (update(1, l, r)) ++ans; } printf("%d\n", ans); } return 0; }
|