Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=1754
Analysis && solving: Direct set template, you need to be aware of the C input. (t^t,c++ input Direct T Super 3000,c input Instant 800+, non-human)
1. Segment Tree Standard preprocessing:
#define LC (D<<1)
#define RC (d<<1|1)
#define MID (L+r >>1)
2. Achievements:
const int Max = 200005;
int A[max];
struct Tr
{
int mx;
} TR[MAX*3];
void Push (int root)
{
tr[root].mx = max (tr[lc].mx, tr[rc].mx);
}
void build (int root, int l, int r)
{
if (L = = r)//gets to the bottom child node
{
tr[root].mx = a[l]; Assigns a value directly from the array
retur n;
}
Build (LC, L, mid);
Build (RC, mid+1, R);
Push (root);
}
2. Query Maximum value:
int query (int root, int L, int r, int L, int R)//l,r indicates the query interval
{
if (l==l && r = = r)
{
return tr[root].mx;
}
if (R <= mid)
return query (LC, L, Mid, L, R);
else if (L > Mid)
return query (RC, mid+1, R, L, R);
else
return max (query (LC, L, Mid, L, mid), query (RC, mid+1, R, Mid+1, R));
}
3. Point modification:
void update (int root, int L, int r, int loc, int value)
{
if (L = = r)
{
tr[d]. mx = value;
return;
}
if (Loc <= mid) Update (LC, L, Mid, Loc, k);
else update (RC, mid+1, R, loc, K);
Push (root);
}
AC Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace St
D
#define LC (D<<1) #define RC (d<<1|1) #define MID (L+r >>1) const int Max = 200005;
int A[max]; struct Tr {int mx;}
TR[MAX*3];
void Push (int d) {tr[d].mx = max (tr[lc].mx, tr[rc].mx);}
void build (int d, int l, int r) {if (L = = r) {tr[d].mx = a[l];
return;
} build (LC, L, mid);
Build (RC, mid+1, R);
Push (d); } int query (int d, int l, int r, int l, int r) {if (l==l && r = r) {//cout << ":" &
Lt;<d <<endl;
return tr[d].mx;
} if (R <= mid) return query (LC, L, Mid, L, R);
else if (L > Mid) return query (RC, mid+1, R, L, R);
else return Max (query (LC, L, Mid, L, mid), query (RC, mid+1, R, Mid+1, R)); } void Update (int d, int l, int r, int loc, int k) {if (L = = r) {TR[D].Mx = k;
return;
} if (Loc <= mid) Update (LC, L, Mid, Loc, k);
else update (RC, mid+1, R, loc, K);
Push (d);
} int main () {int n,m;
while (~SCANF ("%d%d", &n,&m)) {memset (tr, 0, sizeof (TR));
for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);
} build (1, 1, N);
Char c[2];
while (m--) {int A, B;
scanf ("%s%d%d", c,&a,&b);
if (c[0]== ' U ') update (1,1,N,A,B);
if (c[0]== ' Q ') printf ("%d\n", Query (1,1,n,a,b));
}
}
}