线段树基础题
作者:互联网
一:HDU1754
#include<iostream> #include<algorithm> using namespace std; const int maxn=2e+5; int n,m,a[maxn]; struct tree{ int l,r,v; }trees[maxn<<2]; void buildtree(int s,int l,int r) { trees[s].l=l,trees[s].r=r; if(l==r) { trees[s].v=a[l]; return ; } int mid=l+r>>1; buildtree(s<<1,l,mid); buildtree(s<<1|1,mid+1,r); trees[s].v=max(trees[s<<1].v,trees[s<<1|1].v); } void update_point(int s,int a,int m) { if(trees[s].l==a&&trees[s].r==a) { trees[s].v=m; return ; } int mid=trees[s].l+trees[s].r>>1; if(a<=mid) update_point(s<<1,a,m); if(a>mid) update_point(s<<1|1,a,m); trees[s].v=max(trees[s<<1].v,trees[s<<1|1].v); } int ask_interval(int s,int l,int r) { if(trees[s].l==l&&trees[s].r==r) return trees[s].v; int mid=trees[s].l+trees[s].r>>1; if(r<=mid) return ask_interval(s<<1,l,r); if(l>mid) return ask_interval(s<<1|1,l,r); if(l<=mid&&r>mid) return max(ask_interval(s<<1,l,mid),ask_interval(s<<1|1,mid+1,r)); return 0; } int main() { ios_base::sync_with_stdio(0);cin.tie(0); while(cin>>n>>m){ for(int i=1;i<=n;++i) cin>>a[i]; buildtree(1,1,n); while(m--){ char op;int c,d; cin>>op>>c>>d; if(op=='Q') cout<<ask_interval(1,c,d)<<endl; else update_point(1,c,d); } } return 0; }View Code
标签:return,int,线段,基础,buildtree,maxn,include,op 来源: https://www.cnblogs.com/waryan/p/12240877.html