CF272C Dima and Staircase(线段树/贪心)

123 篇文章 0 订阅
122 篇文章 0 订阅

线段树直接维护区间最大值即可。或者可以发现,最大值只可能出现在最左端或最右端,贪心解决。

线段树

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 100010
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m,a[N];
struct node{
    ll mx,lazy;
}tree[N<<2];
inline void pushup(int p){
    tree[p].mx=max(tree[p<<1].mx,tree[p<<1|1].mx);
}
inline void pushdown(int p){
    if(!tree[p].lazy) return;
    tree[p<<1].lazy=tree[p].lazy;tree[p<<1|1].lazy=tree[p].lazy;
    tree[p<<1].mx=tree[p].lazy;tree[p<<1|1].mx=tree[p].lazy;
    tree[p].lazy=0;
}
inline void build(int p,int l,int r){
    if(l==r){tree[p].mx=a[l];return;}
    int mid=l+r>>1;
    build(p<<1,l,mid);build(p<<1|1,mid+1,r);
    pushup(p);
}
ll qmax(int p,int l,int r,int x,int y){
    if(x<=l&&r<=y) return tree[p].mx;
    int mid=l+r>>1;pushdown(p);ll res=0;
    if(x<=mid) res=max(res,qmax(p<<1,l,mid,x,y));
    if(y>mid) res=max(res,qmax(p<<1|1,mid+1,r,x,y));
    return res;
}
void update(int p,int l,int r,int x,int y,ll val){
    if(x<=l&&r<=y){tree[p].lazy=val;tree[p].mx=val;return;}
    int mid=l+r>>1;pushdown(p);
    if(x<=mid) update(p<<1,l,mid,x,y,val);
    if(y>mid) update(p<<1|1,mid+1,r,x,y,val);
    pushup(p);
}
int main(){
//  freopen("a.in","r",stdin);
    n=read();
    for(int i=1;i<=n;++i) a[i]=read();
    build(1,1,n);m=read();
    while(m--){
        int x=read(),val=read();
        ll res=qmax(1,1,n,1,x);
        printf("%I64d\n",res);
        update(1,1,n,1,x,res+val);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值