【CodeForces - 635D】Factory Repairs(线段树)

题意:机器好的时候一天完成a件工作,坏的时候一天完成b件工作,开始为坏的,修机器需要k天,q个询问,每次给两个操作,1,在d天新来a间工作,2在p天开始修机器,修机器时不能完成工作。对于每个2操作输出n天总共能完成多少工作。

思路:

线段树的每个叶结点维护两个数,修理前和修理后,这段区间的几天能做的工作的总和,叶节点我处理的比较麻烦,应该有更好的方式。注意细节

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<stack>
#include<map>
#include<unordered_map>
#include<set>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
const int mod=1e9+7;
const int maxn=200100;
struct node{
    int l,r;ll va,vb;
}tr[maxn<<2];
int n,k,a,b,q,d,p,op;
void pushup(int cur){
    tr[cur].va=0;//初始化
    tr[cur].vb=0;
    if(tr[cur<<1].l==tr[cur<<1].r){
        if(tr[cur<<1].va>=0)tr[cur].va+=a;
        else tr[cur].va+=a+tr[cur<<1].va;
        if(tr[cur<<1].vb>=0)tr[cur].vb+=b;
        else tr[cur].vb+=b+tr[cur<<1].vb;
    }
    else{
        tr[cur].va+=tr[cur<<1].va;
        tr[cur].vb+=tr[cur<<1].vb;
    }
    if(tr[cur<<1|1].l==tr[cur<<1|1].r){
        if(tr[cur<<1|1].va>=0)tr[cur].va+=a;
        else tr[cur].va+=a+tr[cur<<1|1].va;
        if(tr[cur<<1|1].vb>=0)tr[cur].vb+=b;
        else tr[cur].vb+=b+tr[cur<<1|1].vb;
    }
    else{
        tr[cur].vb+=tr[cur<<1|1].vb;
        tr[cur].va+=tr[cur<<1|1].va;//少写了一半
    }
}
void build(int l,int r,int cur){
    int mid=(l+r)>>1;
    tr[cur].l=l;
    tr[cur].r=r;
    tr[cur].va=0;
    tr[cur].vb=0;
    if(l==r){
        tr[cur].va=-a;
        tr[cur].vb=-b;
        return ;
    }
    build(l,mid,cur<<1);
    build(mid+1,r,cur<<1|1);
    pushup(cur);
}
void update(int tar,int val,int cur){
    if(tr[cur].l==tr[cur].r){
        tr[cur].va+=val;
        tr[cur].vb+=val;
        return ;
    }
    if(tr[cur<<1].r>=tar)
    update(tar,val,cur<<1);
    if(tr[cur<<1|1].l<=tar)
    update(tar,val,cur<<1|1);
    pushup(cur);
}
ll query(int pl,int pr,int op,int cur){
    ll res=0;
    if(pl<=tr[cur].l&&pr>=tr[cur].r){
        if(op==0){
            if(tr[cur].l==tr[cur].r) return tr[cur].vb>=0?b:b+tr[cur].vb;
            else return tr[cur].vb;
        }
        else{
            if(tr[cur].l==tr[cur].r) return tr[cur].va>=0?a:a+tr[cur].va;
            else return tr[cur].va;
        }
    }
    if(pl<=tr[cur<<1].r) res+=query(pl,pr,op,cur<<1);
    if(pr>=tr[cur<<1|1].l) res+=query(pl,pr,op,cur<<1|1);
    return res;
}
int main(){
    scanf("%d%d%d%d%d",&n,&k,&a,&b,&q);
    build(1,n,1);
    while(q--){
        scanf("%d",&op);
        if(op==1){
            scanf("%d%d",&d,&p);
            update(d,p,1);
        }
        else {
            scanf("%d",&p);
            ll res=0;
            if(p-1>=1) res+=query(1,p-1,0,1);
            if(p+k<=n) res+=query(p+k,n,1,1);
           printf("%lld\n",res);
        }
    }


    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值