【HDU 1754】 I Hate It
【题目链接】
【算法】
树状数组的最值查询
详见这篇文章 : https://blog.csdn.net/u010598215/article/details/48206959
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXN 200000 int i,N,M,x,y,b; int a[MAXN+10]; char opt; template <typename T> inline void read(T &x) { int f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; } for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } template <typename T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) write(x/10); putchar(x%10+'0'); } template <typename T> inline void writeln(T x) { write(x); puts(""); } struct BinaryIndexedTree { int bit[MAXN+10]; inline int lowbit(int x) { return x & -x; } inline void clear() { int i; for (i = 1; i <= N; i++) bit[i] = 0; } inline void modify(int pos,int val) { int i,j,t; for (i = pos; i <= N; i += lowbit(i)) { bit[i] = a[i]; t = lowbit(i); for (j = 1; j < t; j <<= 1) bit[i] = max(bit[i],bit[i-j]); } } inline int query(int l,int r) { int ret = 0; while (r >= l) { if (r - lowbit(r) < l) { ret = max(ret,a[r]); r--; continue; } while (r - lowbit(r) >= l) { ret = max(ret,bit[r]); r -= lowbit(r); } } return ret; } } BIT; int main() { while (scanf("%d%d",&N,&M) != EOF) { BIT.clear(); for (i = 1; i <= N; i++) { read(a[i]); BIT.modify(i,a[i]); } while (M--) { opt = getchar(); if (opt == 'U') { read(x); read(b); a[x] = b; BIT.modify(x,b); } else { read(x); read(y); writeln(BIT.query(x,y)); } } } return 0; }