线段树入门题,和敌兵布阵一样只有更新和查询操作,维护的是最大值;
# include <stdio.h> # include <string.h> # define N (1 << 19) # define M (1 << 18) int segMax[N*2+10]; int max(int x, int y) { return x>y ? x:y; } void initTree(void) { segMax[0] = 0; memset(segMax, 0, sizeof(segMax)); } int update(int p, int val) { p += M; segMax[p] = val; while (segMax[p >>= 1] < val) { segMax[p] = val; } } int query(int s, int t) { int ret; ret = 0; s += M-1, t += M+1; while ((s^t) != 1) { if ((s&0x1) == 0) ret = max(ret, segMax[s+1]); if ((t&0x1) == 1) ret = max(ret, segMax[t-1]); s >>= 1, t >>= 1; } return ret; } int main() { char ch[5]; int n, m, i, tmp, x, y; while (~scanf("%d%d", &n, &m)) { initTree(); for (i = 1; i <= n; ++i) { scanf("%d", &tmp); update(i, tmp); } for (i = 0; i < m; ++i) { scanf("%s%d%d", &ch, &x, &y); if (ch[0] == 'Q') { if (x > y) tmp = x, x = y, y = tmp; printf("%d\n", query(x, y)); } else if (ch[0] == 'U') update(x, y); } } return 0; }
//