HDU 1754----I Hate It

Description

很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。

不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

Input

本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0 学生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
9

Hint

Huge input,the C function scanf() will work better than cin

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
long long x,y,i,j,m,n,c[200001];
char b;
struct node
{
    long long l,r,w,flag;
    int dis()
    {
        return r-l+1;
    }
} a[800004];
long long max(long long p,long long q)
{
    if(p>q)
        return p;
    return q;
}
void build(long long k,long long l,long long r)
{
    a[k].l=l;
    a[k].r=r;
    a[k].w=0;
    a[k].flag=0;
    long long mid=(a[k].r+a[k].l)/2;
    if(l==r)
    {
        a[k].w=c[l];
        return;
    }
    build(k*2,l,mid);
    build(k*2+1,mid+1,r);
    a[k].w=max(a[k*2].w,a[k*2+1].w);
}
void zengjia(long long k,long long l,long long w)
{
    if(a[k].l>=l&&a[k].r<=l)
    {
        a[k].w=w;
        return;
    }
    long long mid=(a[k].r+a[k].l)/2;
    if(mid>=l)
        zengjia(k*2,l,w);
    if(mid<l)
        zengjia(k*2+1,l,w);
    a[k].w=max(a[k*2].w,a[k*2+1].w);
}
long long xunwen(long long k,long long l,long long r)
{
    if(a[k].l>=l&&a[k].r<=r)
        return a[k].w;
    long long sum=0;
    long long mid=(a[k].r+a[k].l)/2;
    if(mid>=l)
        sum=max(sum,xunwen(k*2,l,r));
    if(mid<r)
        sum=max(sum,xunwen(k*2+1,l,r));
    return sum;
}
int main()
{
    long long jieguo;
    while(~scanf("%lld%lld",&m,&n))
    {
        memset(c,0,sizeof(c));
        for(i=1; i<=m; i++)
            scanf("%lld",&c[i]);
        build(1,1,m);
        while(n--)
        {
            getchar();
            scanf("%c%lld%lld",&b,&x,&y);
            if(b=='Q')
            {
                jieguo=xunwen(1,x,y);
                printf("%lld\n",jieguo);
            }
            if(b=='U')
                zengjia(1,x,y);
        }
    }
}

你可能感兴趣的:(HDU 1754----I Hate It)