开始我写的时候是用链表的,但是一直超内存,就换成树状数组,这题是线段树的一道水题,写了近一天,郁闷,还是写的太少,太菜了,鄙视自己。建树,在查询和更新。 #include <iostream> #include <cstring> using namespace std; struct node { int left,right,num; //node *lch,*rch; }st[600001]; int n,m,d[200001],sum; char s[2]; void bulid(int x,int y,int pos) { st[pos].left=x;st[pos].right=y; if(x==y) {st[pos].num=d[x];return;} else { bulid(x,(x+y)/2,pos*2); bulid((x+y)/2+1,y,pos*2+1); st[pos].num=max(st[pos*2].num,st[pos*2+1].num); } } int ans(int root,int x,int y) { if(st[root].left==x && st[root].right==y) { return st[root].num; } else { int mid=(st[root].left+st[root].right)/2; if(y<=mid) return ans(root*2,x,y); else if(x>=mid+1) return ans(root*2+1,x,y); else return max(ans(root*2,x,mid),ans(root*2+1,mid+1,y)); } } void update(int root,int x,int y) { if(st[root].left==st[root].right) { st[root].num=y;return; } else { int mid=(st[root].left+st[root].right)/2; if(x<=mid) update(root*2,x,y); else update(root*2+1,x,y); } st[root].num=max(st[root*2].num , st[root*2+1].num); } int main() { while(scanf("%d%d",&n,&m)==2) { int i,j,a,b; for(i=1;i<=n;i++) scanf("%d",&d[i]); bulid(1,n,1); for(i=1;i<=m;i++) { scanf("%s%d%d",s,&a,&b); if(s[0]=='Q') printf("%d/n",ans(1,a,b)); else update(1,a,b); } } return 0; }