标签:acm算法
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
5 6 5 9HintHuge input,the C function scanf() will work better than cin
#include <iostream> #include <algorithm> #include <cstdio> #include <cmath> #include <cstring> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 int MAX[2222222<<2]; void pushup(int rt) { MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]); } void build(int l,int r,int rt) { if(r==l) { scanf("%d",&MAX[rt]); return; } int m=(r+l)>>1; build(lson); build(rson); pushup(rt); } void update(int a,int b,int l,int r,int rt) { if(l==r) { MAX[rt]=b; return; } int m=(l+r)>>1; if(a<=m) update(a,b,lson); else update(a,b,rson); pushup(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l &&r<=R) { return MAX[rt]; } int res=0; int m=(r+l)>>1; if(L<=m) res=max(res,query(L,R,lson)); if(R>m) res=max(res,query(L,R,rson)); return res; } int main() { int n,m; int a,b; while(~scanf("%d%d",&n,&m)) { build(1,n,1); char s[2]; while(m--) { scanf("%s",s); scanf("%d%d",&a,&b); if(s[0]=='Q') printf("%d\n",query(a,b,1,n,1)); else update(a,b,1,n,1); } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:acm算法
原文地址:http://blog.csdn.net/sky_miange/article/details/47053381