HDU 1754 I Hate It (線段樹 & 樹狀數組),hdu1754

來源:互聯網
上載者:User

HDU 1754 I Hate It (線段樹 & 樹狀數組),hdu1754

I Hate ItTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 39959 Accepted Submission(s): 15863


題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1754


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 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
92014年10月6日 Problem - 1754
http://acm.hdu.edu.cn/showproblem.php?pid=1754 2/2
Hint
Huge input,the C function scanf() will work better than cin
 
 
Author
linle
 
Source
2007省賽集訓隊練習賽(6)_linle專場





題意:單點修改, 求區間最值。



解題思路:很明顯,這題樹狀數組和線段樹都能做,這裡先用線段樹搞吧,樹狀數組的也不難,只要把update和sum改一下就行了,就不給具體代碼了。這題也是基本的線段樹的應用,只要把單點修改,區間求和的update和query稍微改一下就行了。詳見代碼


線段樹基礎知識詳見:線段樹之入門篇



AC代碼:

#include <cstdio>#include <algorithm>using namespace std;#define lson l , m , rt << 1#define rson m + 1 , r , rt << 1 | 1const int maxn = 222222;int MAX[maxn<<2];void PushUP(int rt) {                    //這個也變成了兩者的最大值,而不是求和了    MAX[rt] = max(MAX[rt<<1] , MAX[rt<<1|1]);         // rt<<1|1 是 rt*2+1 的意思}void build(int l,int r,int rt) {    if (l == r) {        scanf("%d",&MAX[rt]);        return ;    }    int m = (l + r) >> 1;    build(lson);    build(rson);    PushUP(rt);}void update(int p,int sc,int l,int r,int rt) {    if (l == r) {        MAX[rt] = sc;                                return ;    }    int m = (l + r) >> 1;    if (p <= m) update(p , sc , lson);    else update(p , sc , rson);    PushUP(rt);}int query(int L,int R,int l,int r,int rt) {    if (L <= l && r <= R) {        return MAX[rt];    }    int m = (l + r) >> 1;    int ret = 0;    if (L <= m) ret = max(ret , query(L , R , lson));             //這裡變成了求兩者的最大值    if (R > m) ret = max(ret , query(L , R , rson));    return ret;}int main() {    int n , m;    while (~scanf("%d%d",&n,&m)) {        build(1 , n , 1);        while (m --) {            char op[2];            int a , b;            scanf("%s%d%d",op,&a,&b);            if (op[0] == 'Q') printf("%d\n",query(a , b , 1 , n , 1));            else update(a , b , 1 , n , 1);        }    }    return 0;}






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.