题目

写了一天的线段树,这道题主要说明一下sum是赋值的,不是累加的,并且在push_down的时候lazy也是赋值的。因可能对懒标记的理解还不是很透彻吧。

#include <iostream>
#include <cstdio>
#include <cstring> using namespace std;
const int Max = 1e5+10;
typedef long long ll;
struct Tree{
int L,R;
ll sum;
int lazy;
} tree[Max<<2]; void PushUp(int x)
{
tree[x].sum = tree[x<<1].sum+tree[x<<1|1].sum;
}
void pushdown(int x,int num)
{
if(tree[x].lazy){
tree[x<<1].sum=tree[x].lazy*(num-(num/2));
tree[x<<1|1].sum=tree[x].lazy*(num/2);
tree[x<<1].lazy = tree[x].lazy;
tree[x<<1|1].lazy=tree[x].lazy;
tree[x].lazy=0;
}
}
void build(int x,int l,int r)
{
tree[x].L=l;
tree[x].R=r;
tree[x].sum=0;
tree[x].lazy=0;
if(l==r){
tree[x].sum=1;return;
}
int mid = (l+r)>>1;
build(x<<1,l,mid);
build(x<<1|1,mid+1,r);
PushUp(x);
}
void update(int x,int l,int r,int val)
{
int L = tree[x].L,R=tree[x].R;
if(l<=L&&R<=r)
{
tree[x].lazy=val;
tree[x].sum=(ll)(R-L+1)*val;
return ;
}
pushdown(x,R-L+1);
int mid = (L+R)>>1;
if(l<=mid)
update(x<<1,l,r,val);
if(mid<r)
update(x<<1|1,l,r,val);
PushUp(x);
}
ll query(int x,int l,int r)
{
ll res = 0;
int L=tree[x].L,R=tree[x].R;
if(l<=L&&r>=R){
return tree[x].sum;
}
pushdown(x,R-L+1);
int mid = (L+R)>>1;
if(l<=mid)
res += query(x<<1,l,r);
if(mid<r)
res += query(x<<1|1,l,r);
return res;
}
int main()
{
int T,n,m,left,right,sum;
scanf("%d",&T);
int cnt = 0;
while(T--)
{
cnt++;
scanf("%d%d",&n,&m);
build(1,1,n);
while(m--)
{
scanf("%d%d%d",&left,&right,&sum);
update(1,left,right,sum);
}
printf("Case %d: The total value of the hook is %lld.\n",cnt,query(1,1,n));
}
return 0;
}

hdu 1698 Just a Hook 【线段树+lazy】的更多相关文章

  1. HDU 1698 Just a Hook (线段树 成段更新 lazy-tag思想)

    题目链接 题意: n个挂钩,q次询问,每个挂钩可能的值为1 2 3,  初始值为1,每次询问 把从x到Y区间内的值改变为z.求最后的总的值. 分析:用val记录这一个区间的值,val == -1表示这 ...

  2. HDU 1698 just a hook 线段树,区间定值,求和

    Just a Hook Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1 ...

  3. [HDU] 1698 Just a Hook [线段树区间替换]

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  4. HDU 1698 Just a Hook 线段树+lazy-target 区间刷新

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  5. HDU 1698 Just a Hook(线段树区间更新查询)

    描述 In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes ...

  6. HDU 1698 Just a Hook(线段树区间替换)

    题目地址:pid=1698">HDU 1698 区间替换裸题.相同利用lazy延迟标记数组,这里仅仅是当lazy下放的时候把以下的lazy也所有改成lazy就好了. 代码例如以下: # ...

  7. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  8. HDU 1698 Just a Hook(线段树成段更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. (简单) HDU 1698 Just a Hook , 线段树+区间更新。

    Description: In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of ...

  10. HDU 1698 Just a Hook 线段树区间更新、

    来谈谈自己对延迟标记(lazy标记)的理解吧. lazy标记的主要作用是尽可能的降低时间复杂度. 这样说吧. 如果你不用lazy标记,那么你对于一个区间更新的话是要对其所有的子区间都更新一次,但如果用 ...

随机推荐

  1. linux通过speedtest-cli测试服务器网速

    1.git clone speedtest源码 git clone https://github.com/sivel/speedtest-cli.git 2.运行speedtest.py cd spe ...

  2. HDU_2112(最短路)

    经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬 ...

  3. maven 单元测试 ( http://www.cnblogs.com/qinpengming/p/5225380.html )

     对junit单元测试的报告:类似这样的结果 ------------------------------------------------------- T E S T S ----------- ...

  4. Latex基本用法

    空格 需要使用 \qquad,\quad,\,应该是占位符和变量之间需要有{}相隔. $$ C_{1} \qquad {C_2} $$ $$ C_{1} \quad {C_2} $$ $$ C_{1} ...

  5. 放大Button热区的方法哟

    //添加图片不能用backgroundimage [btn setImage:image5 forState:]; //然后 btn.imageEdgeInsets = UIEdgeInsetsMak ...

  6. MySQL中varchar与char区别

    MySQL中varchar与char区别(转) MySQL中varchar最大长度是多少? 一. varchar存储规则: 4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字 ...

  7. 52ABP视频学习

    https://study.163.com/course/courseMain.htm?courseId=1005208064 网易视频 https://www.52abp.com/ReadWiki/ ...

  8. 微信小程序获取当前位置

    详细参数说明请看小程序api文档:https://developers.weixin.qq.com/miniprogram/dev/api/wx.openLocation.html wx.getLoc ...

  9. mysql主从配置思路

    记录一下 原文:http://www.rjfw.com.cn/qamain/prevView.action?id=40482017200000031 mysql主从配置(清晰的思路) mysql主从配 ...

  10. NOIP 2017 d2t2 70points

    革命之路漫漫 第一次尝试 40points spfa #include <bits/stdc++.h> #define read read() using namespace std; i ...