標籤:des style blog http color os io strong
B - I Hate ItTime Limit:3000MS
Memory Limit:32768KB
64bit IO Format:%I64d & %I64uSubmit Status
Description
很多學校流行一種比較的習慣。老師們很喜歡詢問,從某某到某某當中,分數最高的是多少。
這讓很多學生很反感。
不管你喜不喜歡,現在需要你做的是,就是按照老師的要求,寫一個程式,類比老師的詢問。當然,老師有時候需要更新某位同學的成績。
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 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
Sample Output
5659
Hint
Huge input,the C function scanf() will work better than cin
這道題和HDU1166敵兵布陣差不多同樣也是線段樹入門題,學習如何建立線段樹,更行線段樹,查詢線段樹
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<string> 5 #include<stdlib.h> 6 #include<algorithm> 7 using namespace std; 8 struct node 9 {10 int l,r;11 int num;12 int mid()13 {14 return (l+r)/2;15 }16 }a[1000000];17 int maxn;18 19 void btree(int l,int r,int step)20 {21 a[step].l=l;22 a[step].r=r;23 if(l==r)24 {25 scanf("%d",&a[step].num);26 return ;27 }28 int mid=a[step].mid();29 btree(l,mid,step*2);30 btree(mid+1,r,step*2+1);31 a[step].num=max(a[step*2].num,a[step*2+1].num);32 }33 34 void ptree(int step,int vis,int val)35 {36 if(a[step].l==a[step].r&&a[step].l==vis)37 {38 a[step].num=val;39 return ;40 }41 int mid=a[step].mid();42 if(vis>mid) ptree(step*2+1,vis,val);43 else ptree(step*2,vis,val);44 a[step].num=max(a[step*2].num,a[step*2+1].num);45 }46 47 void maxtree(int l,int r,int step,int L,int R)48 {49 if(L<=l&&r<=R)50 {51 maxn=max(maxn,a[step].num);52 return ;53 }54 int mid=a[step].mid();55 if(L>mid)56 maxtree(mid+1,r,step*2+1,L,R);57 else if(R<=mid)58 maxtree(l,mid,step*2,L,R);59 else60 {61 maxtree(l,mid,step*2,L,R);62 maxtree(mid+1,r,step*2+1,L,R);63 }64 }65 66 int main()67 {68 int n,ope;69 while(scanf("%d %d",&n,&ope)!=EOF)70 {71 btree(1,n,1);72 while(ope--)73 {74 getchar();75 char ch;76 int x,y;77 scanf("%c %d %d",&ch,&x,&y);78 if(ch==‘Q‘)79 {80 maxn=-1;81 maxtree(1,n,1,x,y);82 printf("%d\n",maxn);83 }84 if(ch==‘U‘)85 ptree(1,x,y);86 }87 }88 89 return 0;90 }View Code