線段樹模板

2022-02-05 12:03:31 字數 3047 閱讀 4761

給定乙個包含n個數的序列,初值全為0,現對這個序列有兩種操作:

操作1:把 給定 第k1 個數改為k2;

操作2:查詢 從第k1個數到第k2個數得最大值。(k1<=k2<=n)

n<=100000;所有的數<=100000;

由於這裡的n過大所以構造線段樹。

#include#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using

namespace

std;

inline

intread()

while(ch>='

0'&&ch<='9')

return x*f;

}struct

segment

t[400009

];void build(int p,int l,int

r)

int mid=(l+r)>>1

; build(p

<<1

,l,mid);

build((p

<<1)|1,mid+1

,r);

t[p].v=max(t[p<<1].v,t[(p<<1)|1

].v);

}void change(int p,int x,int

v)

int mid=(t[p].l+t[p].r)>>1

;

if(x<=mid)change(p<<1

,x,v);

else change((p<<1)|1

,x,v);

t[p].v=max(t[p<<1].v,t[(p<<1)|1

].v);

} int ask(int p,int l,int

r)int

n;int

main()

if(x==2

)

}return0;

}

view code

最近遇到了乙個毒瘤的線段樹模板,好模板及其鵝心。

對於這道毒瘤,不清除真難受,思考:乘的時候由於加的懶標記也有可能在,所以考慮先乘後加,就是有乘的話懶標記的加先乘,然後pushdown的時候先乘當前的乘法懶標記,再加上加法懶標記。這樣即可。

#include#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

using

namespace

std;

inline

long

long

read()

while(ch>='

0'&&ch<='9')

return x*f;

}const

long

long maxn=100002

;struct bwy//

聞道玉門猶被遮,應將性命逐輕車

t[maxn

<<2

];long

long

a[maxn],n,m,k;

void build(long

long p,long

long l,long

long

r)

long

long mid=(l+r)>>1

;

if(l<=mid)build(p<<1

,l,mid);

if(r>mid)build(p<<1|1,mid+1

,r);

t[p].sum=t[p<<1].sum+t[p<<1|1

].sum;

t[p].sum%=k;return;}

void pushdown(long

long

p)void change(long

long p,long

long l,long

long r,long

long

d) pushdown(p);

long

long mid=(t[p].l+t[p].r)>>1

;

if(l<=mid)change(p<<1

,l,r,d);

if(r>mid)change(p<<1|1

,l,r,d);

t[p].sum=(t[p<<1].sum+t[p<<1|1].sum)%k;

}void exchange(long

long p,long

long l,long

long r,long

long

d) pushdown(p);

long

long mid=(t[p].l+t[p].r)>>1

;

if(l<=mid)exchange(p<<1

,l,r,d);

if(r>mid)exchange(p<<1|1

,l,r,d);

t[p].sum=(t[p<<1].sum+t[p<<1|1].sum)%k;

}long

long ask(long

long p,long

long l,long

long

r)int

main()

if(u==2)

if(u==3)

}return0;

}

view code

多情自古傷離別,更那堪,冷落清秋節。

線段樹模板(模板)

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