【POJ】2528 Mayor's posters
离散化+线段树。
数组开的不够大,wa了N多回。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 #define MAXN 12010 8 #define lson l, mid, rt<<1 9 #define rson mid+1, r, rt<<1|1 10 11 int ls[MAXN], rs[MAXN]; 12 int nums[MAXN<<4]; 13 int buf[MAXN*3], cnt; 14 char set[MAXN]; 15 16 inline int bin(int x, int n) { 17 int l = 0, r = n-1; 18 int mid; 19 20 while (l <= r) { 21 mid = (l+r)>>1; 22 if (buf[mid] == x) 23 return mid; 24 if (buf[mid] < x) 25 l = mid+1; 26 else 27 r = mid-1; 28 } 29 30 return -1; 31 } 32 /* 33 void build(int l, int r, int rt) { 34 nums[rt] = 0; 35 if (l == r) 36 return ; 37 int mid = (l+r)>>1; 38 build(lson); 39 build(rson); 40 }*/ 41 42 inline void PushDown(int rt) { 43 if ( nums[rt] ) { 44 nums[rt<<1] = nums[rt<<1|1] = nums[rt]; 45 nums[rt] = 0; 46 } 47 } 48 49 void update(int ll, int rr, int c, int l, int r, int rt) { 50 if (ll<=l && rr>=r) { 51 nums[rt] = c; 52 return ; 53 } 54 PushDown(rt); 55 int mid = (l+r)>>1; 56 if (ll <= mid) 57 update(ll, rr, c, lson); 58 if (rr > mid) 59 update(ll, rr, c, rson); 60 } 61 62 void query(int l, int r, int rt) { 63 if ( nums[rt] ) { 64 if ( !set[nums[rt]] ) 65 ++cnt; 66 set[nums[rt]] = 1; 67 return ; 68 } 69 if (l == r) 70 return ; 71 int mid = (l+r)>>1; 72 query(lson); 73 query(rson); 74 } 75 76 int main() { 77 int case_n, n, m; 78 int i, j, ll, rr; 79 80 scanf("%d", &case_n); 81 82 while (case_n--) { 83 scanf("%d", &n); 84 m = 0; 85 for (i=1; i<=n; ++i) { 86 scanf("%d %d", &ls[i], &rs[i]); 87 buf[m++] = ls[i]; 88 buf[m++] = rs[i]; 89 } 90 sort(buf, buf+m); 91 j = 1; 92 for (i=1; i<m; ++i) { 93 if (buf[i] != buf[i-1]) 94 buf[j++] = buf[i]; 95 } 96 m = j; 97 for (i=m-1; i>0; --i) { 98 if (buf[i] != buf[i-1]+1) 99 buf[m++] = buf[i-1]+1; 100 } 101 sort(buf, buf+m); 102 //build(0, m, 1); 103 memset(nums, 0, sizeof(nums)); 104 memset(set, 0, sizeof(set)); 105 for (i=1; i<=n; ++i) { 106 ll = bin(ls[i], m); 107 rr = bin(rs[i], m); 108 update(ll, rr, i, 0, m, 1); 109 } 110 cnt = 0; 111 query(0, m, 1); 112 printf("%d\n", cnt); 113 } 114 115 return 0; 116 }