【HDU 1698】 Just a Hook 线段树 + pushdown 懒惰标记 成段更新

43 篇文章 0 订阅
10 篇文章 0 订阅
博客介绍了如何利用线段树和pushdown技术解决DotA游戏中Pudge的肉钩价值计算问题。通过线段树进行区间更新,并通过lazy标记实现成段更新优化,避免了O(nlogn)的时间复杂度。
摘要由CSDN通过智能技术生成

Problem Description
In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sticks which are of the same length.

Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
You may consider the original hook is made up of cupreous sticks.

Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.

Output
For each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.

Sample Input
1
10
2
1 5 2
5 9 3

Sample Output
Case 1: The total value of the hook is 24.

题意:给定一个初始元素都为1,长度为n的数组,然后每次询问要将指定区间内元素修改成一个特定值,问最后整段元素和

思路(线段树):

· 本来想用树状数组写,毕竟方便点,但是发现要实现成段更新特别麻烦,会使得单次时间复杂度到O(nlogn),只好放弃。
· 线段树的话,肯定要用到pushdown操作,达到优化效果,也就是我们说的成段更新,先不去管子区间。但是这和常规的单点修改或者区间+x不太一样,其实这个还更简单了一点,懒惰标记会覆盖性的更新,不用叠加,当前区间更新到x,直接

a[k].sum = (a[k].r - a[k].l + 1)*x,代表这个区间都是单位为x的元素。标记a[k].lazy = x。

到时执行pushdown的时候也是同样把lazy给变成x即可。

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
#define maxn 500000
using namespace std;
typedef long long ll;
ll n,m;
typedef struct Tree
{
    ll sum;
    ll l;
    ll r;
    ll lazy;
} T;

T  a[maxn];
ll num[maxn];

void update(ll k)
{
    a[k].sum = a[k<<1].sum + a[k<<1|1].sum;
    return ;
}


void build_tree(ll k, ll l, ll r)       //建树,当前结点k(包含区间左右端点),和待建立区间
{
    a[k].l = l;
    a[k].r = r;
    a[k].lazy = 0;
    if(l==r)
    {
        a[k].sum = num[l];
        return ;
    }
    ll mid = (l+r)>>1;      //划分左右子区间
    build_tree(k<<1,  l, mid);      //左结点,负责当前区间的左半
    build_tree(k<<1|1 , mid+1, r);      //同理右半
    update(k);      //建完树更新当前结点,等于左右子区间的和
}

void change(ll k,  ll pos,  ll obj)     //单点修改
{
    if(a[k].l==a[k].r)
    {
        a[k].sum = obj;
        return ;
    }
    ll mid = (a[k].l + a[k].r)>>1;
    if(pos<=mid)  change(k<<1, pos , obj);
    else change(k<<1|1,pos,obj);
    update(k);
}

void pushdown( ll k)        //pushdown操作  本题直接成段修改
{
    if(a[k].l==a[k].r)      //到底了,当前这个结点不用pushdown,并清除标记
    {
        a[k].lazy = 0;
        return ;
    }
    a[k<<1].sum = (a[k<<1].r - a[k<<1].l + 1)*a[k].lazy;       //否则下传记号
    a[k<<1|1].sum = (a[k<<1|1].r - a[k<<1|1].l +1) *a[k].lazy;
    a[k<<1].lazy = a[k].lazy;
    a[ k<<1|1].lazy = a[k].lazy;
    a[k].lazy = 0;      //清空,说明这个点已经下传完毕
}


void changeSegment( ll k,  ll l,  ll r, ll x)       //区间更新
{
    if(a[k].lazy) pushdown(k);      //当前区间若要更新,先下传标记,让子区间先更新
    if(a[k].l>r||a[k].r<l)  return ;            //如果当前要修改区间和本结点表示区间不沾边,直接返回
    if(a[k].l>=l&&a[k].r<=r)            //如果当前区间是查询区间的一部分的话,直接操作并返回
    {
        a[k].sum = (a[k].r-a[k].l+1)*x;            // 直接成段更新
        a[k].lazy = x;              //标签更新
        return ;
    }
    changeSegment(k<<1,l,r,x) ,  changeSegment(k<<1|1,l,r,x);       //进入左右区间查询
    update(k);      //更新
}


ll query( ll k,  ll l,  ll r)       //区间查询
{
    if(a[k].lazy)  pushdown(k);         //先下传标签
    if(a[k].l>r||a[k].r<l)  return 0;       //区间不沾边,说明当前区间贡献为0
    if(a[k].l>=l&&a[k].r<=r)            //如果被包含,直接返回当前的sum
        return a[k].sum;
    return query(k<<1, l, r) + query(k<<1|1,l,r);       //否则进入左右区间
}


int main()
{
    int kase;
    int   cass=1;
    scanf("%d",&kase);
    while(kase--)
    {
        scanf("%lld",&n);
        for(int i=1; i<=n; i++)
        {
            num[i] = 1;
        }
        build_tree(1,1,n);
        scanf("%lld",&m);
        for(int i=1; i<=m; i++)
        {
            ll x,y,val;
            scanf("%lld%lld%lld",&x,&y,&val);
            changeSegment(1,x,y,val);
        }
        printf("Case %d: The total value of the hook is %lld.\n",cass++,query(1,1,n));

    }


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值