HDU–1754 — I Hate It [線段樹]

來源:互聯網
上載者:User

 

I Hate It

 

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28044    Accepted Submission(s): 11126

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 
 

 

Code:

 

把結點int變數的意義寫成最大值即可

 

#include<stdio.h>#define M 200005struct node{    int l,r,max;}tree[M*4];int a[M];int max(int a,int b){    return a>b?a:b;}void build(int p,int l,int r){    tree[p].l = l;    tree[p].r = r;    if(l==r){        tree[p].max = a[l];        return;    }    int mid = (l+r)>>1;    int next = p<<1;    build(next,l,mid);    build(next+1,mid+1,r);    tree[p].max = max(tree[next].max,tree[next+1].max);}void update(int p,int side,int val){    if(tree[p].l ==side && tree[p].r == side){        tree[p].max = val;        return;    }    int mid = (tree[p].l + tree[p].r)>>1;    int next = p<<1;    if(side>mid)  update(next+1,side,val);    else  update(next,side,val);    tree[p].max = max(tree[next].max,tree[next+1].max);    }int query(int p,int l,int r){    if(tree[p].l == l && tree[p].r == r)        return tree[p].max;    int mid = (tree[p].l + tree[p].r)>>1;    int next = p<<1;    if(r<=mid) return query(next,l,r);    else if(l>mid) return query(next+1,l,r);    else{        return max(query(next,l,mid),query(next+1,mid+1,r));//這裡如果max用宏定義實現的話會逾時..     }//可能是因為調用忒多了吧.. 用的話函數要拿出來 }int main(){    int n,m,l,r,i;    while(scanf("%d%d",&n,&m)!=EOF)    {        for(i=1;i<=n;i++)            scanf("%d",&a[i]);        build(1,1,n);        for(i=1;i<=m;i++)        {            char c[2];//這裡寫成一個字元的話要在判斷完後加getchar()            scanf("%s",c);            if(c[0]=='Q'){                scanf("%d%d",&l,&r);                printf("%d\n",query(1,l,r));            }            if(c[0]=='U'){                scanf("%d%d",&l,&r);                update(1,l,r);            }        }    }    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.