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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98




#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

#define MAXN 200010
#define Lson(x) (x<<1)
#define Rson(x) (Lson(x)|1)
#define Mid(x,y) ((x+y)>>1)


int num[MAXN], n, m;

struct Node{
int left;
int right;
int maxs;
}node[4 * MAXN];

void (int pos)
{
node[pos].maxs = max(node[Lson(pos)].maxs, node[Rson(pos)].maxs);
}

void build_tree(int left, int right, int pos)
{
node[pos].left = left;
node[pos].right = right;
if(left == right){
node[pos].maxs = num[left];
return ;
}
int x = Mid(left, right);
build_tree(left, x, Lson(pos));
build_tree(x + 1, right, Rson(pos));
push_up(pos);
}

void update(int index, int val, int pos)
{
if(node[pos].left == node[pos].right){
node[pos].maxs = val;
//printf("haha : %dn", node[pos].maxs);
return ;
}
int mid = Mid(node[pos].left, node[pos].right);
if(index <= mid)
update(index, val, Lson(pos));
else
update(index, val, Rson(pos));
push_up(pos);
}

int query(int left, int right, int pos)
{
if(node[pos].left == left && node[pos].right == right)
return node[pos].maxs;
int mid = Mid(node[pos].left, node[pos].right);
if(right <= mid)
return query(left, right, Lson(pos));
else if(mid < left)
return query(left, right, Rson(pos));
else
return max(query(left, mid, Lson(pos)), query(mid + 1, right, Rson(pos)));

}

void input()
{
char ch;
int x, y;
for(int i = 1 ; i <= n ; i++)
scanf("%d" , &num[i]);
getchar();
build_tree(1 , n , 1);
while(m--){
scanf("%c%d%d" , &ch , &x , &y);
getchar();
if(ch == 'Q'){
printf("%dn" , query(x , y , 1));
}
else
update(x , y , 1);
}
}

int main()
{
//freopen("input.txt", "r", stdin);
while(scanf("%d%d", &n, &m) != EOF){
input();
}
return 0;
}