1. 程式人生 > >I Hate It HDU - 1754

I Hate It HDU - 1754

cti build 兩個 out urn 學校 線段 ont oid

很多學校流行一種比較的習慣。老師們很喜歡詢問,從某某到某某當中,分數最高的是多少。
這讓很多學生很反感。

不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程序,模擬老師的詢問。當然,老師有時候需要更新某位同學的成績。

Input本題目包含多組測試,請處理到文件結束。
在每個測試的第一行,有兩個正整數 N 和 M ( 0<N<=200000,0<M<5000 ),分別代表學生的數目和操作的數目。
學生ID編號分別從1編到N。
第二行包含N個整數,代表這N個學生的初始成績,其中第i個數代表ID為i的學生的成績。
接下來有M行。每一行有一個字符 C (只取‘Q‘或‘U‘) ,和兩個正整數A,B。
當C為‘Q‘的時候,表示這是一條詢問操作,它詢問ID從A到B(包括A,B)的學生當中,成績最高的是多少。
當C為‘U‘的時候,表示這是一條更新操作,要求把ID為A的學生的成績更改為B。
Output對於每一次詢問操作,在一行裏面輸出最高成績。Sample Input

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

Sample Output

5
6
5
9

Hint

Huge input,the C function scanf() will work better than cin
        
 這題就略微的修改一下 線段樹結構  入門級題目 水題
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace
std; 6 #define maxn 200010 7 int sum[maxn*4+10]; 8 void pushup(int rt) 9 { 10 sum[rt]=max(sum[rt<<1],sum[rt<<1|1]); 11 } 12 void build(int l,int r,int rt) 13 { 14 if (l==r){ 15 scanf("%d",&sum[rt]); 16 return ; 17 } 18 int m=(l+r)>>1; 19 build(l,m,rt<<1
); 20 build(m+1,r,rt<<1|1); 21 pushup(rt); 22 } 23 void updata(int c,int x,int l,int r,int rt) 24 { 25 if (l==r) { 26 sum[rt]=c; 27 return ; 28 } 29 int m=(l+r)>>1; 30 if (x<=m) updata(c,x,l,m,rt<<1); 31 if (x>m) updata(c,x,m+1,r,rt<<1|1); 32 pushup(rt); 33 } 34 int maxs(int x,int y,int l,int r,int rt) 35 { 36 int ans=0; 37 if (x<=l && r<=y ) return sum[rt]; 38 int m=(l+r)>>1; 39 if (x<=m) ans=max(ans,maxs(x,y,l,m,rt<<1)); 40 if (y>m) ans=max(ans,maxs(x,y,m+1,r,rt<<1|1)); 41 return ans; 42 } 43 44 int main() 45 { 46 int n,m; 47 while(scanf("%d%d",&n,&m)!=EOF){ 48 build(1,n,1); 49 char b[2]; 50 int x,y; 51 while(m--){ 52 scanf("%s%d%d",b,&x,&y); 53 if (b[0]==Q) printf("%d\n",maxs(x,y,1,n,1)); 54 else updata(y,x,1,n,1); 55 } 56 } 57 return 0; 58 }



I Hate It HDU - 1754