關於線段樹的update

來源:互聯網
上載者:User
下面這種方法對於區間兩端如果數值一樣的話,是無法update到那個區間的.if ( nod[t].lf == e.y1 && nod[t].rf == e.y2 )     nod[t].cnt += e.f;else {   if ( e.y2 <= nod[t*2].rf )        update ( t * 2, e );   else if ( e.y1 >= nod[t*2+1].lf )   update ( t * 2 + 1, e );   else    {        Line tmp = e;        tmp.y2 = nod[t*2].rf;update ( t * 2, tmp );        tmp = e;tmp.y1 = nod[t*2+1].lf;update ( t * 2 + 1, tmp );   }}當然,另一個比較通用的方法是if ( e.y1 <= nod[t].lf && nod[t].rf <= e.y2 )    nod[t].cnt += e.f;else if ( nod[t].l + 1 != nod[t].r ) {   int m = ( nod[t].l + nod[t].r ) / 2;   if ( e.y1 < y[m] )   update ( t * 2, e );   if ( y[m] < e.y2 )   update ( t * 2 + 1, e );}這樣即使數值一樣,也可以。突然想到,貌似之前幾道矩形並交的題都是使用上面的那種方法,貌似都沒有出問題,其實是因為求矩形面積並時如果有端點一樣的情況,訪問到那裡其數值也是為0,因為長度為0,而對於這題,由於需要判斷端點是否重複覆蓋,故一定要訪問到,在端點有一樣的情況下,第一種方法就不適用了。以上轉自http://hi.baidu.com/phoenixwright/blog/item/72a8bbcf6ce07531b600c8e4.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.