線段樹(模板)

2021-07-16 13:43:53 字數 1857 閱讀 1287

注意對lazy 等標記的清空

區間更新,區間查詢:

struct nodes

node[800040];

int a[200009];

void pushup(int u)

void update(int u,int left,int right,int val);

void pushdown(int n)

}void build (int u,int left,int right)

int mid = (node[u].l + node[u].r)>>1;

build (l(u),left,mid);

build (r(u),mid+1,right);

pushup(u);

}void update(int u,int left,int right,int val)

pushdown(u); //延時更新

int mid = (node[u].l + node[u].r)>>1;

if (right <= mid) update(l(u),left,right,val);

else if (left > mid) update(r(u),left,right,val);

else

pushup(u); //這裡不需要再向上更新,因為我們是從上到下更新的

}long long querysum(int u,int left,int right)

long long querymin(int u,int left,int right)

int main ()

build(1,1,n);

for(int i = 0; i < m; i ++)

if(s[0] == 'm')

if(s[0] == 's')

}return 0;

}

單點更新,區間查詢

struct nodes

node[200200];

int a[50005];

void pushup(int u)

void build (int u,int left,int right)

int mid = (node[u].l + node[u].r)>>1;

build (l(u),left,mid);

build (r(u),mid+1,right);

pushup(u);

}void update(int u,int left,int right,int val)

//if (node[u].add) pushdown(u); //延時更新

int mid = (node[u].l + node[u].r)>>1;

if (right <= mid) update(l(u),left,right,val);

else if (left > mid) update(r(u),left,right,val);

else

pushup(u); //這裡不需要再向上更新,因為我們是從上到下更新的

}int query(int u,int left,int right)

int main ()

build(1,1,n);

while(1)

else if(strcmp(s,"sub") == 0)

else if(strcmp(s,"query" ) == 0)

else}}

return 0;

}

單點更新與區間更新的不同是:單點更新,一次更新到底,不需要lazy標記

線段樹模板(模板)

參考部落格 持續更新。外鏈轉存失敗,源站可能有防盜煉機制,建議將儲存下來直接上傳 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...