【hdu】【线段树入门】I Hate It

来源:互联网 发布:行行 知乎 编辑:程序博客网 时间:2024/05/02 02:42

Problem Description

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

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

Input

本题目包含多组测试,请处理到文件结束。
在每个测试的第一行,有两个正整数 N 和 M ( 0

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

Code

#include <cstdio>#include <cstring>#include <algorithm>#define maxn 200000+100#define lson L,mid,rt<<1#define rson mid+1,R,rt<<1|1#define root L,R,rtusing namespace std;struct node{    int val;}T[maxn<<2];int n,m,a,b,score[maxn];char comm[3];void pushUp(int rt){    T[rt].val=max(T[rt<<1].val,T[rt<<1|1].val);    return;}void build(int L,int R,int rt){    if(L==R){        scanf("%d",&T[rt].val); return;    }    int mid=(L+R)>>1;    build(lson); build(rson);    pushUp(rt);}void update(int L,int R,int rt,int num,int pos){    if(L==R&&pos==L) {    T[rt].val=num; return;}     else{        int mid=(L+R)>>1;        if(pos<=mid) update(lson,num,pos);        else update(rson,num,pos);        pushUp(rt);    }    return;}int query(int l,int r,int L,int R,int rt){    if(l==L&&r==R) return T[rt].val;    int mid=(L+R)>>1;    if(r<=mid) return query(l,mid,lson);    if(l>mid) return query(mid+1,r,rson);    return max(query(l,mid,lson),query(mid+1,r,rson));}int main(){    scanf("%d%d",&n,&m);    //for(int i=1;i<=n;i++) scanf("%d",&score[i]);    build(1,n,1);    while(m--){        scanf("%s",comm);        if(strcmp(comm,"Q")==0){            scanf("%d%d",&a,&b);            printf("%d \n",query(a,b,1,n,1));        }        else{            scanf("%d%d",&a,&b);            update(1,n,1,b,a);        }    }    return 0;}
0 0
原创粉丝点击