题目链接:http://hihocoder.com/problemset/problem/1079
MD坑爹,线段查询的时候左闭右开。插完挨个点找一遍扔set里,注意没染色的情况。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define lson l, m, rt << 1 5 #define rson m + 1, r, rt << 1 | 1 6 const int maxn = 200100; 7 int sum[maxn<<2]; 8 9 void pushDOWN(int rt) { 10 if(sum[rt] != -1) { 11 sum[rt<<1] = sum[rt<<1|1] = sum[rt]; 12 sum[rt] = -1; 13 } 14 } 15 16 void update(int L, int R, int c, int l, int r, int rt) { 17 if(L <= l && r <= R) { 18 sum[rt] = c; 19 return; 20 } 21 pushDOWN(rt); 22 int m = (l + r) >> 1; 23 if(L <= m) update(L, R, c, lson); 24 if(R > m) update(L, R, c, rson); 25 } 26 27 int query(int p, int l, int r, int rt) { 28 if(l == r) return sum[rt]; 29 pushDOWN(rt); 30 int m = (l + r) >> 1; 31 if(p <= m) return query(p, lson); 32 else return query(p, rson); 33 } 34 35 int h[maxn], hcnt; 36 int n, m; 37 int lo[maxn], hi[maxn]; 38 39 int id(int x) { 40 return lower_bound(h, h+hcnt, x) - h + 1; 41 } 42 43 int main() { 44 // freopen("in", "r", stdin); 45 while(~scanf("%d%d",&n,&m)) { 46 hcnt = 0; 47 memset(sum, -1, sizeof(sum)); 48 for(int i = 0; i < n; i++) { 49 scanf("%d%d",&lo[i], &hi[i]); 50 h[hcnt++] = lo[i], h[hcnt++] = hi[i]; 51 } 52 sort(h, h+hcnt); hcnt = unique(h, h+hcnt) - h; 53 m = hcnt; 54 for(int i = 0; i < n; i++) { 55 update(id(lo[i]), id(hi[i])-1, i, 1, m, 1); 56 } 57 set<int> s; 58 s.insert(-1); 59 for(int i = 1; i <= m; i++) { 60 s.insert(query(i, 1, m, 1)); 61 // printf("%d ", query(i, 1, m, 1)); 62 } 63 // printf(" "); 64 printf("%d ", s.size()-1); 65 } 66 return 0; 67 }