本文主要是介绍【hdu】I Hate It(线段树,结点修改求区间最大值),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
线段树的模板题,还是二分递归。
#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);elseUpDate(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】I Hate It(线段树,结点修改求区间最大值)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!