本文共 2296 字,大约阅读时间需要 7 分钟。
1、
这道题目一直Runtime Error(STACK_OVERFLOW),原来是读入Q,U的时候不能只读入一个字符,得把回车也读进来
2、题目大意:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 31258 Accepted Submission(s): 12409
5 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
5659 Hint Huge input,the C function scanf() will work better than cin
3、AC代码:
#include#include using namespace std;#define N 2222220#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int ma[N*4];void updateUp(int rt){ ma[rt]=max(ma[rt<<1],ma[rt<<1|1]);}void build(int l,int r,int rt){ if(l==r) { scanf("%d",&ma[rt]); return ; } int m=(l+r)>>1; build(lson); build(rson); updateUp(rt);}void update(int p,int v,int l,int r,int rt){ if(l==r) { ma[rt]=v; return ; } int m=(l+r)>>1; if(p<=m) update(p,v,lson); else update(p,v,rson); updateUp(rt);}int query(int L,int R,int l,int r,int rt){ if(L<=l && R>=r) return ma[rt]; int m=(l+r)>>1;//这样写错在if(L<=m)调用左孩子,直接返回,底下的if不执行// if(L<=m)// return query(L,R,lson);// if(R>m)// return query(L,R,rson); int ret=0; if(L<=m) ret=max(ret,query(L,R,lson)); if(R>m) ret=max(ret,query(L,R,rson)); return ret;}int main(){ int n,m,b,c; while(scanf("%d%d",&n,&m)!=EOF) { build(1,n,1); for(int i=1; i<=m; i++) { char x[2];//此处包含回车,不能直接用char x来读入 scanf("%s%d%d",x,&b,&c); if(x[0]=='U') { update(b,c,1,n,1); } else if(x[0]=='Q') { printf("%d\n",query(b,c,1,n,1)); } } } return 0;}
转载地址:http://zwcdi.baihongyu.com/