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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| #include <cstdio> #include <iostream> #include <vector> #include <algorithm> #include <cstring> using namespace std;
const int maxN = 1e5 + 7;
struct Tree { int l, r, color, tag; }t[maxN << 4];
int T, n, l[maxN], r[maxN], vis[maxN], ans;
vector<int> pos;
inline int getPos(int x) { return lower_bound(pos.begin(), pos.end(), x) - pos.begin(); }
void build(int l, int r, int num) { t[num].l = l; t[num].r = r; t[num].color = 0; t[num].tag = -1; if(l == r) return ; int mid = (l + r) >> 1; build(l, mid, num << 1); build(mid + 1, r, num << 1 | 1); }
void spread(int num) { if(t[num].tag != 0) { t[num << 1].color = t[num].color; t[num << 1 | 1].color = t[num].color; t[num << 1].tag = t[num << 1 | 1].tag = 1; t[num].tag = 0; } }
void change(int l, int r, int num, int x) {
if(l <= t[num].l && r >= t[num].r) { t[num].color = x; t[num].tag = 1; return ; } spread(num); int mid = (t[num].l + t[num].r) >> 1; if(l <= mid) change(l, r, num << 1, x); if(r > mid) change(l, r, num << 1 | 1, x); }
void query(int l, int r, int num) { if(t[num].tag == -1) return ; if(t[num].tag != 0) { if(!vis[t[num].color] && t[num].color != 0) { ++ans; vis[t[num].color] = 1; } return ; } int mid = (l + r) >> 1; query(l, r, num << 1); query(l, r, num << 1 | 1); }
int main() { scanf("%d", &T); while(T--) { scanf("%d", &n); ans = 0; memset(vis, 0, sizeof vis); pos.clear(); pos.push_back(0); for(int i = 1; i <= n; ++i) { scanf("%d%d", &l[i], &r[i]); pos.push_back(l[i]); pos.push_back(r[i]); pos.push_back(r[i] + 1); } sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin(), pos.end()), pos.end()); int len = pos.size() , cnt = 0; build(1, len, 1); for(int i = 1; i <= n; ++i) { l[i] = getPos(l[i]); r[i] = getPos(r[i]); change(l[i], r[i], 1, ++cnt); } query(1, len, 1); printf("%d\n", ans); } return 0; }
|