Just a Hook

题意:
屠夫的钩子有三种连起来,他可以改变某一段的钩子种类来改变钩子的长度m次。问m次后改变链子的长度。

思路:
这个算是区间赋值吧,在最后在输出整个区间的和,lazy标记和处理完孩子后更新当前节点一个不能少。
因为是赋值,所以lazy标记不要去累加。

Code:

#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
inline ll read() {
    ll s = 0, w = 1;
    char ch = getchar();
    while (ch < 48 || ch > 57) {
        if (ch == '-') w = -1;
        ch = getchar();
    }
    while (ch >= 48 && ch <= 57)
        s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar();
    return s * w;
}
int tree[maxn<<2],rz[maxn<<2];
inline pushdown(int rt,int len) {
    if(rz[rt]) {
        tree[rt<<1]=rz[rt]*(len-(len>>1));
        tree[rt<<1|1]=rz[rt]*(len>>1);
        rz[rt<<1]=rz[rt<<1|1]=rz[rt];
        rz[rt]=0;
    }
}
inline void build(int l,int r,int rt) {
    rz[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);
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
inline void update(int x,int y,int val,int l,int r,int rt) {
    if(x<=l&&y>=r) {
        tree[rt]=val*(r-l+1);
        rz[rt]=val;
        return;
    }
    int mid=l+r>>1;
    pushdown(rt,r-l+1);
    if(x<=mid) update(x,y,val,l,mid,rt<<1);
    if(y>mid) update(x,y,val,mid+1,r,rt<<1|1);
    tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
int main() {
    int t=read(),n,m,x,y,z,cas=0;
    while(t--) {
        n=read(),m=read();
        build(1,n,1);
        while(m--) {
            x=read(),y=read(),z=read();
            update(x,y,z,1,n,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",++cas,tree[1]);
    }
    return 0;
}

线段树入门到进阶 树不能只记得它的样子,要清楚它的性质,比如相邻点的关系

全部评论

相关推荐

10-30 19:23
已编辑
山东大学(威海) C++
牛至超人:我了个雷 1.实习经历写太长了吧,精简一点,你写那么老多,面试官看着都烦 2.项目经历你放俩竞赛干啥单独拿出来写上几等奖就行了呗 3.一大雷点就是项目经历里的那个课程设计,大家都知道课程设计巨水,不要写课程设计,换一个名字,就叫学生管理系统,面试官问就说是自己做的项目,不要提课程设计的事 4.那个交流经历,简化一下塞到最上面的教育经历里就行了 5.简历尽量一页纸
点赞 评论 收藏
分享
10-29 18:20
济南大学 Java
用微笑面对困难:他不是人事吗,怎么净特么不干人事
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务