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
| #include <cstdio> #include <algorithm> #include <vector> #include <set> using namespace std; const int N = 1e5; int t, n, c[N], l[N], r[N], order[N], p[N], sz[N], cnt; struct Cmp{ bool operator() (const int x, const int y) const { return r[x] != r[y] ? r[x] < r[y] : x < y; } }; int find(int x) { if (p[x] != x) p[x] = find(p[x]); return p[x]; } void connect(int x, int y) { int px = find(x), py = find(y); if (px != py) { if (sz[x] > sz[y]) swap(x, y); p[px] = py; sz[y] += sz[x]; --cnt; } } int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); cnt = n; vector<set<int, Cmp>> open(2); for (int i = 0; i < n; ++i) { scanf("%d%d%d", &c[i], &l[i], &r[i]); p[i] = order[i] = i, sz[i] = 1; } sort(order, order + n, [&](int x, int y) { return l[x] != l[y] ? l[x] < l[y] : (r[x] != r[y] ? r[x] < r[y] : x < y); }); for (int i = 0; i < n; ++i) { int o = order[i]; int lo = l[o], ro = r[o], co = c[o], xc = co ^ 1; auto &cs = open[co], &xs = open[xc]; while (cs.size() && r[*cs.begin()] < lo) cs.erase(cs.begin()); while (xs.size() && r[*xs.begin()] < lo) xs.erase(xs.begin()); cs.insert(o); while (xs.size()) { auto it = xs.begin(); connect(o, *it); if (xs.size() > 1) xs.erase(it); else break; } } printf("%d\n", cnt); } return 0; }
|