線段樹模板

2021-09-07 04:13:07 字數 4115 閱讀 5543

1.無成段更新

#define lson l , m , rt << 1

#define rson m + 1 , r , rt << 1 | 1

const

int maxn = 222222

;int max[maxn<<2

];int min[maxn<<2

];int sum[maxn<<2

];int max(int a,int b)

int min(int a,int b)

void pushup(int

rt)void build(int l,int r,int

rt)

int m = (l + r) >> 1

; build(lson);

build(rson);

pushup(rt);

}void update(int p,int tihuan,int l,int r,int

rt)

int m = (l + r) >> 1

;

if (p <=m) update(p , tihuan ,lson);

else

update(p , tihuan , rson);

pushup(rt);

}void update1(int p,int add,int l,int r,int

rt)

int m = (l + r) >> 1

;

if (p <=m) update1(p , add ,lson);

else

update1(p , add , rson);

pushup(rt);

}int query(int l,int r,int l,int r,int

rt)

int m = (l + r) >> 1

;

int ret = -1

;

if (l <= m) ret =max(ret , query(l , r , lson));

if (r > m) ret =max(ret , query(l , r , rson));

return

ret;

}int query1(int l,int r,int l,int r,int

rt)

int m = (l + r) >> 1

;

int ret = 99999

;

if (l <= m) ret =min(ret , query1(l , r , lson));

if (r > m) ret =min(ret , query1(l , r , rson));

return

ret;

}int queryhe(int l,int r,int l,int r,int

rt)

int m = (l + r) >> 1

;

int ret = 0

;

if (l <= m) ret +=queryhe(l , r , lson);

if (r > m) ret +=queryhe(l , r , rson);

return

ret;}/*

int main()

else if(op[0]=='u') //單點替換

update(a , b , 1 , n , 1);

else if(op[0]=='m')//區間求最小

else if(op[0]=='h')//區間求和

else if(op[0]=='s')//單點增加

else if(op[0]=='e')//單點減少}}

return 0;

}*/

2.成段更新

(區間替換)

#define lson l , m , rt << 1

#define rson m + 1 , r , rt << 1 | 1

const

int maxn =n;

int lazy[maxn<<2

];int sum[maxn<<2

];void pushup(int rt)//

由左孩子、右孩子向上更新父節點

void pushdown(int rt,int m) //

向下更新

}void build(int l,int r,int rt)//

建樹

int m = (l + r) >> 1

; build(lson);

build(rson);

pushup(rt);

}void update(int l,int r,int c,int l,int r,int rt)//

更新 pushdown(rt , r - l + 1

);

int m = (l + r) >> 1

;

if (l <=m) update(l , r , c , lson);

if (r >m) update(l , r , c , rson);

pushup(rt);

}int query(int l,int r,int l,int r,int

rt) pushdown(rt , r - l + 1

);

int m = (l + r) >> 1

;

int ret = 0

;

if (l <= m) ret +=query(l , r , lson);

if (m < r) ret +=query(l , r , rson);

return

ret;}/*

build(1 , n , 1);//n為線段樹最大長度

update(udl , udr , x , 1 , n , 1);//替換線段[udl,udr]為x

int ans = query(ql,qr,1,n,1);//查詢[ql,qr]的區間和

*/

3.(區間增減)o(╯□╰)o 兩行**的差別。

#define lson l , m , rt << 1

#define rson m + 1 , r , rt << 1 | 1

#define ll __int64

const

int maxn = 100100

;ll lazy[maxn

<<2

];ll sum[maxn

<<2

];void putup(int

rt)void putdown(int rt,intm)}

void build(int l,int r,int

rt)

int m = (l + r) >> 1

; build(lson);

build(rson);

putup(rt);

}void update(int l,int r,int c,int l,int r,int

rt) putdown(rt , r - l + 1

);

int m = (l + r) >> 1

;

if (l <=m) update(l , r , c , lson);

if (m putup(rt);

}ll query(

int l,int r,int l,int r,int

rt) putdown(rt , r - l + 1

);

int m = (l + r) >> 1

; ll ret = 0

;

if (l <= m) ret +=query(l , r , lson);

if (m < r) ret +=query(l , r , rson);

return

ret;}/*

int main()

else if(str[0]=='c')

}return 0;

}*/

線段樹模板(模板)

參考部落格 持續更新。外鏈轉存失敗,源站可能有防盜煉機制,建議將儲存下來直接上傳 img xhrgdjcd 1613976863463 區間儲存在陣列中的下標對應為 12 3 4 5 6 7 8 9 10 11 12 13 14 15 四部分單點更新 根據題目的要求編寫自己的pushup,query...

線段樹模板

include include include using namespace std const int size 10010 struct node the node of line tree class linetree void updatem void updateline public ...

線段樹模板

單點更新,區間求最值 include include include include include define n 222222 using namespace std int num n struct tree tree n 4 void push up int root void build...