这也是比较基本的 -。-
hdu1754 I Hate It
题意:O(-1)
思路:O(-1)
线段树功能:update:单点替换 query:区间最值
#include <cstdio> #include <algorithm> using namespace std; #define lson l , m , rt << 1 #define rson m + 1 , r , rt << 1 | 1 const int maxn = 222222; struct Tree{ int value; }tree[maxn<<2]; int max(int va,int vb){ return va>vb?va:vb; } void PushUP(int rt) { tree[rt].value = max(tree[rt<<1].value , tree[rt<<1|1].value); } void build(int l,int r,int rt) { if (l == r) { scanf("%d",&tree[rt].value); return ; } int m = (l + r) >> 1; build(lson); build(rson); PushUP(rt); } void update(int p,int sc,int l,int r,int rt) { if (l == r) { tree[rt].value = sc; return ; } int m = (l + r) >> 1; if (p <= m) update(p , sc , lson); else update(p , sc , rson); PushUP(rt); } int query(int L,int R,int l,int r,int rt) { if (L <= l && r <= R) { return tree[rt].value; } int m = (l + r) >> 1; int ret = 0; if (L <= m) ret = max(ret , query(L , R , lson)); //得到左半段与之前比较的最大值 if (R > m) ret = max(ret , query(L , R , rson)); //得到右半段与之前比较的最大值 return ret; } int main() { int n , m; while (~scanf("%d%d",&n,&m)) { build(1 , n , 1); while (m --) { char op[2]; int a , b; scanf("%s%d%d",op,&a,&b); if (op[0] == 'Q') printf("%d\n",query(a , b , 1 , n , 1)); else update(a , b , 1 , n , 1); } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
today lazy . tomorrow die .