hdu 1754  I Hate It (線段樹)

來源:互聯網
上載者:User

標籤:des   style   blog   http   java   color   

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

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


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 <stdlib.h>#include <algorithm>#include <iostream>#define  Maxx 200010int str[Maxx],root[Maxx*4];int n,m;int sum=0;using namespace std;void make_tree(int l,int r,int rt){    if(l==r)    {        root[rt]=str[l];        return ;    }    int mid=(l+r)/2;    make_tree(l,mid,rt*2);    make_tree(mid+1,r,rt*2+1);    root[rt]=max(root[rt*2],root[rt*2+1]);}void update(int l,int r,int rt,int a,int b){    if(l == r&&l ==a)    {        root[rt]=b;        return ;    }    int mid=(l+r)/2;    if(a<=mid)    {        update(l,mid,rt*2,a,b);    }    else    {        update(mid+1,r,rt*2+1,a,b);    }    root[rt]=max(root[rt*2],root[rt*2+1]);}void query(int l,int r,int rt,int left,int right){    if(l==left && r==right)    {        sum=max(sum,root[rt]);        return ;    }    int mid=(l+r)/2;    if(left>mid)        query(mid+1,r,rt*2+1,left,right);    else if(right<=mid)        query(l,mid,rt*2,left,right);    else    {        query(l,mid,rt*2,left,mid);        query(mid+1,r,rt*2+1,mid+1,right);    }}int main(){    int i,j;    char str1[2];    int x,y;    while(scanf("%d%d",&n,&m)!=EOF)    {        for(i=1;i<=n;i++)        {            scanf("%d",&str[i]);        }        make_tree(1,n,1);        for(j=1;j<=m;j++)        {            scanf("%s%d%d",str1,&x,&y);            if(str1[0]==‘Q‘)            {                sum=0;                query(1,n,1,x,y);                printf("%d\n",sum);            }            else            {                update(1,n,1,x,y);            }        }    }    return 0;}
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.