模板:1.求sum 2.求max 3.求min 4. 更新線段樹值;
**:
#include#includeusing namespace std;
#define l o<<1
#define r (o<<1)|1
struct node
tree[1000<<2];
void pushup(int o)
void build(int o,int l,int r)
int mid = (l+r) >> 1; //找到中間節點
build(o*2 , l , mid); //遞迴建左子樹
build(o*2+1 , mid+1 , r); //遞迴建右子樹
pushup(o); //更新當前節點的值
}void update(int o,int l,int r,int x,int y) //把x節點更新為y
int mid = (l+r) / 2; //找到中間位置
if (x <= mid)
update(o*2,l,mid,x,y); //找左子樹
else
update(o*2+1,mid+1,r,x,y); //找右子樹
pushup(o); //更新當前節點
}int querysum(int o,int l,int r,int x,int y) //查詢x到y的和
int mid = (l + r) / 2;
if (mid >= y) //全在左邊
return querysum(o*2,l,mid,x,y);
else if (x > mid) //全在右邊
return querysum(o*2+1,mid+1,r,x,y);
else //一半在左一半在右
return querysum(o*2,l,mid,x,mid) + querysum(o*2+1,mid+1,r,mid+1,y);
} int querymax(int o,int l,int r,int x,int y) //查詢最大值;
int querymin(int o,int l,int r,int x,int y)//查詢最小值;
int main()
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...