#include<bits/stdc++.h> using namespace std; typedef long long ll; #define maxN 200005 int A[maxN]; struct SegTreeNode{ int val; }s[maxN<<2]; //开四倍空间 void pushUp(int rt){ //更新根节点 s[rt].val=max(s[rt<<1].val,s[rt<<1|1].val); } void build(int l,int r,int rt){ //构造线段树 if(l==r){ s[rt].val=A[l]; return; } int m=(l+r)/2; build(l,m,rt<<1); build(m+1,r,rt<<1|1); pushUp(rt); //向上更新 } void update(int L,int c,int l,int r,int rt){ //单点更新 if(l==r){ s[rt].val=c; return; } int m=(l+r)>>1; if(L<=m){ update(L,c,l,m,rt<<1); } else{ update(L,c,m+1,r,rt<<1|1); } pushUp(rt); //向上更新 } int query(int L,int R,int l,int r,int rt){ //区间最值查询 if(L<=l&&r<=R)return s[rt].val; //区间在[L,R]中间 if(L>r||R<l)return 0; //区间在[L,R]之外 int m=(l+r)>>1; int ans=INT_MIN; if(L<=m){ //L在中间值左边 ans=max(ans,query(L,R,l,m,rt<<1)); } if(R>m){ //R在中间值右边 ans=max(ans,query(L,R,m+1,r,rt<<1|1)); } return ans; } int main(){ ios::sync_with_stdio(false); //流加速 int n,m; while(cin>>n>>m){ for(int i=1;i<=n;i++){ cin>>A[i]; } build(1,n,1); while(m--){ int a,b; char c; cin>>c>>a>>b; if(c=='U'){ update(a,b,1,n,1); } else cout<<query(a,b,1,n,1)<<endl; } } return 0; }
HDU 1754 I Hate It (线段树)
最新推荐文章于 2024-09-28 15:28:59 发布