HDU 4027 Can you answer these queries(线段树 成段更新)

公文派2025:免费社区版重大安装更新! 距离上一次更新已经过去了将近一年的时间,今天我们带来了全新的免费2025社区版,该版本也是目前最新的版本,无需授权即可使用所有的功能。 阅读详情


Problem Description
A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

Notice that the square root operation should be rounded down to integer.
 

Input
The input contains several test cases, terminated by EOF.
  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263.
  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.
 

Output
For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
 

Sample Input
10 1 2 3 4 5 6 7 8 9 10 5 0 1 10 1 1 10 1 1 5 0 5 8 1 4 8
 

Sample Output
Case #1: 19 7 6
 

题意:给一个区间跟新,不过不同的是把这一个区间的数都开根号,


思路:因为一个数开一定的根号后是1 ,那么就不会再变了,一段区间这样都是一就没必要改变了,所以这样可以节约时间,还有这份代码用G++交超时,c++交过





#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 1000005

struct stud{
int le,ri;
ll len,sum;
}f[N*4];

ll a[N];

inline void pushup(int pos)
{
	f[pos].sum=f[L(pos)].sum+f[R(pos)].sum;
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].len=ri-le+1;
	if(le==ri)
	{
		f[pos].sum=a[le];
		return ;
	}

    int mid=MID(le,ri);
    build(L(pos),le,mid);
    build(R(pos),mid+1,ri);

    pushup(pos);
}

void hello(int pos)       //把这一个区间的数都开根号
{
	if(f[pos].len==f[pos].sum)
		return;

	if(f[pos].le==f[pos].ri)
	{
		f[pos].sum=(ll)(sqrt(f[pos].sum+0.0));
		return ;
	}
    hello(L(pos));
    hello(R(pos));

    pushup(pos);
}

void update(int pos,int le,int ri)
{
    if(f[pos].len==f[pos].sum)
		return ;

	if(f[pos].le==le&&f[pos].ri==ri)
	{
		hello(pos);
		return ;
	}

	int mid=MID(f[pos].le,f[pos].ri);

	if(mid>=ri)
		update(L(pos),le,ri);
	else
		if(mid<le)
		 update(R(pos),le,ri);
	else
		{
			update(L(pos),le,mid);
			update(R(pos),mid+1,ri);
		}
   pushup(pos);
}

ll query(int pos,int le,int ri)
{
	if(f[pos].le==le&&f[pos].ri==ri)
		return f[pos].sum;

	int mid=MID(f[pos].le,f[pos].ri);

	if(mid>=ri)
		return query(L(pos),le,ri);
	else
		if(mid<le)
		return query(R(pos),le,ri);
	else
		return (ll)(query(L(pos),le,mid)+query(R(pos),mid+1,ri));
}

int main()
{
   int i,j,n,m,ca=0;
   while(~sf(n))
   {
   	   fre(i,1,n+1)
   	     scanf("%I64d",&a[i]);

		build(1,1,n);
		sf(m);
		pf("Case #%d:\n",++ca);

		int op,le,ri;

		while(m--)
		{
			sfff(op,le,ri);
			if(le>ri) swap(le,ri);
			if(op)
			   pf("%I64d\n",query(1,le,ri));
			else
			   update(1,le,ri);
		}
		pf("\n");
   }
   return 0;
}






Win11 系统下 VisualStudio2022 配置点云库 PCL1.11.1 安装完成之后打开文件夹E:\PCL 1.11.1\3rdParty\OpenNI2双击 并选择路径 (E:\PCL 1.11.1\3rdParty\OpenNI2) 安装即可全部安装完成之后,将pcl-1.11.1-pdb-msvc2019-win64.zip解压后的 .pdb文件拷贝到(E:\PCL 1.11.1\bin)中设置环境变量:到此,环境变量配置完成。 阅读详情

相关推荐

Flowable第一篇、快速上手(Flowable安装、配置、集成)

Flowable是一个轻量级、高效可扩展的工作流和业务流程管理(BPM)引擎,支持在Java应用程序中实现复杂的工作流自动化。Flowable最初是作为Activiti项目的一个分支发展而来的,但它现在已经成为一个独立的开源项目,并在企业级应用中得到了广泛使用

Du_XiaoNan的博客 1万+

HDU 4027 Can you answer these queries? (线段树成段更新 && 开根操作 && 规律)

题意 :给你N个数以及M个操作,操作分两类,第一种输入 "0 l r" 表示将区间[l,r]里的每个数都开根号。第二种输入"1 l r",表示查询区间[l,r]里所有数的和。 分析 : 不难想到用线段树,但是这里的线段树开根操作的更新很明显不能跟加减操作那样子通过Lazy Tag来实现,那么最笨的方法就是一直更新到叶子节点,不过这也就失去了线段树的高效性,每一次操作都更新到叶子...

134

永磁同步电机功率计算实战:从公式到避坑指南(附Excel计算模板)

本文详细解析永磁同步电机(PMSM)功率计算的实战技巧,涵盖三维参数体系、三相与单相系统差异、动态工况补偿及常见计算陷阱。特别提供Excel计算模板,帮助工程师快速准确完成功率计算,避免工业应用中的常见错误,提升工作效率。

x8y9z0的博客 802

hdu4027 Can you answer these queries?(成段更新)

成段更新,操作为0时对节点值开根号,操作为1时区间求和。 区间求和和上一道题一样,只不过数据量大些。主要是对节点开根号,向下取整直接强制转换就好,还要记得剪枝。因为2^63次大约开方8次就变成了1(比赛时候我要得出这个结论估计还得算半天。。),所以最多到8层就没必要再往下走了。判断条件就是tree[i].l == l && tree[i].r == r && tree[i].value

Iguodala的博客 351

实战指南:用Python实现ONVIF WS-Discovery设备发现(附完整代码)

本文提供了一份详细的实战指南,介绍如何使用Python实现基于ONVIF标准的WS-Discovery设备发现协议。文章深入解析了协议原理、网络通信模型,并提供了完整的代码实现,帮助开发者快速构建智能安防和物联网应用中的设备自动发现工具,有效提升网络设备集成与管理的效率。

chair的专栏 1113

别再死记硬背了!用Multisim仿真5分钟搞懂运放电压跟随器到底要不要加反馈电阻

本文通过Multisim仿真实验,深入解析运放电压跟随器中反馈电阻的必要性。实验对比不同阻值对电路性能的影响,揭示反馈电阻在提供偏置电流通路、提升高频稳定性和ESD保护中的关键作用,并给出工程实践中的优化建议,帮助工程师快速掌握运放电路设计要点。

weixin_42641998的博客 371

HDU-4027-Can you answer these queries

HDU-4027-Can you answer these queries http://acm.hdu.edu.cn/showproblem.php?pid=4027 线段树成段更新,n比较小,更新到每个叶子节点,注意1开根号后仍是1,不需要再更新 #include #include #include #include #include using namespace std; typed

菜鸟的程序人生 680

【电击防护篇】电击防护技术详解:从人体效应到接地系统全解析

本文系统讲解了电击防护的关键知识,包括电流对人体的影响(如30mA是心室颤动阈值)、人体电阻特性(700Ω-6100Ω)以及电气接触防护措施。重点分析了TT、TN和IT三大接地系统的结构特点与应用场景,强调TN-S系统最安全,IT系统供电连续性最佳。详细介绍了漏电保护器(RCD)的工作原理与选型指南,建议人身防护选用30mA高灵敏度RCD。文章通过对比表总结三大系统的安全性、成本及适用场景,为电气安全设计提供实用参考。

2301_81299416的博客 1742

hdu 4027 Can you answer these queries? (线段树+__int64)

http://acm.hdu.edu.cn/showproblem.php?pid=4027 又是纯粹的线段树的基本操作。 (1)数组data[]是纯是浪费,不用开。数组sum[]在 build() 函数下应保证初始化成功(尤其是指先赋值为0,在读入数据)。 (2)if(b>c) {int x=b;b=c;c=x;} 这关乎AC与否的问题。 (3)稍微注意__int64 。 (4...

weixin_30273501的博客 63

生成模型实战 | MuseGAN详解与实现

人工智能音乐生成领域已引起了广泛关注,MuseGAN 是其中一个重要的模型。本节我们将使用 JSB Chorales 数据集来训练 MuseGAN,该数据集包含巴赫创作的四音轨合唱曲。在生成图像时,生成器使用的是来自潜在空间的单一噪声向量来生成不同格式的内容;而在 MuseGAN 中,生成器将使用四个噪声向量来生成一段音乐,旨在增强音乐生成过程中的可控行和多样性。每个噪声向量代表音乐的不同方面,通过单独调整这些向量,模型可以生成更复杂、更细腻的音乐作品。

盼小辉丶的博客 6543

基于Matlab的自动泊车系统开发:利用模糊控制实现智能停车解决方案的详细教程

Matlab是一种广泛应用于科学计算、工程建模与仿真的编程语言,其强大的工具箱和简洁的编程语法,使其成为开发自动泊车系统的理想选择。在开始编写代码之前,我们需要搭建Matlab的工作环境,并熟悉相关的工具箱,如Fuzzy Logic Toolbox,用于实现模糊控制。首先,确保你已经安装了Matlab软件,并加载了必要的工具箱。接下来,我们将逐步讲解如何在Matlab中实现自动泊车系统。

qq_38334677的博客 663

[HDOJ4027]Can you answer these queries?(线段树,特殊成段更新,成段查询)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 RT,该题要求每次更新是更新所有节点,分别求平方根,查询是求和。昨晚思前想后找有没有一个数学上的开平方的和等于和的开平方之类的规律。但是想了想发现这不就是小的时候,如果你这么想那老师就会骂死你的那个- -! 所以显然这个题是无法按套路成段更新了,懒惰标记也是没有用了,我们应该去按照区间更新每...

weixin_30784141的博客 125

科技与伦理的平衡点:人类计算的伦理

科技与伦理的平衡点:人类计算的伦理 关键词 科技与伦理 人类计算 隐私问题 道德决策自动化 公平性与偏见 法律与社会责任 摘要 本

AI天才研究院 1162

HDU 5023 A Corrupt Mayor's Performance Art (线段树)

Problem Description Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this

左眼皮跳跳 1854

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

POJ 3667 Hotel (线段树区间合并 )

Language: Default Hotel Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12417   Accepted: 5346 Description The cows are journeying north to Thunder Bay i

左眼皮跳跳 1778

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

poj 3263 Tallest Cow(线段树)

Language: Default Tallest Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1964   Accepted: 906 Description FJ's N (1 ≤ N ≤ 10,000) cows conveniently

左眼皮跳跳 1670

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

POJ 2182 Lost Cows(牛排序,线段树)

POJ 2182 Lost Cows(牛排序,线段树)

左眼皮跳跳 1213

HDU 2795 Billboard 贴广告(线段树)

HDU 2795 Billboard 贴广告(线段树)

左眼皮跳跳 1201

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

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

左眼皮跳跳 1136

POJ 3264 Balanced Lineup(最大最小差值 线段树水题)

Language: Default Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 37122   Accepted: 17383 Case Time Limit: 2000MS Description For th

左眼皮跳跳 1009

POJ 2777 Count Color(线段树 )

Language: Default Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36180   Accepted: 10927 Description Chosen Problem Solving and Program desi

左眼皮跳跳 1006

poj 3264 Balanced Lineup(简单线段树 或 rmq)

Language: Default Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36833   Accepted: 17252 Case Time Limit: 2000MS Description For th

左眼皮跳跳 997

HDU 1754 I Hate It (线段树单点更新)

HDU 1754 I Hate It (线段树单点更新)

左眼皮跳跳 976

HDU 1541 Stars (线段树)

HDU 1541 Stars (线段树)

左眼皮跳跳 972

POJ 2828 Buy Tickets(线段树)

Language: Default Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 13847   Accepted: 6926 Description Railway tickets were difficult to buy ar

左眼皮跳跳 949

HDU 5316 Magician(线段树)

Magician Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 150    Accepted Submission(s): 42 Problem Description Fantasy magicians u

左眼皮跳跳 946
上一篇: POJ 3264 Balanced Lineup(最大最小差值 线段树水题)
下一篇: CF 205 C Little Elephant and Interval(模拟)
左眼皮跳跳~
博客等级 码龄13年 17粉丝 400原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值