【HDU1698】 Just a Hook 【线段树入门】

原题:原题链接
题意:(机器翻译的...)
让我们将钩子的连续金属棒从1到N编号。对于每次操作,Pudge可以将连续的金属棒(从X到Y编号)改为铜棒,银棒或金棒。
钩的总值计算为N个金属棒的值的总和。更确切地说,每种棒的值计算如下:

对于每个铜棒,值为1.
对于每个银棒,值为2.
对于每个金棒,值为3.

Pudge想知道执行操作后钩子的总值。
你可能会认为原来的钩子是由铜棒组成的。

【思路】

线段树-区间更新lazy_tag
PS:原来都是1,不是0,因为这个卡了好久

#include<iostream>
#include<cstdio>
#include<cctype>
#include<algorithm>
#define MAXN 100005
using namespace std;

inline int read()
{
    char chr=getchar();
    int f=1,ans=0;
    while(!isdigit(chr)) {if(chr=='-') f=-1;chr=getchar();}
    while(isdigit(chr))  {ans=ans*10;ans+=chr-'0';chr=getchar();}
    return ans*f;

}

struct node{
    int l,r;
    int val,lazy;
    int mid(){
        return l+r>>1;
    }
}t[MAXN<<2];
int a[MAXN];
int n,m;
void push_up(int x){//上传
    t[x].val=t[x<<1].val+t[x<<1|1].val;
}
void push_down(int x){//下传
    if(t[x].lazy){
        t[x<<1].lazy=t[x].lazy;
        t[x<<1|1].lazy=t[x].lazy;//如果是加上一个数的话,要写+=,改成一个数的话直接等于
        t[x<<1].val=(t[x<<1].r-t[x<<1].l+1)*t[x<<1].lazy;
        t[x<<1|1].val=(t[x<<1|1].r-t[x<<1|1].l+1)*t[x<<1|1].lazy;
        t[x].lazy=0;
    }
}
void build(int i,int l,int r){//建树
    t[i].l=l;
    t[i].r=r;
    t[i].lazy=0;
    if(l==r){
        t[i].val=1;//初始铜棒——》 1
        return;
    }
    build(i<<1,t[i].l,t[i].mid());
    build(i<<1|1,t[i].mid()+1,t[i].r);
    push_up(i);
}

void updata(int i,int l,int r,int v){//更新
    if(l<=t[i].l && t[i].r<=r){
        t[i].val=(t[i].r-t[i].l+1)*v;//这里要注意(r-l+1)*v,不是v
        t[i].lazy=v;
        return;
    }
    push_down(i);//记得下传
    int m=t[i].mid();
    if(l<=m) updata(i<<1,l,r,v);
    if(r>m)  updata(i<<1|1,l,r,v);
    push_up(i);//...
}

int query(int i,int l,int r){
    if(l<=t[i].l && t[i].r<=r) return t[i].val;
    push_down(i);//敲黑板:这里不要忘了
    int m=t[i].mid();
    int sum=0;
    if(l<=m) sum+=query(i<<1,l,r);
    if(m<r)  sum+=query(i<<1|1,l,r);
    push_up(i);
    return sum;
}
int T;
int main()
{
    T=read();

    int tot=0;
    while(T--){
        n=read();
        build(1,1,n);
        m=read();
        while(m--){
            int x=read(),y=read(),v=read();
            updata(1,x,y,v);//更新
        }
        printf("Case %d: The total value of the hook is %d.\n",++tot,query(1,1,n));//格式很强
    }
    return 0;
}

原文地址:https://www.cnblogs.com/zhenglw/p/9507891.html

时间: 2024-11-10 14:38:04

【HDU1698】 Just a Hook 【线段树入门】的相关文章

hdu1698 Just a Hook 线段树:成段替换,总区间求和

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 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 consecut

HDU-1698 Just A Hook(线段树)

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 t

【原创】hdu1698 Just a Hook(线段树→区间更新,区间查询)

学习线段树第二天,这道题属于第二简单的线段树,第一简单是单点更新,这个属于区间更新. 区间更新就是lazy思想,我来按照自己浅薄的理解谈谈lazy思想: 就是在数据结构中,树形结构可以线性存储(线性表)也可以树状存储(链表) 树形typedef struct node { int data; struct node* Lchild; struct node* Rchild; }Btree,*BTree;BTree = (BTree)malloc(Btree);好像是这样吧...大半个暑假过去忘得

HDU1698:Just a Hook(线段树区域更新模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1698 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 len

hdu1698 Just a Hook (线段树区间更新 懒惰标记)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 22730    Accepted Submission(s): 11366 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible thing

HDU 1698 Just a Hook 线段树解法

很经典的题目,而且是标准的线段树增加lazy标志的入门题目. 做了好久线段树,果然是practice makes perfect, 这次很畅快,打完一次性AC了. 标志的线段树函数. 主要是: 更新的时候只更新到需要的节点,然后最后的时候一次性把所以节点都更新完毕. 这也是线段树常用的技术. #include <stdio.h> const int SIZE = 100005; struct Node { bool lazy; int metal; }; const int TREESIZE

线段树入门小结

QUE:线段树? 称谓: 从刘汝佳的书中得知,"这种数据结构在学术界没有统一的术语,但线段树是最常见的叫法.其他叫法包括区间树(interval tree).范围树(range tree)等,但这些属于在特定的场合(如计算几何)中有着特殊的意义".怎么叫看读者的心情,以下统一用线段树称呼. 先来作一些了解: 线段树是一棵二叉树,它的左右儿子也都是一棵线段树.(定义) 线段树也叫区间树,为什么叫它区间树呢?因为线段树是一种基于区间的数据结构. 线段树的每个节点代表一个区间 [L,R],其

线段树入门(Billboard)

Billboard Time Limit:8000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description At the entrance to the university, there is a huge rectangular billboard of size h*w (h is its height and w is its width). The board is the place where

Hdu1698Just a Hook线段树区间更新

区间更新基础..不说了,也是照着notonlysuccess的博客撸的. #include <cstdio> #include <cstring> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list>

HDU 1698 Just a Hook (线段树,区间更新)

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17214    Accepted Submission(s): 8600 Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing f