hdu 1754 I Hate It(線段樹之 單點更新+區間最值),hdu1754

來源:互聯網
上載者:User

hdu 1754 I Hate It(線段樹之 單點更新+區間最值),hdu1754
I Hate It                                                                            Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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 61 2 3 4 5Q 1 5U 3 6Q 3 4Q 4 5U 2 9Q 1 5
 
Sample Output
5659HintHuge input,the C function scanf() will work better than cin 
 線段樹入門題目。最近做多校聯合的比賽,發現每一場比賽都有涉及到線段樹的題目,但是我之前沒有學過線段樹,於是決定學一下。
#include<cstdio>#include<algorithm>using namespace std;#define lson l, mid, root<<1#define rson mid+1, r, root<<1|1const int N = 2000000 + 50;struct node{    int l, r, mmax;}a[4*N];void build_tree(int l, int r, int root){    a[root].l = l;    a[root].r = r;    if(l == r)    {        scanf("%d",&a[root].mmax);        return ;    }    int mid = (l + r) >> 1;    build_tree(lson);    build_tree(rson);    a[root].mmax = max(a[root<<1].mmax, a[root<<1|1].mmax);}void update(int l, int r, int root, int k){    if(l == a[root].l && r == a[root].r)    {        a[root].mmax = k;        return;    }    int mid = (a[root].l + a[root].r) >> 1;    if(r <= mid)        update(l, r, root<<1, k);    else if(l > mid)        update(l, r, root<<1|1, k);    else    {        update(lson, k);        update(rson, k);    }    a[root].mmax = max(a[root<<1].mmax, a[root<<1|1].mmax); //更新完單個點之後更新整棵樹}int Query(int l, int r, int root){    if(l == a[root].l && r == a[root].r)        return a[root].mmax;    int mid = (a[root].l + a[root].r) >> 1;    if(r <= mid)        return Query(l, r, root<<1);    else if(l > mid)        return Query(l, r, root<<1|1);    else        return max(Query(lson), Query(rson));}int main(){    int n, m, a, b;    char ch[5];    while(~scanf("%d%d",&n,&m))    {        build_tree(1, n, 1);        while(m--)        {            getchar();            scanf("%s%d%d",ch, &a, &b);            if(ch[0] == 'Q')                printf("%d\n",Query(a, b, 1));            else                update(a, a, 1, b);        }    }    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.