首页 > 其他 > 详细

just a hook

时间:2020-02-22 13:17:38      阅读:50      评论:0      收藏:0      [点我收藏+]

 到简单线段树题,首先赋值不是加上,然后下放也是需要的(不会优化,而选择将就)

卑微的我只会做简单题……

#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int tree[400000],lazy[400000];
void push_up(int rt){
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void push_down(int rt,int _size){
    if(lazy[rt]){
        tree[rt<<1]=(_size-(_size>>1))*lazy[rt];
        tree[rt<<1|1]=(_size>>1)*lazy[rt];
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        lazy[rt]=0;
    }
}
void build(int l,int r,int rt){
    lazy[rt]=0;
    if(l==r){
        tree[rt]=1;
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,rt<<1);
    build(mid+1,r,rt<<1|1);
    push_up(rt);
}

void add(int a,int b,int c,int l,int r,int rt){
    if(a<=l&&b>=r){
        tree[rt]=(r-l+1)*c;
        lazy[rt]=c;
        return;
    }
    push_down(rt,r-l+1);
    int mid=(r+l)>>1;
    if(a<=mid)add(a,b,c,l,mid,rt<<1);
    if(b>mid)add(a,b,c,mid+1,r,rt<<1|1);
    push_up(rt);
}

int main(){
    int t;
    cin >> t;
    for(int i=1;i<=t;i++){
        int n,m,a,b,c;
        cin >> n >> m;
        build(1,n,1);
        while(m--){
            cin>>a>>b>>c;
            add(a,b,c,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",i,tree[1]);
    }
    return 0;
}

just a hook

原文:https://www.cnblogs.com/sos3210/p/12344338.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!