I Hate it-HDU1754

Meaning of the questions:

A more popular habit of many schools. The teachers really like to ask, so and so to so and so from among the highest score is. This allows many students are disgusted. Whether you like it or not,

Now you need to do is, it is in accordance with the requirements of the teacher, to write a program to simulate the teacher asked. Of course, teachers sometimes need to update certain students achievements.

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

Ideas:

The basic point of the segment tree modify the query interval maximum value +

Code:

#include <bits/stdc++.h>
using namespace std;
const int MAXN=2e5+5;
int tree[MAXN<<2];int a[MAXN];
void push_up(int node)
{
    tree[node]=max(tree[node<<1],tree[node<<1|1]);
}
void build(int node,int l,int r)
{
    if(l==r)
    {
        tree[node]=a[l];return;
    }
    int mid=(l+r)>>1;
    build(node<<1,l,mid);
    build(node<<1|1,mid+1,r);
    push_up(node);
}
void update(int node,int l,int r,int i,int k)
{
    int mid=(l+r)>>1;
    if(l==r)
    {
        a[l]=k;tree[node]=k;
        return;
    }
    if(i<=mid)
        update(node<<1,l,mid,i,k);
    else
        update(node<<1|1,mid+1,r,i,k);
    push_up(node);
}
int query(int node,int l,int r,int x,int y)
{
    if(x<=l&&y>=r)
    {
        return tree[node];
    }
    int res=0;
    int mid=(l+r)>>1;
    if(x<=mid)
        res=max(res,query(node<<1,l,mid,x,y));
    if(y>mid)
        res=max(res,query(node<<1|1,mid+1,r,x,y));
    return res;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        build(1,1,n);
        for(int i=1;i<=m;i++)
        {
            char str[3];
            scanf("%s",str);
            int x,y;
            if(str[0]=='Q')
            {
                scanf("%d%d",&x,&y);
                printf("%d\n",query(1,1,n,x,y));
            }
            else
            {
                scanf("%d%d",&x,&y);
                update(1,1,n,x,y);
            }
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/ljxdtc666/p/12222796.html