标签:
动态建立结点就不用离散化了,细节见代码,相信还是比较好理解的。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <set> 5 using namespace std; 6 7 const int N = 500000; 8 set<int> s; 9 int cnt; 10 11 struct Node 12 { 13 int lc, rc, l, r, c; 14 void init( int _l, int _r, int _c ) 15 { 16 l = _l, r = _r, c = _c; 17 lc = rc = -1; 18 } 19 } node[N << 2]; 20 21 void pushdown( int i ) 22 { 23 if ( node[i].c != -1 ) 24 { 25 node[node[i].lc].c = node[i].c; 26 node[node[i].rc].c = node[i].c; 27 node[i].c = -1; 28 } 29 } 30 31 void update( int i, int l, int r, int c ) 32 { 33 if ( node[i].l == l && node[i].r == r ) 34 { 35 node[i].c = c; 36 return ; 37 } 38 int mid = ( node[i].l + node[i].r ) >> 1; 39 if ( node[i].lc == -1 ) 40 { 41 node[i].lc = cnt; 42 node[cnt].init( node[i].l, mid, -1 ); 43 cnt++; 44 } 45 if ( node[i].rc == -1 ) 46 { 47 node[i].rc = cnt; 48 node[cnt].init( mid + 1, node[i].r, -1 ); 49 cnt++; 50 } 51 pushdown(i); 52 if ( r <= mid ) 53 { 54 update( node[i].lc, l, r, c ); 55 } 56 else if ( l > mid ) 57 { 58 update( node[i].rc, l, r, c ); 59 } 60 else 61 { 62 update( node[i].lc, l, mid, c ); 63 update( node[i].rc, mid + 1, r, c ); 64 } 65 } 66 67 void dfs( int i ) 68 { 69 if ( node[i].c != -1 ) 70 { 71 s.insert( node[i].c ); 72 return ; 73 } 74 if ( node[i].lc != -1 ) dfs( node[i].lc ); 75 if ( node[i].rc != -1 ) dfs( node[i].rc ); 76 } 77 78 int main () 79 { 80 int t; 81 scanf("%d", &t); 82 while ( t-- ) 83 { 84 int n; 85 scanf("%d", &n); 86 node[0].init( 1, 10000000, -1 ); 87 cnt = 1; 88 s.clear(); 89 for ( int i = 0; i < n; i++ ) 90 { 91 int l, r; 92 scanf("%d%d", &l, &r); 93 if ( l > r ) swap( l, r ); 94 update( 0, l, r, i ); 95 } 96 dfs(0); 97 printf("%d\n", s.size()); 98 } 99 return 0; 100 }
标签:
原文地址:http://www.cnblogs.com/huoxiayu/p/4815936.html