1. 程式人生 > >模板-線段樹

模板-線段樹

僅供參考 print tree 維護 pre sca turn for i++

  上課時候的筆記。僅供參考。

 1 #include <cstdio>
 2 
 3 struct Tree {
 4     int l, r;
 5     long long sum;
 6 } tr[1024000];
 7 int a[1024000];
 8 
 9 void Build_Tree ( int x , int y , int i ) {
10     tr[i].l = x;
11     tr[i].r = y;
12     if( x == y )tr[i].sum = a[x] ;                             //到葉子節點,賦值
13
else { 14 int mid = (tr[i].l + tr[i].r ) / 2 ; 15 Build_Tree ( x , mid , i * 2); //左子樹 16 Build_Tree ( mid + 1 , y , i * 2 + 1); //右子樹 17 tr[i].sum = tr[i * 2].sum + tr[i * 2 + 1].sum; //回溯維護區間和 18 } 19 } 20 21 long long
Query_Tree ( int q , int w , int i ) { 22 if ( q <= tr[i].l && w >= tr[i].r ) return tr[i].sum; //當前結點的區間完全被目標區間包含 23 else { 24 long long mid = (tr[i].l + tr[i].r) / 2; 25 if( q > mid ) { //完全在左兒子 26 return Query_Tree ( q , w , i * 2
+ 1); 27 } else if (w <= mid ) { //完全在右兒子 28 return Query_Tree ( q , w , i * 2); 29 } else { //目標區間在左右都有分布 30 return Query_Tree ( q , w , i * 2) + Query_Tree ( q , w , i * 2 + 1 ); 31 } 32 } 33 } 34 35 int main ( ) { 36 int N, M, q, val, l, r; 37 scanf("%d", &N); 38 for ( int i = 1 ; i <= N ; i++ )scanf("%d", &a[i]); 39 Build_Tree ( 1 , N , 1); 40 scanf("%d%d", &l, &r); 41 printf("%lld\n", Query_Tree ( l , r, 1 )); 42 return 0 ; 43 }

模板-線段樹