hdu 1698 Just a Hook (区间更新)

题意:胖子有一条大jb,大JB由n个小JB组成,每次操作将一个区间的小JB变成金银铜三者之一,最后取出所有区间的JB总价值

思路:和刷气球差不多意思了,简单的区间更新,无需更新到叶子节点,防止超时

#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#define INF 0x3f3f3f3f
#define maxn 1000100
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long ll;
int ans[maxn],len;
struct node
{
    int l,r,n;
}tree[maxn*4];

/*void pushup(int pos)
{
    tree[pos].n = max(tree[pos<<1].n,tree[pos<<1|1].n);
}
void pushdown(int pos)
{
    if(!tree[pos].lazy)
        return;
    tree[pos<<1].sum += tree[pos].lazy-tree[pos<<1].n;
    tree[pos<<1|1].sum += tree[pos].lazy;
    tree[pos<<1].lazy = tree[pos].lazy;
    tree[pos<<1|1].lazy = tree[pos].lazy;
    tree[pos].n = tree[pos].lazy;
    tree[pos].lazy = 0;
}*/

void build(int l,int r,int pos)
{
    tree[pos].l = l;
    tree[pos].r = r;
    tree[pos].n = 1;
    //if(l == r)
    //{
        //scanf("%lld",&tree[pos].sum);
        //return;
    //}
    if(l != r)
    {
        int mid = (l+r)/2;
        build(l,mid,pos<<1);
        build(mid+1,r,pos<<1|1);
    }
}

void update(int l,int r,int k,int pos)
{
    if(tree[pos].n == k)
        return;
    if(tree[pos].l == l && tree[pos].r == r)
    {
        tree[pos].n = k;
        return ;
    }
    if(tree[pos].n != -1)
    {
        tree[pos<<1].n = tree[pos<<1|1].n = tree[pos].n;
        tree[pos].n = -1;
    }
    int mid = (tree[pos].l + tree[pos].r) >> 1;
    if(r <= mid)
        update(l,r,k,pos<<1);
    else if(l > mid)
        update(l,r,k,pos<<1|1);
    else
    {
        update(l,mid,k,pos<<1);
        update(mid+1,r,k,pos<<1|1);
    }
//    pushup(pos);
}                                         //自上而下更新节点

/*int query(int l,int r,int pos)
{
    if(l == tree[pos].l && r == tree[pos].r)
        return tree[pos].n;
    pushdown(pos);
    int mid = (tree[pos].l+tree[pos].r)>>1;
    if(r<=mid)
        return query(l,r,pos<<1);
    else if(l>mid)
        return query(l,r,pos<<1|1);
    else
        return max(query(l,mid,pos<<1),query(mid+1,r,pos<<1|1));
}*/
int Find(int pos)
{
    if(tree[pos].n != -1)
        return (tree[pos].r - tree[pos].l + 1) * tree[pos].n;
    else
        return Find(pos<<1) + Find(pos<<1|1);
}

int main()
{
    int t,n,m,C = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        int a,b,c;
        build(1,n,1);
        for(int i = 1; i <= m; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            update(a,b,c,1);
        }
        printf("Case %d: The total value of the hook is %d.\n",C++,Find(1));
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值