线段树模板 区间更新 区间查询

数据结构专题——线段树_MetalSeed的博客-CSDN博客 

线段树详解 (原理,实现与应用)_岩之痕的博客-CSDN博客_线段树

先推荐两篇大佬的博客,包含了对于线段树的理解、常见题型。

今天学了线段树的区间更新,主要是懒惰标记的应用。

众所周知,世界的进步是由懒人推动的(大雾),那么懒惰标签可以说是线段树的精髓,所谓懒惰就体现在多次更新,一次下推,非必要不更新。至于为啥能把时间复杂度降到log级,我现在也不懂啊啊啊啊。

hdu 1698 Problem - 1698

本题就是线段树的区间更新,单点查询。     

由于最后是问整个钩子的价值,所以直接输出树根节点的值即可。

顺便一提   

这张图真的是时代的眼泪了, 让我想起了小学在网吧看别人玩war3哈哈。

// Problem: Just a Hook
// Contest: HDOJ
// URL: http://acm.hdu.edu.cn/showproblem.php?pid=1698
// Memory Limit: 32 MB
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>

using namespace std;
const int N = 100005;
struct node
{
    int val;
    int lazy;
} segtree[4 * N + 500];
void pushup(int rt)
{
    segtree[rt].val = segtree[rt * 2].val + segtree[rt * 2 + 1].val;
}
void pushdown(int ln, int rn, int rt)
{
    if (segtree[rt].lazy)
    {
        segtree[rt * 2].lazy = segtree[rt].lazy;
        segtree[rt * 2 + 1].lazy = segtree[rt].lazy;
        segtree[rt * 2].val = segtree[rt].lazy * ln;
        segtree[rt * 2 + 1].val = segtree[rt].lazy * rn;
        segtree[rt].lazy = 0;
    }
}
void build(int l, int r, int rt)
{
    segtree[rt].lazy = 0;
    if (l == r)
    {
        segtree[rt].val = 1;
        return;
    }
    int m = (l + r) / 2;
    build(l, m, rt * 2);
    build(m + 1, r, rt * 2 + 1);
    pushup(rt);
}
void update(int L, int R, int add, int l, int r, int rt)
{
    if (L <= l && r <= R)
    {
        segtree[rt].val = add * (r - l + 1);
        segtree[rt].lazy = add;
        return;
    }
    int m = (l + r) / 2;
    pushdown(m - l + 1, r - m, rt);
    if (L <= m)
        update(L, R, add, l, m, rt * 2);
    if (R > m)
        update(L, R, add, m + 1, r, rt * 2 + 1);
    pushup(rt);
}
int query(int L, int R, int l, int r, int rt)
{
    if (L <= l && r <= R)
    {
        return segtree[rt].val;
    }
    if (l > R || r < L)
    {
        return 0;
    }
    int m = (l + r) / 2;
    return query(L, R, l, m, rt * 2) + query(L, R, m + 1, r, rt * 2 + 1);
}
int main()
{
    int t;
    scanf("%d", &t);
    for (int count = 1; count <= t; count++)
    {
        int n;
        scanf("%d", &n);
        int q;
        scanf("%d", &q);
        build(1, n, 1);
        for (int i = 1; i <= q; i++)
        {
            int x, y, z;
            scanf("%d %d %d", &x, &y, &z);
            if (z == 1)
            {
                update(x, y, z, 1, n, 1);
            }
            else if (z == 2)
            {
                update(x, y, z, 1, n, 1);
            }
            else if (z == 3)
            {
                update(x, y, z, 1, n, 1);
            }
        }
        printf("Case %d: The total value of the hook is %d.\n", count, segtree[1]);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值