P1531 I Hate It 树状数组解法
P1531 I Hate It
区间最大值,区间查询最大值,单点更新,本解使用树状数组求解
#include#include #include using namespace std; const int MAXN = 3e5; int a[MAXN], h[MAXN]; int n, m; int lowbit(int x) { return x & (-x); } void updata(int x) { int lx, i; while (x <= n) { h[x] = a[x]; lx = lowbit(x); for (i=1; i = x) { ans = max(a[y], ans); y --; for (; y-lowbit(y) >= x; y -= lowbit(y)) ans = max(h[y], ans); } return ans; } int main() { int i, j, x, y, ans; char c[10]; scanf("%d %d",&n,&m); for (i=1; i<=n; i++) h[i] = 0; for (i=1; i<=n; i++) { scanf("%d",&a[i]); updata(i); } for (i=1; i<=m; i++) { cin>>c; if (c[0]== 'Q') { scanf("%d %d",&x,&y); ans = query(x, y); printf("%d\n",ans); } else if (c[0] == 'U') { scanf("%d %d",&x,&y); if (a[x]