首页 > 其他分享 >HDU 1698 Just a Hook(线段树)

HDU 1698 Just a Hook(线段树)

时间:2022-10-10 21:06:17浏览次数:60  
标签:rt HDU Just int sum value Hook 更新 include


题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1698

思路:updata()区间替换,query()区间求和

先上3篇博客 1:http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html 

2:http://www.tuicool.com/articles/j6N3eaz 

因为每次都更新到最下面会很费时间,所有有了懒惰标记

就是每次更新不更新到最后..而是更新到包含了区间的最大的节点.

然后如果下次更新的时候更新到了上次已经更新到的节点..

那先把上次更新暂停的节点往下更新..直到这次更新的区间最大的节点没有被标记..

因为没必要都更新只要更新到需要的程度就可以了(查询或者更新节点)

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <climits>
#include <cmath>
#include <cctype>

const int inf = 0x3f3f3f3f;//1061109567
typedef long long ll;
const int maxn = 100010;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
int sum[maxn<<2];
int value[maxn<<2];

void pushup(int rt)
{
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void pushdown(int rt,int m)
{
if(value[rt])
{
value[rt<<1] = value[rt<<1|1] = value[rt];
sum[rt<<1] = (m - (m>>1)) * value[rt];//左节点的数目总是比右节点的数目多一个或者相等
sum[rt<<1|1] = (m>>1) * value[rt];
value[rt] = 0;//增量已经传递了,所以要变为0
}
}
void build(int l,int r,int rt)
{
value[rt] = 0;
sum[rt] = 1;
if(l == r) return;
int m = (l + r) >> 1;
build(lson);
build(rson);
pushup(rt);
}
void updata(int L,int R,int c,int l,int r,int rt)
{
if(l >= L && r <= R)
{
value[rt] = c;
sum[rt] = (r - l + 1) * c;
return;
}
pushdown(rt,r-l+1);//里面的参数要写对
int m = (l + r) >> 1;
if(L <= m) updata(L,R,c,lson);
if(R > m) updata(L,R,c,rson);
pushup(rt);
}
int main()
{
int t,n,m;
scanf("%d",&t);
for(int cas=1; cas<=t; cas++)
{
scanf("%d%d",&n,&m);
build(1,n,1);
for(int i=0; i<m; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
updata(x,y,z,1,n,1);
}
printf("Case %d: The total value of the hook is %d.\n",cas,sum[1]);
}
return 0;
}







标签:rt,HDU,Just,int,sum,value,Hook,更新,include
From: https://blog.51cto.com/u_15740602/5745259

相关文章

  • HDU 5373 The shortest problem(判断一个数能否被11整除)
    题目地址;​​点击打开链接​​思路:参考队友的代码写的,资料地址:​​点击打开链接​​ 怎样判断一个数能不能被11整除?判断一个数能不能被11整除与判断一个数能不能被......
  • 深度探讨react-hooks实现原理
    reacthooks实现Hooks解决了什么问题在React的设计哲学中,简单的来说可以用下面这条公式来表示:UI=f(data)等号的左边时UI代表的最终画出来的界面;等号的右边是......
  • android hook之ELF hook
    android平台的ELFhook技术LD_PRELOADhooklinker程序在对elf可执行程序进行重定位时会根据so库加载的顺序去寻找对应导出符号。利用LD_PRELOAD优先加载自定义的so库并......
  • Adjustable Low Idle Speed on Cummins INSITE Software (CUMMINS Engines)
    AdjustableLowIdleSpeedonCumminsINSITESoftware(CUMMINSEngines)AdjustableLowIdleSpeedThisfeaturecanreducetheamountoffuelburnedanddecreas......
  • HDU7239 Matryoshka Doll (DP)
    题目大意:题目描述.有......
  • hooks
    statehookconst[count,setCount]=useState<number>(10)//必须用setCount更新count,但是setCount是一个异步函数,在频繁地操作时会有一些问题<buttononClick={()......
  • Webhook到底是个啥?
    服务器:​​JenkinsServer​​​​​GitServer​​​​​AppServer​​​关键词:​​​nodejs​​​​​ngrok​​​​​githubwebhook​​在配置Jenkins实现前端自......
  • 【Vue3.x】自定义hooks
    Vue3hooksvue2里的mixins相似,但是mixins会组件的配置项覆盖。vue3使用了自定义hooks替代mixnins,hooks本质上是函数,引入调用。封装自定义的hooks将图片转化成base64im......
  • React+hook+ts+ant design封装一个具有编辑和新增功能的页面
    前言我是歌谣我有个兄弟巅峰的时候排名c站总榜19叫前端小歌谣曾经我花了三年的时间创作了他现在我要用五年的时间超越他今天又是接近兄弟的一天人生难免坎坷大不了从......
  • [BJDCTF2020]JustRE 1
        ......