【hdu】I Hate It(线段树,结点修改求区间最大值)
2014-09-12 20:17
225 查看
线段树的模板题,还是二分递归。
#include <iostream> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <vector> #include <queue> #include <set> #include <map> #include <stack> #include <algorithm> using namespace std; typedef long long LL; #define MAXD 10 #define maxn 200010 int n; int tree[maxn << 2]; void BuildTree(int l,int r,int pos){ //递归建树 if(l == r){ scanf("%d",&tree[pos]); return ; } int m = (l + r) >> 1; BuildTree(l,m,(pos << 1)); BuildTree(m + 1,r,(pos << 1)|1); tree[pos] = max(tree[pos << 1] , tree[(pos << 1) | 1]); return ; } void UpDate(int aim,int value,int l,int r,int pos){ //目标结点,当前区间长度,当前结点编号 if(l == r){ tree[pos] = value; return ; } int m = (l + r) >> 1; if(aim <= m) UpDate(aim,value,l,m,pos << 1); else UpDate(aim,value,m + 1, r,(pos << 1)|1); tree[pos] = max(tree[pos << 1] , tree[(pos << 1)|1]); return ; } int Query(int L,int R,int l,int r,int pos){ if(L <= l && r <= R) return tree[pos]; int m = (l + r) >> 1; int ans = -1; if(L <= m) ans = max(ans,Query(L,R,l,m,pos << 1)); if(m + 1 <= R) ans = max(ans,Query(L,R,m + 1, r, (pos << 1)|1)); return ans; } int main(){ int m; while(scanf("%d%d",&n,&m) != EOF){ BuildTree(1,n,1); char str[MAXD]; for(int i = 0 ; i < m ; i++){ scanf("%s",str); if(str[0] == 'Q'){ int a,b; scanf("%d%d",&a,&b); int ans = Query(a,b,1,n,1); printf("%d\n",ans); } else if(str[0] == 'U'){ int a,b; scanf("%d%d",&a,&b); UpDate(a,b,1,n,1); } } } return 0; }
相关文章推荐
- HDU-1754 I Hate It (线段树裸题 splay模板 单点修改 区间询问最大值)
- HDU 1754 I Hate It(线段树:单点更新,求区间最大值)
- hdu 1754 I Hate it(线段树的单节点更新,求区间最大值)
- hdu-1754 I Hate It(线段树求区间最大值)
- HDU-1754 I Hate It (线段树 入门题 点修改 区间查询)
- 线段树(区间修改、区间查询) HDU 1754 I Hate It
- HDU 1754 I Hate It(线段树-区间求最值&&单点修改)
- 【线段树I:点修改+区间查询】hdu 1754 I Hate It
- 线段树(区间最大值查询和点修改)——I Hate It ( HDU 1754 )
- HDU 1754 I Hate It(线段树求区间最大值)
- [kuangbin带你飞]专题七 线段树 B - I Hate It(单点修改,区间最大值)
- 【HDU-1754】-I Hate It(线段树,区间最大值)
- hdu 1754 I Hate It(线段树单点更新求区间最大值)
- (HDU 1754)I Hate It 线段树区间查询入门,单点修改
- HDU 1754 I Hate It(线段树 单点更新 区间最大值)
- hdu 1754 I Hate It(线段树,单点更新,区间最值)
- hdu 1754 I Hate It (线段树求区间最值)
- hdu I hate it 区间最大值 线段树
- HDU 1754 I Hate It (线段树 单点替换, 区间最值)
- HDU 1754 I Hate It(线段树--单点赋值, 区间求最大值)