線段樹模板

2021-07-11 22:55:23 字數 3383 閱讀 9481

基本用法:

區間求和,求最大值,求最小值

#include#include#include#includeusing namespace std;

const int inf = 0x3f3f3f3f;

const int length = 10010;

int max[length << 2];

int min[length << 2];

int sum[length << 2];

void up(int p) //由左右子樹向上調整

void build(int l, int r, int p) //構建樹

else }

void replace(int pos, int val, int l, int r, int tr)//在 pos 位置將值改為val ,【l,r】表示子樹邊界,tr表示子樹根節點

int mid = (l + r) >> 1;

if (mid >= pos)

else

up(tr);

}void add(int pos, int val, int l, int r, int tr)//在 pos 位置加上 val ,,【l,r】表示子樹邊界,tr表示子樹根節點

int mid = (l + r) >> 1;

if (mid >= pos)

else

up(tr);

}int quemax(int l, int r, int l, int r, int tr)//在區間[l,r]上找出最大值,【l,r】表示子樹邊界,tr表示子樹根節點

int res = quemax(l, r, l, (l+r)>>1, tr << 1);

int ans = quemax(l, r, 1+((l+r)>>1), r, tr << 1 | 1);

return res>ans?res:ans;

}int quemin(int l, int r, int l, int r, int tr)//在區間[l,r]上找出最小值,【l,r】表示子樹邊界,tr表示子樹根節點

int res = quemin(l, r, l, (l+r)>>1, tr << 1);

int ans = quemin(l, r, 1+((l+r)>>1), r, tr << 1 | 1);

return resr)

return 0;

if (l <= l&&r <= r)

int res = quesum(l, r, l, (l+r)>>1, tr << 1);

int ans = quesum(l, r, 1+((l+r)>>1), r, tr << 1 | 1);

return res+ans;

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

//// else

// //}

int main()

void pushdown(int rt, int m) //向下更新

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

int m = (l + r) >> 1;

build(l , m , rt << 1);

build(m+1, r , rt<<1|1);

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, l , m , rt << 1);

if (r > m) update(l, r, c, m+1, r , rt<<1|1);

pushup(rt);

}ll query(int l, int r, int l, int r, int rt)//在【l,r】區間求和,l 和 r 為左右邊界,rt 為根

pushdown(rt, r - l + 1);

int m = (l + r) >> 1;

ll ret = 0;

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

if (m < r) ret += query(l, r, m+1, r , rt<<1|1);

return ret;

}int main()

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

}} return 0;

}

區間增減:

#include #include #include #include #include #define max(a,b) (a>b)?a:b

#define min(a,b) (a>b)?b:a

#define lson l , m , rt << 1

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

#define ll __int64

const int maxn = 100100;

using namespace std;

ll lazy[maxn<<2];

ll sum[maxn<<2];

void putup(int rt)

void putdown(int rt,int m)

}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 < r) update(l , r , c , rson);

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...