線段樹補充模板

2021-10-07 12:34:19 字數 3143 閱讀 7613

這裡主要補充本人遺漏的線段樹模板,沒有說明。

掃瞄線,並面積

const

int n =

1e5+5;

struct node

node (

double a,

double b,

double c,

int d):x

(a),

y1(b),y2

(c),

w(d)

}su[n]

;struct node

tr[n]

;double lisan[n]

;bool

cmp(node a, node b)

bool

_cmp

(double a,

double b)

int mx;

void

build

(int p,

int l,

int r)

void

change

(int p,

int l,

int r,

int w)

int mid =

(tr[p]

.l +tr[p]

.r)>>1;

if(l <= mid)

change

(p<<

1, l, r, w);if

(r > mid)

change

(p<<1|

1, l, r, w);if

(tr[p]

.lu >

0) tr[p]

.sum = lisan[tr[p]

.r+1

]- lisan[tr[p]

.l];

else tr[p]

.sum = tr[p<<1]

.sum + tr[p<<1|

1].sum;

}int

main()

sort

(lisan, lisan+_cnt)

;int m =

unique

(lisan, lisan+_cnt, _cmp)

- lisan;

sort

(su, su+cnt, cmp)

;build(1

,0, _cnt-1)

;double ans =

0.0;

for(

int i =

0; i < cnt-

1; i++

)printf

("test case #%d\n"

, t0++);

printf

("total explored area: %.2f\n\n"

, ans);}

}

掃瞄線,兩次以上交面積

const

int n =

1010

;struct node

node (

double a,

double b,

double c,

int d):x

(a),

y1(b),y2

(c),

w(d)

}su[n<<3]

;struct node

tr[n<<3]

;double lisan[n<<3]

;bool

cmp(node a, node b)

int mx;

void

build

(int p,

int l,

int r)

voidph(

int p)

void

change

(int p,

int l,

int r,

int w)

int mid =

(tr[p]

.l +tr[p]

.r)>>1;

if(l <= mid)

change

(p<<

1, l, r, w);if

(r > mid)

change

(p<<1|

1, l, r, w);ph

(p);

}void

print()

intmain()

sort

(lisan, lisan+_cnt)

;int m =1;

for(

int i =

1; i < _cnt; i ++)if

(lisan[i]

!= lisan[i-1]

) lisan[m++

]= lisan[i]

;// cout << m (su, su+cnt, cmp)

;build(1

,0, m-1)

;double ans =

0.0;

for(

int i =

0; i < cnt-

1; i++

)printf

("%.2f\n"

, ans +

0.000003);

}return0;

}

樹拉取dfs序列

void

dfs(

int x,

int fa)

r[x]

= cnt;

}void

build

(int p,

int l,

int r)

//......

}

序列單次覆蓋權值

序列中,每個元素有乙個權值,有m個覆蓋區間[l, r],k個減少覆蓋的區間[l, r]。乙個區間被覆蓋一次或以上就會貢獻一遍這個權值。否則貢獻值為0。有q次穿插詢問:問現在q這個序列的總權值。

參見掃瞄線,並面積。這裡要注意,不能查詢區間的權值資訊,只能查全序列的權值資訊。

線段樹模板(模板)

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