hdu 1754 I Hate It 【線段樹 應用型別二 單點更新 區間最值】【模板】
阿新 • • 發佈:2019-01-11
題目連線:http://acm.hdu.edu.cn/showproblem.php?pid=1754
Problem Description 很多學校流行一種比較的習慣。老師們很喜歡詢問,從某某到某某當中,分數最高的是多少。
Input 本題目包含多組測試,請處理到檔案結束。
Output 對於每一次詢問操作,在一行裡面輸出最高成績。
Sample Input 5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
Sample Output 5 6 5 9 |
思路:在單點更新 區間求和的基礎上面改動query和pushu的相加求和公式改為求最大值就歐克了
程式碼:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=200000+5; #define lson i*2,l,m #define rson i*2+1,m+1,r #define INF 1e9 int maxv[maxn*4]; void PushUP(int i) { maxv[i]=max(maxv[i*2],maxv[i*2+1]); } void build(int i,int l,int r) { if(l==r) { scanf("%d",&maxv[i]); return; } int m=(l+r)/2; build(lson); build(rson); PushUP(i); } int query(int ql,int qr,int i,int l,int r) { if(ql<=l&&qr>=r) return maxv[i]; int m=(l+r)/2; int ans=-INF; if(ql<=m) ans=max(ans,query(ql,qr,lson)); if(m<qr) ans=max(ans,query(ql,qr,rson)); return ans; } void update(int id,int val,int i,int l,int r) { if(l==r) { maxv[i]=val; return; } int m=(r+l)/2; if(id<=m) update(id,val,lson); else update(id,val,rson); PushUP(i); } int main() { int n,t; while(scanf("%d%d",&n,&t)==2&&n&&t) { build(1,1,n); for(int i=1;i<=t;i++) { char s1[5]; int a,b; scanf("%s%d%d",s1,&a,&b); if(s1[0]=='U') { update(a,b,1,1,n); } else if(s1[0]=='Q') { printf("%d\n",query(a,b,1,1,n)); } } } return 0; }