Description
Input
Output
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
Source
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int HLEN = 10000010; 7 const int LEN = 20020; 8 9 struct line 10 { 11 int left; 12 int right; 13 int value; 14 }line[LEN*4]; 15 16 struct input 17 { 18 int a, b; 19 }in[LEN>>1]; //用于记录输入的数据 20 int sub[LEN]; //用于离散化 21 22 short hash[HLEN]; //用于储存离散后的映射关系 23 int ans = 0; 24 bool poster[LEN]; 25 26 void buildt(int l, int r, int step) 27 { 28 line[step].left = l; 29 line[step].right = r; 30 line[step].value = 0; 31 if (l == r) 32 return; 33 int mid = (l + r)>>1; 34 buildt(l, mid, step<<1); 35 buildt(mid+1, r, step<<1|1); 36 } 37 38 void pushdown(int step) 39 { 40 line[step<<1].value = line[step<<1|1].value = line[step].value; 41 line[step].value = 0; 42 } 43 44 void update(int l, int r, int value, int step) 45 { 46 if (line[step].left == l && line[step].right == r){ 47 line[step].value = value; 48 return; 49 } 50 if (line[step].value != 0 && line[step].value != value) //如果当前位置已被涂过色,则向下更新 51 pushdown(step); 52 int mid = (line[step].left + line[step].right)>>1; 53 if (r <= mid) 54 update(l, r, value, step<<1); 55 else if (l > mid) 56 update(l, r, value, step<<1|1); 57 else{ 58 update(l, mid, value, step<<1); 59 update(mid+1, r, value, step<<1|1); 60 } 61 } 62 63 void query(int l, int r, int step) 64 { 65 if (line[step].value != 0){ 66 if (!poster[line[step].value]){ 67 poster[line[step].value] = true; 68 ans++; 69 } 70 return; 71 } 72 if (line[step].left == line[step].right) 73 return; 74 int mid = (line[step].left + line[step].right)>>1; 75 if (r <= mid) 76 query(l, r, step<<1); 77 else if (l > mid) 78 query(l, r, step<<1|1); 79 else{ 80 query(l, mid, step<<1); 81 query(mid+1, r, step<<1|1); 82 } 83 } 84 85 int main() 86 { 87 int T; 88 scanf("%d", &T); 89 while(T--){ 90 memset(poster, 0, sizeof(poster)); 91 int n; 92 int num = 0; 93 scanf("%d", &n); 94 for(int i = 1; i <= n; i++){ 95 int a, b; 96 scanf("%d %d", &a, &b); 97 sub[num++] = a; 98 sub[num++] = b; 99 in[i].a = a; 100 in[i].b = b; 101 } 102 103 sort(sub, sub+num); 104 int size = unique(sub, sub+num) - sub; //删除重复数据并使用哈希表映射 105 for(int i = 0; i < size; i++) 106 hash[sub[i]] = i+1; 107 108 buildt(1, size, 1); //离散后的值建树 109 110 for(int i = 1; i <= n; i++) 111 update(hash[ in[i].a], hash[ in[i].b], i, 1); //用离散后的值更新 112 ans = 0; 113 query(1, size, 1); 114 printf("%d\n", ans); 115 } 116 return 0; 117 }