線段樹入門經典題 HDU1166 HDU1754

2021-09-24 18:07:18 字數 1976 閱讀 5176

今天學習了一下線段樹最簡單的單點的思想,一開始有點難懂,理解了的話就還好。這兩道題本質都差不多,以後有時間可能會更其他線段樹的思路分享,這裡就先放題目和**:

hdu1166:題目描述

更新單點,求區間總和

#include

#include

using namespace std;

const

int maxn=

50005

;int t,n;

int score[maxn*2]

;void

build

(int l,

int r,

int cur)

int mid=

(l+r)/2

;build

(l,mid,cur*2)

;build

(mid+

1,r,cur*2+

1); score[cur]

=score[cur*2]

+score[cur*2+

1];}

void

update

(int i,

int k,

int l,

int r,

int cur)

int mid=

(l+r)/2

;if(i<=mid)

update

(i,k,l,mid,cur*2)

;else

update

(i,k,mid+

1,r,cur*2+

1); score[cur]

=score[cur*2]

+score[cur*2+

1];}

intquery

(int ql,

int qr,

int l,

int r,

int cur)

intmain()

}return0;

}

hdu1754:題目描述

更新單點,求區間最大值

#include

#include

using namespace std;

const

int maxn=

200005

;int n,m;

int score[maxn*2]

;void

build

(int l,

int r,

int cur)

int mid=

(l+r)/2

;build

(l,mid,cur*2)

;build

(mid+

1,r,cur*2+

1); score[cur]

=max

(score[cur*2]

,score[cur*2+

1]);

}void

update

(int i,

int k,

int l,

int r,

int cur)

int mid=

(l+r)/2

;if(i<=mid)

update

(i,k,l,mid,cur*2)

;else

update

(i,k,mid+

1,r,cur*2+

1); score[cur]

=max

(score[cur*2]

,score[cur*2+

1]);

}int

query

(int ql,

int qr,

int l,

int r,

int cur)

intmain()

}return0;

}

hdu1166(敵兵布陣)(線段樹經典題)

題面就不在這裡發了,大家自行去hdoj看題。第一次做線段樹題,套板子很不熟悉。最後還是看了某大牛的 才寫出來。上 include include include includeusing namespace std int a 50010 char str 10 int x,y int n stru...

HDU 1166 敵兵布陣 線段樹入門題

題意 中文題 思路 單點更新,區間求和的線段樹入門題 include include includeusing namespace std const int maxn 50000 5 線段樹需要維護的資訊 int sum maxn 4 define lson i 2,l,m define rson...

初步線段樹 hdu1166

include include includeusing namespace std define max 50000 struct node node maintree 4 max void settree int n,int l,int r int main printf case d n tt...