單點更新,區間求最值
#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(int root,int l,int r)
int mid=(l+r)/2;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
push_up(root);
}void update(int root,int pos,int val)
int mid=(tree[root].l+ tree[root].r)/2;
if(pos<=mid)
update(root<<1,pos,val);
else
update(root<<1|1,pos,val);
push_up(root);
}int query(int root,int l,int r)
區間更新,區間求和
#include#include#include#include#include#define n 222222
using namespace std;
int num[n];
struct tree
tree[n*4];
void push_up(int rt)
void push_down(int rt,int m)
}void build(int root,int l,int r)
int mid=(l+r)/2;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
push_up(root);
}void update(int root,int l,int r,int val)
push_down(root,tree[root].r-tree[root].l+1);
int mid=(tree[root].l+tree[root].r)/2;
if (l<=mid)
update(root<<1,l,r,val);
if (r>mid)
update(root<<1|1,l,r,val);
push_up(root);
}long long query(int root,int l,int r)
成段更新,區間求值
#include#include#include#include#include#define n 555555
using namespace std;
const int oo=1e9;
int num[n];
int _min,_max,_sum;
struct tree
big_tree[n*4];
void push_up(int root,tree tree)
void push_down(int root,tree tree)
tree[root].set=-1;
}if (tree[root].add>0)
tree[root].add=0;
}}void build(int root,int l,int r,tree tree)
int mid=(l+r)/2;
build(root<<1,l,mid,tree);
build(root<<1|1,mid+1,r,tree);
push_up(root,tree);
}void update_add(int root,int l,int r,int val,tree tree)
push_down(root,tree);
int mid=(tree[root].l+tree[root].r)/2;
if(l<=mid)
update_add(root<<1,l,r,val,tree);
if (r>mid)
update_add(root<<1|1,l,r,val,tree);
push_up(root,tree);
}void update_set(int root,int l,int r,int val,tree tree)
push_down(root,tree);
int mid=(tree[root].l+tree[root].r)/2;
if(l<=mid)
update_set(root<<1,l,r,val,tree);
if (r>mid)
update_set(root<<1|1,l,r,val,tree);
push_up(root,tree);
}void query(int root,int l,int r,tree tree)
push_down(root,tree);
int mid=(tree[root].l+tree[root].r)/2;
if(l<=mid) query(root<<1,l,r,tree);
if(r>mid) query(root<<1|1,l,r,tree);
}
線段樹模板(模板)
參考部落格 持續更新。外鏈轉存失敗,源站可能有防盜煉機制,建議將儲存下來直接上傳 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 using namespace std define maxn 1000 define inf 0x3f3f3f3...