每個節點代表區間
唯一根節點,也就是全部區間
葉節點是長度為1的子區間,也就是所代表陣列上的乙個點
const
int maxn =
1e3;
struct segmenttree e[
4* maxn]
;
建樹
void
build
(int p,
int l,
int r)
int mid =
(l+r)/2
;build
(p*2
,l,mid)
;//遞迴建左子樹
build
(p*2+1
,mid+
1,r)
;//右子樹
e[p]
.dat =
max(e[p*2]
.dat,e[p*2+
1].dat)
;//從下到上(回溯)更新區間節點的最大值
}
修改某節點值
void
change
(int p,
int index,
int val)
int mid =
(e[p]
.l + e[p]
.r)/2;
if(index <= mid)
change
(p*2
,index,val)
;else
change
(p*2+1
,index,val)
; e[p]
.dat =
max(e[p*2]
.dat,e[p*2+
1].dat)
;}
詢問區間最值
int
ask(
int p,
int l,
int r)
hdu 1166
#include
#include
using
namespace std;
const
int maxn =
2e5+5;
int a[maxn]
;int n;
struct segmenttree e[
4* maxn]
;void
build
(int p,
int l,
int r)
int mid =
(l+r)/2
;build
(p*2
, l , mid)
;//遞迴建左子樹
build
(p*2+1
,mid+
1,r)
;//右子樹
e[p]
.dat = e[p*2]
.dat + e[p*2+
1].dat;
//從下到上(回溯)更新區間節點的最大值
}void
change
(int p,
int index,
int val)
int mid =
(e[p]
.l + e[p]
.r)/2;
if(index <= mid)
change
(p*2
,index,val)
;else
change
(p*2+1
,index,val)
; e[p]
.dat = e[p*2]
.dat + e[p*2+
1].dat;
}int
ask(
int p,
int l,
int r)
intmain()
}return0;
}
#include
using namespace std;
using ll =
long
long
;const
int maxn =
4e5+10;
int a[maxn/4]
,left_[maxn]
,right_[maxn]
;ll add[maxn]
,sum[maxn]
;void
pushup
(int p)
void
pushdown
(int p)
}void
build
(int p,
int l,
int r)
int mid =
(l + r)
>>1;
build
(p<<
1,l,mid)
;build
(p<<1|
1,mid+
1,r)
;pushup
(p);
}void
change
(int p,
int l,
int r,
int val)
// 要向下繼續找,則下傳標記
pushdown
(p);
int mid =
(left_[p]
+ right_[p]
)>>1;
if(l <= mid)
change
(p<<
1,l,r,val);if
(mid < r)
change
(p<<1|
1,l,r,val)
;pushup
(p);
}ll query
(int p,
int l,
int r)
int n,m;
intmain()
(); cin >> n >> m;
for(
int i=
1; i<=n;
++i)
cin >> a[i]
;build(1
,1,n);
while
(m--
)else
}return0;
}
bzoj 1798 線段樹 雙lazy標記
time limit 30 sec memory limit 64 mb submit 2645 solved 984 submit status 老師交給小可可乙個維護數列的任務,現在小可可希望你來幫他完成。有長為n的數列,不妨設為a1,a2,an 有如下三種操作形式 1 把數列中的一段數全部乘乙...
專題 線段樹 E 區間更新,使用lazy標記
include include include include using namespace std const int maxn 100010 typedef long long ll int lazy 3 maxn 懶惰標記陣列 int num maxn struct node segtree...
hdu1698 線段樹區間更新,lazy標記
題意 一段線段由n條小線段組成,每次操作把乙個區間的小線段變成金銀銅之一 金的價值為3,銀為2,銅為1 最初可當做全為銅 最後求這條線段的總價值。void build int l,int r,int rt int m l r 1 build lson build rson pushup rt 下面是...