hdu--4027--不錯的線段樹

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   for   sp   

這題 蠻好的 如果用心去感受<講得好像太文藝了>

不知道 你們有沒有想過為什麼 有時候進行區間更新操作的時候 可以直接對整個區間進行操作 而不必要一個點 一個點地去更新

因為 我們對這個區間內的每個數 都是相同的操作 所以可以放在一起操作 就像一起+- K什麼的

但這裡呢 是對[ L , R ]這個區間內的每一個元素 開平方  我們只能一個點 一個點地更新.

那你可能會想 這樣我們就發揮不了線段樹的優勢了啊 和for遍曆一樣了

我們還是可以運用下 標記  雖然不是延遲更新的思想 但效果上也能節省很多時間

我們知道<1,3>無限開平方 下去 永遠都是1  所以這是沒必要的  我們就應該標記出這些特殊資料

orz...11.11 分手半年多了 我要擺脫單身了嗎?

  1 #include <iostream>  2 #include <cmath>  3 #include <algorithm>  4 using namespace std;  5   6 typedef long long LL;  7 const int size = 100010;  8 struct data  9 { 10     int L; 11     int R; 12     bool flag;//判斷是否為1 13     LL sum; 14 }tree[size*4]; 15  16 void pushUp( int root ) 17 { 18     tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum; 19     tree[root].flag = tree[root<<1].flag && tree[root<<1|1].flag; 20 } 21  22 void build( int root , int L , int R ) 23 { 24     int Mid = ( L + R ) >> 1; 25     tree[root].L = L; 26     tree[root].R = R; 27     tree[root].flag = false; 28     if( L == R ) 29     { 30         cin >> tree[root].sum; 31         if( tree[root].sum<=1 ) 32             tree[root].flag = true; 33         return ; 34     } 35     build( root<<1 , L , Mid ); 36     build( root<<1|1 , Mid+1 , R ); 37     pushUp( root ); 38 } 39  40 void update( int root , int L , int R ) 41 { 42     int Mid = ( tree[root].L + tree[root].R ) >> 1; 43     if( tree[root].flag ) 44         return; 45     if( tree[root].L  == L && tree[root].R == R && L == R ) 46     { 47         tree[root].sum = (LL)sqrt( tree[root].sum*1.0 ); 48         if( tree[root].sum <= 1 ) 49         { 50             tree[root].flag = true; 51         } 52         return ; 53     } 54     if( R<=Mid ) 55         update( root<<1 , L , R ); 56     else if( L>=Mid+1 ) 57         update( root<<1|1 , L , R ); 58     else 59     { 60         update( root<<1 , L , Mid ); 61         update( root<<1|1 , Mid+1 , R ); 62     } 63     pushUp( root ); 64 } 65  66 LL query( int root , int L , int R ) 67 { 68     int Mid = ( tree[root].L + tree[root].R ) >> 1; 69     LL ans = 0; 70     if( tree[root].L == L && tree[root].R == R ) 71     { 72         return tree[root].sum ; 73     } 74     if( R<=Mid ) 75         ans += query( root<<1 , L , R ); 76     else if( L>=Mid+1 ) 77         ans += query( root<<1|1 , L , R ); 78     else 79     { 80         ans += query( root<<1 , L , Mid ); 81         ans += query( root<<1|1 , Mid+1 , R ); 82     } 83     return ans; 84 } 85  86 int main() 87 { 88     cin.sync_with_stdio(false); 89     int n , m , op , x , y; 90     int cnt = 1; 91     while( cin >> n ) 92     { 93         build( 1 , 1 , n ); 94         cin >> m; 95         cout << "Case #" << cnt++ << ":" << endl; 96         while( m-- ) 97         { 98             cin >> op >> x >> y; 99             if( x>y )100                 swap( x , y );101             if( !op )102             {103                 update( 1 , x , y );104             }105             else106             {107                 cout << query( 1 , x , y ) << endl;108             }109         }110         cout << endl;111     }112     return 0;113 }
View Code

 

 

today:

  科比 布萊恩特

  無需多言

  老大

 

hdu--4027--不錯的線段樹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.