hrbust 1752 Page Rank (单点更新 区间最值)

单点更新 区间最值

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define lson l,m,rt<<1          //函数用左儿子
#define rson m+1,r,rt<<1|1      //函数用右儿子
#define havemid int m=(l+r)>>1  //取节点中点
#define left (rt<<1)            //左儿子
#define right (rt<<1|1)         //右儿子
const int maxn=100005;          //最大节点个数
int Max[maxn<<2];               //节点个数,开maxn的4倍
int id[maxn<<2];
void pushup(int rt){
    if(Max[left]>Max[right]){Max[rt]=Max[left];id[rt]=id[left];}
    else {Max[rt]=Max[right];id[rt]=id[right];}
}
void build(int l,int r,int rt){  //建树(1,n,1)
    if(l==r){                    //跟新到叶子节点 输入值 也为当前节点的和值
        scanf("%d",&Max[rt]);    //返回
        id[rt]=l;
        return ;
    }
    havemid;
    build(lson);                 //递归建树左
    build(rson);                 //右
    pushup(rt);                  //更新到父结点信息 来自左儿子和右儿子
}
void update(int p,int c,int l,int r,int rt){//跟新函数(位置,加几,1,n,1)
    if(l==r){
        Max[rt]=c;return ;
    }
    havemid;
    if(p<=m)update(p,c,lson);  //位置在左儿子里
    else update(p,c,rson);     //右
    pushup(rt);                //加完数要向上更新父节点信息
}
int query(int L,int R,int l,int r,int rt){//查询函数(查询区间的左端点,右,1,n,1)
    if(L<=l&&r<=R)return rt;         //查询区间包括节点区间 return 节点的和值
    havemid;
    int ret=0;
    if(R<=m)return query(L,R,lson);         //左儿子在查询区间
    if(L>m)return query(L,R,rson);          //右
    else
    {
        int a,b;
        a=query(l,m,L,m,rt<<1);
        b=query(m+1,r,m+1,R,rt<<1|1);
        if(Max[a]<Max[b])return b;
        else return a;
    }

}
int main()
{
    int m,n,p,q;
    char s[20];
    while(scanf("%d",&n)!=EOF)
    {

        build(1,n,1);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",s);
            if(s[0]=='C'){scanf("%d%d",&p,&q);update(p,q,1,n,1);}
            else {int res=query(1,n,1,n,1);printf("%d %d\n",id[res],Max[res]);}
        }
        printf("\n");
    }
    return 0;
}


可以省去 query函数

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define lson l,m,rt<<1          //函数用左儿子
#define rson m+1,r,rt<<1|1      //函数用右儿子
#define havemid int m=(l+r)>>1  //取节点中点
#define left (rt<<1)            //左儿子
#define right (rt<<1|1)         //右儿子
const int maxn=100005;          //最大节点个数
int Max[maxn<<2];               //节点个数,开maxn的4倍
int id[maxn<<2];
void pushup(int rt){
    if(Max[left]>Max[right]){Max[rt]=Max[left];id[rt]=id[left];}
    else {Max[rt]=Max[right];id[rt]=id[right];}
}
void build(int l,int r,int rt){  //建树(1,n,1)
    if(l==r){                    //跟新到叶子节点 输入值 也为当前节点的和值
        scanf("%d",&Max[rt]);    //返回
        id[rt]=l;
        return ;
    }
    havemid;
    build(lson);                 //递归建树左
    build(rson);                 //右
    pushup(rt);                  //更新到父结点信息 来自左儿子和右儿子
}
void update(int p,int c,int l,int r,int rt){//跟新函数(位置,加几,1,n,1)
    if(l==r){
        Max[rt]=c;return ;
    }
    havemid;
    if(p<=m)update(p,c,lson);  //位置在左儿子里
    else update(p,c,rson);     //右
    pushup(rt);                //加完数要向上更新父节点信息
}
int main()
{
    int m,n,p,q;
    char s[20];
    while(scanf("%d",&n)!=EOF)
    {

        build(1,n,1);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",s);
            if(s[0]=='C'){scanf("%d%d",&p,&q);update(p,q,1,n,1);}
            else {printf("%d %d\n",id[1],Max[1]);}
        }
        printf("\n");
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值