數列區間最大值 線段樹

2021-10-19 07:02:13 字數 1222 閱讀 8509

acwing 1270

time limit:2s

memory limit:64mb

problem description

輸入一串數字,給你 m 個詢問,每次詢問就給你兩個數字 x,y要求你說出 x 到 y 這段區間內的最大數。

input

output

輸出共 m 行,每行輸出乙個數。

sample input

10 2

3 2 4 5 6 8 1 2 9 7

1 43 8

sample output
5

8

線段樹解法

ac**:

#include

#include

using

namespace std;

int n,m,x[

100005];

//原陣列

struct node

;node tree[

400005];

//線段樹

void

build

(int num,

int l,

int r)

int mid =

(l + r)

>>1;

int lc = num <<1;

//乘2是左子樹

int rc = num <<1|

1;//乘2或1是右子樹

build

(lc,l,mid)

;//建左子樹

build

(rc,mid +

1,r)

;//建右子樹

tree[num]

.l = l,tree[num]

.r = r,tree[num]

.w =

max(tree[lc]

.w,tree[rc]

.w);

return

;//儲存左右子樹的最大子(區間最大)

}int

find

(int num,

int l,

int r)

intmain()

return0;

}

線段樹 Acwing 1270 數列區間最大值

題目 輸入一串數字,給你 m 個詢問,每次詢問就給你兩個數字 x,y,要求你說出 x 到 y 這段區間內的最大數。輸入格式 第一行兩個整數 n,m 表示數字的個數和要詢問的次數 接下來一行為 n 個數 接下來 m 行,每行都有兩個整數 x,y。輸出格式 輸出共 m 行,每行輸出乙個數。資料範圍 1 ...

hdu I hate it 區間最大值 線段樹

include define maxn 200001 define lson l,mid,rt 1 define rson mid 1,r,rt 1 1 int max maxn 2 int n,m int max int a,int b void pushup int rt 要理解好更新的定義,不...

線段樹 區間修改 最大值

最大值 區間修改 總時間限制 10000ms 單個測試點時間限制 1000ms 記憶體限制 65536kb 描述 在n 1 n 100000 個數a1 an組成的序列上進行m 1 m 100000 次操作,操作有兩種 1 1 lr c 表示把a l 到a r 增加c c的絕對值不超過10000 2 ...