标签:
Description
Input
Output
Sample Input
Sample Output
Hint
Huge input,the C function scanf() will work better than cin
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 using namespace std; 5 6 int n,m,a[200005],ma[600005],id,ad,ql,qr; 7 8 void build(int o,int l,int r) 9 { 10 if(l==r){ 11 ma[o]=a[l]; 12 return; 13 } 14 int mid=(l+r)/2; 15 build(o*2,l,mid); 16 build(o*2+1,mid+1,r); 17 ma[o]=max(ma[o*2],ma[o*2+1]); 18 } 19 20 void update(int o,int l,int r) 21 { 22 if(l==r){ 23 if(l==id){ 24 a[l]=ad; 25 ma[o]=ad; 26 } 27 return; 28 } 29 int mid=(l+r)/2; 30 if(id<=mid){ 31 update(o*2,l,mid); 32 } 33 else{ 34 update(o*2+1,mid+1,r); 35 } 36 ma[o]=max(ma[o*2],ma[o*2+1]); 37 } 38 39 int quary(int o,int l,int r) 40 { 41 if(ql<=l && qr>=r) 42 return ma[o]; 43 int mid=(l+r)/2; 44 int ans1=0,ans2=0; 45 if(ql<=mid){ 46 ans1=quary(o*2,l,mid); 47 } 48 if(qr>mid){ 49 ans2=quary(o*2+1,mid+1,r); 50 } 51 return max(ans1,ans2); 52 } 53 54 int main() 55 { 56 int i,j,k,x,y; 57 char str[10]; 58 while(scanf("%d %d",&n,&m)!=EOF) 59 { 60 memset(ma,0,sizeof(ma)); 61 for(i=1;i<=n;i++) 62 scanf("%d",&a[i]); 63 build(1,1,n); 64 while(m--) 65 { 66 scanf("%s",str); 67 scanf("%d %d",&x,&y); 68 if(str[0]==‘Q‘) 69 { 70 ql=x,qr=y; 71 printf("%d\n",quary(1,1,n)); 72 } 73 else if(str[0]==‘U‘) 74 { 75 id=x,ad=y; 76 update(1,1,n); 77 } 78 } 79 } 80 return 0; 81 }
标签:
原文地址:http://www.cnblogs.com/cyd308/p/4734664.html