HDU1754

来源:互联网 发布:java自增id生成策略 编辑:程序博客网 时间:2024/05/29 17:54

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

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

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

#include<stdio.h>#include<cstring>#include<stdlib.h>#include<algorithm>#define N 200005using namespace std;struct Tree{    int l, r, sum,maxn,add;}tree[N<<2];int a[N];//延迟更新 void pushup(int root){//儿子把信息传递给父亲    if(tree[root].l==tree[root].r)return ;    tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;  //<<1:*2; <<1|1:*2+1\    tree[root].maxn=max(tree[root<<1].maxn,tree[root<<1|1].maxn);    return ;} void pushdown(int root){//父亲把自己的信息传给儿子    if(tree[root].l==tree[root].r)return ;    if(tree[root].add==-1)return ;    tree[root<<1].add=tree[root<<1|1].add=tree[root].add;    tree[root<<1].sum=(tree[root<<1].r-tree[root<<1].l+1)*tree[root].add; //长度*父亲.add    tree[root<<1|1].sum=(tree[root<<1|1].r-tree[root<<1|1].l+1)*tree[root].add;    tree[root<<1].maxn=tree[root<<1|1].maxn=tree[root].maxn;    tree[root].add=-1;    return ;}//初始化 void build(int l,int r,int root){    tree[root].l=l;    tree[root].r=r;    tree[root].sum=0;    tree[root].maxn=0;    tree[root].add=-1;    if(l==r){//直到单个区间位置        tree[root].sum=a[l];        tree[root].maxn=a[l];        return;    }    int mid=(l+r)>>1;    build(l,mid,root<<1);//左区间    build(mid+1,r,root<<1|1); //右区间    //pushup(root);//把儿子的信息更新到父亲    tree[root].maxn=max(tree[root<<1].maxn,tree[root<<1|1].maxn);    return;}//更新区间 void update(int l,int r,int z,int root){    if(l==tree[root].l&&tree[root].r==r){        tree[root].sum=(tree[root].r-tree[root].l+1)*z;        tree[root].maxn=z;        tree[root].add=z;//把要更新的内容保存下来,等到要用儿子时                                    再去更新        return ;    }    pushdown(root);//用父亲的信息更新儿子    int mid=tree[root].l+tree[root].r>>1;    if(r<=mid)update(l,r,z,root<<1);    else if(l>mid)update(l,r,z,root<<1|1);    else {        update(l,mid,z,root<<1);        update(mid+1,r,z,root<<1|1);    }    //pushup(root);//更新父亲    tree[root].maxn=max(tree[root<<1].maxn,tree[root<<1|1].maxn);    return ;}//查找区间信息 int query(int l,int r,int root){    if(l<=tree[root].l&&tree[root].r<=r){        return tree[root].maxn;    }    pushdown(root);    int mid=tree[root].l+tree[root].r>>1;    if(r<=mid)return query(l,r,root<<1);    else if(l>mid)return query(l,r,root<<1|1);    else return max(query(l,mid,root<<1),query(mid+1,r,root<<1|1));}int main(){    int t,n,m,cnt=1;    while(~scanf("%d%d",&n,&m))    {        for(int i=1;i<=n;i++)        scanf("%d",&a[i]);        build(1,n,1);        char s[10];        //printf("Case %d:\n",cnt++);        while(m--)        {            scanf("%s",s);            if(s[0]=='U')            {                int i,j;                scanf("%d%d",&i,&j);                //a[i]+=j;                update(i,i,j,1);            }            /*if(s[0]=='')            {                int i,j;                scanf("%d%d",&i,&j);                //a[i]-=j;                update(i,i,-j,1);            }*/            if(s[0]=='Q')            {                int i,j;                scanf("%d%d",&i,&j);                printf("%d\n",query(i,j,1));            }        }    }    return 0;}
原创粉丝点击