标签:
1 #include "bits/stdc++.h" 2 3 #define maxN 100010 4 5 using namespace std; 6 typedef long long QAQ; 7 8 struct Tree 9 { 10 int l, r ; 11 QAQ sum ;//当前开着的灯数量 12 bool idv;//开关灯的延迟标记 13 }; 14 15 QAQ Min(QAQ a, QAQ b) 16 { 17 return a > b ? b : a; 18 } 19 20 Tree tr[maxN << 2]; 21 22 void Push_down ( int i , int m) 23 { 24 if(tr[i].idv)//当前结点有延迟标记 25 { 26 tr[i << 1].idv = !tr[i << 1].idv ;//左 27 tr[i << 1 | 1].idv = !tr[i << 1 | 1].idv ;//右 28 tr[i << 1].sum = tr[i << 1].r - tr[i << 1].l + 1 - tr[i << 1].sum ;//所以灯开关状态取反 29 tr[i << 1 | 1].sum = tr[i << 1 | 1].r - tr[i << 1 | 1].l + 1 - tr[i << 1 | 1].sum ;//同上 30 tr[i].idv = !tr[i].idv ;//清零!!! 31 } 32 } 33 34 void Build_Tree (int x , int y, int i) 35 { 36 tr[i].l = x ;//左端点 37 tr[i].r = y ;//右端点 38 if( x == y)return ; 39 else 40 { 41 QAQ mid = (tr[i].l + tr[i].r ) >> 1 ; 42 Build_Tree ( x , mid , i << 1);//左递归 43 Build_Tree (mid + 1 , y , i << 1 | 1);//右递归 44 } 45 } 46 47 void Update_Tree (int q , int w , int i) 48 { 49 if( w >= tr[i].r && q <= tr[i].l)//被完全包含 50 { 51 tr[i].idv = !tr[i].idv;//延迟标记 52 QAQ tot = tr[i].r - tr[i].l + 1;//当前结点总共的灯 53 tr[i].sum = tot - tr[i].sum;//开关状态全部取反 54 return ; 55 } 56 else 57 { 58 Push_down( i , tr[i].r - tr[i].l + 1 );//信息下传函数 59 QAQ mid = (tr[i].l + tr[i].r) >> 1; 60 if( q > mid ) 61 { 62 Update_Tree ( q , w , i << 1 | 1); 63 } 64 else if ( w <= mid ) 65 { 66 Update_Tree ( q , w , i << 1); 67 } 68 else 69 { 70 Update_Tree ( q , w , i << 1 | 1); 71 Update_Tree ( q , w , i << 1); 72 } 73 tr[i].sum = tr[i << 1].sum + tr[i << 1 | 1].sum ;//回溯更新 74 } 75 } 76 77 QAQ Query_Tree (int q , int w , int i ) 78 { 79 if( w >= tr[i].r && q <= tr[i].l) 80 { 81 return tr[i].sum;//被完全包含直接返回值 82 } 83 else 84 { 85 Push_down( i , tr[i].r - tr[i].l + 1 ); 86 QAQ mid = (tr[i].l + tr[i].r ) >> 1; 87 if( q > mid ) 88 { 89 return Query_Tree ( q , w , i << 1 | 1); 90 } 91 else if ( w <= mid ) 92 { 93 return Query_Tree ( q , w , i << 1); 94 } 95 else 96 { 97 return Query_Tree ( q , w , i << 1 | 1) + Query_Tree ( q , w , i << 1); 98 } 99 } 100 } 101 102 int main ( ) 103 { 104 QAQ N, M; 105 int op, l, r; 106 scanf("%d%d", &N, &M); 107 Build_Tree ( 1 , N , 1 );//建树操作 108 while (M--) 109 { 110 scanf("%d", &op); 111 if( !op ) 112 { 113 scanf("%d%d", &l, &r); 114 Update_Tree ( l , r , 1 );//更新树 115 } 116 else 117 { 118 scanf("%d%d", &l, &r); 119 printf("%lld\n", Query_Tree ( l , r , 1 ));//查询 120 } 121 } 122 return 0 ; 123 }
标签:
原文地址:http://www.cnblogs.com/shadowland/p/5870402.html