HDU_1754_线段树
- 求区间最值.线段树维护
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59#include <iostream>
#include <string.h>
using namespace std;
const int maxn = 2e5+5;
struct segementTree{
int left,right;
int div;
} e[4*maxn];
int a[maxn];
int n,m;
void build(int p,int left,int right) {
e[p].left = left; e[p].right = right;
if (left == right) {
e[p].div = a[left]; return ;
}
int mid = (left + right)/2;
build(2*p,left,mid);
build(2*p+1,mid+1,right);
e[p].div = max(e[p*2].div,e[p*2+1].div);
}
void change(int p,int index,int val) {
if (e[p].left == e[p].right) {
e[p].div = val;
return ;
}
int mid = (e[p].left + e[p].right)/2;
if (index <= mid) change(2*p,index,val);
else change(2*p+1,index,val);
e[p].div = max(e[p*2].div,e[p*2+1].div);
}
int ask(int p,int left,int right) {
if (left <= e[p].left && right >= e[p].right) return e[p].div;
int mid = (e[p].left + e[p].right)/2;
int val = -(1<<30);
if (left <= mid) val = max(ask(2*p,left,right),val);
if (right > mid) val = max(ask(2*p+1,left,right),val);
return val;
}
int main() {
// freopen("a.txt","r",stdin);
while (scanf("%d%d",&n,&m) != EOF) {
memset(a,0,sizeof a);
memset(e,0,sizeof e);
for (int i=1; i<=n; i++) scanf("%d",&a[i]);
build(1,1,n);
for (int i=1; i<=m; i++) {
char ch; int u,v;
scanf("\n%c %d %d",&ch,&u,&v);
if (ch == 'Q') printf("%d\n",ask(1,u,v));
else change(1,u,v);
}
}
return 0;
}
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!