hdu 1754 線段樹 單點更新 動態區間最大值

I Hate It

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 52417    Accepted Submission(s): 20598 ios

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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define INF 0xfffffff
using namespace std;
int MAXV=-INF;
int MINV=INF;
struct tree
{
    int L;
    int R;
    int maxv;
    int minv;
    int mid()
    {
        return (L+R)/2;
    }
}tree[600010];
void buildtree(int root,int ss,int ee)
{
    tree[root].L=ss;
    tree[root].R=ee;
    tree[root].minv=INF;
    tree[root].maxv=-INF;
    if(ss!=ee)
    {
        buildtree(2*root,ss,(ss+ee)/2);
        buildtree(2*root+1,(ss+ee)/2+1,ee);
    }
}
void insetree(int root ,int i,int s)
{
    if(tree[root].L==tree[root].R)
    {
        tree[root].maxv=s;
        tree[root].minv=s;
        return ;
    }
     tree[root].maxv=max(tree[root].maxv,s);
     tree[root].minv=min(tree[root].minv,s);
     if(i<=tree[root].mid())
     insetree(2*root,i,s);
     else
     insetree(2*root+1,i,s);
     tree[root].maxv=max(tree[2*root].maxv,tree[2*root+1].maxv);//關鍵所在
}
void Query(int root,int ss,int ee)
{
    if(tree[root].minv>=MINV&&tree[root].maxv<=MAXV)
       return;
    if(tree[root].L==ss&&tree[root].R==ee)
    {   //cout<<"1****5"<<endl;
        MAXV=max(tree[root].maxv,MAXV);
        MINV=min(tree[root].minv,MINV);
        return ;
    }
    if(ee<=tree[root].mid())
        Query(2*root,ss,ee);
    else if(ss>tree[root].mid())
        Query(2*root+1,ss,ee);
    else
    {
       // cout<<"1*****4"<<endl;
        Query(2*root,ss,tree[root].mid());
        Query(2*root+1,tree[root].mid()+1,ee);

    }
}
int main()
{
    int n,m;
    int chushi;
    while(scanf("%d %d",&n,&m)!=EOF)
  {
    MAXV=-INF;
    MINV=INF;
    buildtree(1,1,n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&chushi);
        insetree(1,i,chushi);
    }
    int ss,ee;
    char panduan;
    for(int j=1;j<=m;j++)
    {   //scanf("%c",&panduan);
       // getchar();
       // scanf("%d %d",&ss,&ee);
        cin>>panduan>>ss>>ee;
         MAXV=-INF;
         MINV=INF;
        if(panduan=='Q')
        {
            Query(1,ss,ee);
            printf("%d\n",MAXV);
        }
        else
         insetree(1,ss,ee);
    }
  }
    return 0;
}

 

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 9
Hint
Huge input,the C function scanf() will work better than cin
相關文章
相關標籤/搜索