標籤:
http://acm.hdu.edu.cn/showproblem.php?pid=1754
I Hate It
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 49750 Accepted Submission(s): 19544
Problem 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 Input5 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5 Sample Output5659 HintHuge input,the C function scanf() will work better than cin 更新區間內某一點然後找最大值
#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#define N 200010#define Lson root<<1, L, tree[root].Mid()#define Rson root<<1|1, tree[root].Mid() + 1, Rusing namespace std;struct Tree{ int L, R, Max; int Mid() { return (L + R) / 2; }}tree[N * 4];int al[N];void Build(int root, int L, int R){ tree[root].L = L, tree[root].R = R; if(L == R) { tree[root].Max = al[L]; return ; } Build(Lson); Build(Rson); tree[root].Max = max(tree[root<<1].Max, tree[root<<1|1].Max);}void Update(int root, int k, int e){ if(tree[root].L == tree[root].R) { tree[root].Max = e; return ; } if(k <= tree[root].Mid()) Update(root<<1, k, e); else Update(root<<1|1, k, e); tree[root].Max = max(tree[root<<1].Max, tree[root<<1|1].Max);//更新k點後,還要更新節點的最大值,上一區間節點的最大值取下面左右兩個區間節點的最大值}int Query(int root, int L, int R){ if(tree[root].L == L && tree[root].R == R) return tree[root].Max; if(R <= tree[root].Mid()) return Query(root<<1, L, R); else if(L > tree[root].Mid()) return Query(root<<1|1, L, R); else return max(Query(Lson), Query(Rson));}int main(){ int n, m, a, b, i; char s[10]; while(~scanf("%d%d", &n, &m)) { for(i = 1 ; i <= n ; i++) scanf("%d", &al[i]); Build(1, 1, n); while(m--) { scanf("%s%d%d", s, &a, &b); if(s[0] == ‘U‘) Update(1, a, b); else printf("%d\n", Query(1, a, b)); } } return 0;}/*10 21 2 3 4 5 10 6 7 8 9U 6 8Q 1 6*/
hdu 1754 I Hate It