题目链接:https://vjudge.net/problem/HDU-1754
题目:
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5Sample Output
5 6 5 9
思路:简单修改一下线段树单点修改即可,将求和换成max求最大值即可,更新依旧按左节点右节点一步一步更新
// // Created by HJYL on 2019/9/4. // #include <algorithm> #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <set> #include<math.h> #include<map> using namespace std; const int maxn=1e6+7; int input[maxn]; struct Node{ int left; int right; int maxx; }tree[maxn*4]; void pushup(int root) { tree[root].maxx=max(tree[root<<1].maxx,tree[root<<1|1].maxx); } void build(int root,int l,int r) { tree[root].left=l; tree[root].right=r; if(l==r) { tree[root].maxx=input[l]; return; } int mid=(l+r)>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); pushup(root); } int maxxx; void change(int root,int dis,int k) { if(tree[root].left==tree[root].right) { tree[root].maxx=k; return; } if(tree[root<<1].right>=dis) change(root<<1,dis,k); else change(root<<1|1,dis,k); pushup(root); } int search(int root,int l,int r) { int maxxx=0; if(l<=tree[root].left&&tree[root].right<=r) { return tree[root].maxx; } if(l<=tree[root<<1].right) maxxx=max(maxxx,search(root<<1,l,r)); if(r>=tree[root<<1|1].left) maxxx=max(maxxx,search(root<<1|1,l,r)); return maxxx; } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { for (int i = 1; i <= n; i++) scanf("%d", &input[i]); build(1, 1, n); char ch[10]; // cout<<"m="<<m<<endl; while (m--) { scanf("%s", ch); //getchar(); if (ch[0] == ‘Q‘) { int ll, rr; // cout<<"hhh"<<endl; scanf("%d%d", &ll, &rr); printf("%d\n", search(1, ll, rr)); } else { int p, q; scanf("%d%d", &p, &q); change(1, p, q); } } } return 0; }
I Hate It HDU - 1754(线段树找区间最大值)
原文:https://www.cnblogs.com/Vampire6/p/11462741.html