ps:這題數據太水了, 看以前錯的代碼都能過, 改了下應該是對的了。
#include <stdio.h>
#include <string.h>
#define N 2000002
#define max(a, b) a>b? a:b
int n = 0, m = 0, ans = 0, Y[4*N], num[N];
void update(int c, int l, int r, int k)
{
int m = l+(r-l)/2;
if(l == r)
{
// Y[c] = max(Y[c], num[k]);
Y[c] = num[k];
return ;
}
if(m>=k) update(2*c, l, m, k);
else if(m<k) update(2*c+1, m+1, r, k);
Y[c] = max(Y[2*c], Y[2*c+1]);
}
void query(int c, int l, int r, int lf, int rt)
{
int m = l+(r-l)/2;
if(l == lf && r == rt)
{
ans = max(Y[c], ans);
return ;
}
if(m>=rt) query(2*c, l, m, lf, rt);
else if(m<lf) query(2*c+1, m+1, r, lf, rt);
else
{
query(2*c, l, m, lf, m);
query(2*c+1, m+1, r, m+1, rt);
}
Y[c] = max(Y[2*c], Y[2*c+1]);
}
int main()
{
int i = 0, a = 0, b = 0;
char ch = 0;
while(scanf("%d %d", &n, &m) != EOF)
{
memset(Y, -1, sizeof(Y));
for(i = 1; i<=n; i++)
scanf("%d", num+i);
for(i = 1; i<=n; i++)
update(1, 1, n, i);
getchar();
while(m--)
{
scanf("%c", &ch);
if(ch == 'Q')
{
ans = 0;
scanf("%d %d", &a, &b);
query(1, 1, n, a, b);
printf("%d\n", ans);
}
else
{
scanf("%d %d", &a, &b);
num[a] = b;
update(1, 1, n, a);
}
getchar();
}
}
return 0;
}