hdu4027_Can you answer these queries?

Unity 2021 LTS安装避坑指南:从下载到第一个3D项目的完整流程 本文提供了一份详尽的Unity 2021 LTS安装与首个3D项目创建指南。内容涵盖从Unity Hub下载、版本选择、模块安装避坑,到编辑器界面解析、创建旋转立方体脚本的完整流程,旨在帮助初学者快速搭建稳定开发环境并上手核心操作。 阅读详情

http://acm.hdu.edu.cn/showproblem.php?pid=4027


题目是标准的线段树,由于要对区间内的每个数进行开方操作,所以每次update都要update到叶子节点那里。
这样很明显没有体现线段树的lazy update,稍微分析也可知道必定TLE。。。

改进:
首先,题目中要求开方后向下取整。然后,由于sum最大不超过2^63,所以每个数最多开方7次,等到变成1的时候,数值就不会再变化了,也就是,如果某个区间内所有数都变成1了,那么就不需要再update了。
最后,注意,x有可能大于y的。。。若无注意到此处,会RE滴~


代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

typedef long long ll;

const int N = 100000 + 10;
struct SegTree
{
	int left, right;
	ll sum;
	bool allone;  /**这里是重点,标记是否区间内全为1**/
};
SegTree st[N*3];
ll num[N];

void build(int left, int right, int idx)
{
	st[idx].left = left;
	st[idx].right = right;
	if (left == right)
	{
	    st[idx].sum = num[left];
	    if (st[idx].sum == 1)
            st[idx].allone = true;
        else st[idx].allone = false;
	    return;
	}
	int mid = (left + right) / 2;
    build(left, mid, idx*2);
    build(mid+1, right, idx*2+1);
    st[idx].sum = st[idx*2].sum + st[idx*2+1].sum;
    if (st[idx*2].allone && st[idx*2+1].allone)
        st[idx].allone = true;
    else st[idx].allone = false;
}

void update(int left, int right, int idx)
{
	if (st[idx].left == st[idx].right)
	{
	    st[idx].sum = (ll)sqrt(st[idx].sum * 1.0);
	    if (st[idx].sum == 1)
            st[idx].allone = true;
		return;
	}
	if (st[idx].allone && st[idx].left <= left && right <= st[idx].right)
        return;
	int mid = (st[idx].left + st[idx].right) / 2;
	if (right <= mid)
        update(left, right, idx*2);
    else if (left > mid)
        update(left, right, idx*2+1);
    else
    {
        update(left, mid, idx*2);
        update(mid+1, right, idx*2+1);
    }
	st[idx].sum = st[idx*2].sum + st[idx*2+1].sum;
    if (st[idx*2].allone && st[idx*2+1].allone)
        st[idx].allone = true;
}

ll query(int left, int right, int idx)
{
	if (st[idx].left == left && st[idx].right == right)
		return st[idx].sum;
	int mid = (st[idx].left + st[idx].right) / 2;
	if (left > mid)
		return query(left, right, idx*2+1);
	else if (right <= mid)
		return query(left, right, idx*2);
	else return query(left, mid, idx*2) + query(mid+1, right, idx*2+1);
}

int main()
{
//    freopen("in.txt", "r", stdin);

	int n, m, k = 1;
	while (scanf("%d", &n) != EOF)
	{
		printf("Case #%d:\n", k++);
		for (int i = 1; i <= n; i++)
			scanf("%I64d", &num[i]);
		build(1, n, 1);
		int t, a, b;
		scanf("%d", &m);
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d%d", &t, &a, &b);
			if (a > b)
                swap(a, b);
			if (t == 0)
                    update(a, b, 1);
            else printf("%I64d\n", query(a, b, 1));
		}
		printf("\n");
	}
	return 0;
}


Vue - 简约大气 404 页面 效果图 实现代码 复制即可。 <template> <div> <div class="error"> <!-- 图片可保存到本地 --> <img src="https://img-blog.csdnimg.cn/20210715111039922.png"> <p>抱歉!你访问的页面不存在或已删除 :( </p> <span class="back" @click="go( 阅读详情

相关推荐

远心F-theta镜ZEMAX设计

根据给出的F-theta镜的设计指标,对F-theta镜进行了具体的光学设计。从对F-theta镜设计思想的介绍出发,对选取的初始结构进行了优化设计,优化完成后,对所设计的F-theta镜进行了像质评价,通过点列图、场曲与畸变图、相对照度、能量集中度对所设计的F-theta镜进行了分析,最终,对F-theta镜的设计效果进行了验证。

hitliuyi17的博客 9442

Can you answer these queries? HDU - 4027

Can you answer these querites? HDU - 4027 普通的线段树题,但是有一个问题是,区间更新时,因为必须更新每个点,才能更新区间,那么线段树更新就很慢了,无法使用lazy数组。有一个小技巧是当区间和等于区间长度时,那么说明已经到最好的情况了,不用再修改了。这一步简化后就可以过了 如果写线段树可以像喝水一样自然就好了 1 #include <...

weixin_30886233的博客 124

从硬件异常到代码救赎:RH850内存访问错误(MAE)的深层诊断与艺术

本文深入探讨瑞萨RH850微控制器的内存访问错误(MAE)诊断与解决方法。针对程序跑飞和Flash Driver交互中的潜在陷阱,提供系统性硬件异常分析框架和防御性编程实践,帮助嵌入式开发者从底层机制到代码层面有效预防和修复MAE问题,提升系统稳定性。

pytorch8learner的博客 197

hdu 4027 Can you answer these queries?

hdu 4027 Can you answer these queries?         线段树 #include #include #include #define MAX 400000 struct node { int left, right; __int64 val; int flag; }; node tree[MAX]; void buildTree(int

happen_zhang 677

别再让会议变‘回音壁’:手把手教你调优WebRTC的AEC3回声消除(附实战代码)

本文深入解析WebRTC的AEC3回声消除技术,提供从参数调优到声学环境适配的实战指南。通过诊断回声特征、解剖核心参数(如filter.length_blocks和delay_estimate_smoothing)以及应对特殊场景(如密闭空间与开放办公区),帮助开发者有效解决视频会议中的回声问题,提升语音通信质量。附有实战代码和调试工具,适合需要优化实时通信体验的开发者。

weixin_29220405的博客 178

人工智能100问☞第5问:什么是AI三要素?

人工智能的核心三要素是​​数据、算法和算力​​,分别作为AI的“燃料”、“大脑”和“动力”,三者协同驱动智能系统的学习、推理与决策能力。

bjdx_001的博客 1998

HDU 4027 Can you answer these queries?

题目链接:Problem - 4027 (hdu.edu.cn) 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 atta

m0_55682843的博客 300

Python连接高斯数据库踩坑实录:从SASL报错到SSL安全连接的完整指南

本文详细记录了Python连接高斯数据库时遇到的SASL authentication报错及其解决方法。通过剖析SASL认证机制,对比了修改数据库配置、使用官方驱动等方案的利弊,并重点提供了通过自动化脚本生成SSL证书,实现安全、免密连接的完整指南,帮助开发者彻底解决连接难题。

weixin_28340315的博客 442

HDU - 4027 Can you answer these queries? (线段树)

HDU - 4027 Can you answer these queries? 题 意:给你n个数字,q个操作。每次操作输入x,l,r。x= 0 ,代表把al到ar的值全部都开根号。x=1,代表求al到ar的和。 数据范围: 1&amp;amp;lt;=M,N&amp;amp;lt;=1e5 输入样例: 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...

子灬丶逾 242

uniapp - 微信支付报错 {“errMsg“:“requestPayment:fail:[payment微信:-1]General errors“} 详细解决办法,uniApp使用微信支付时报错

uniapp,微信支付报错,uniapp微信小程序 H5网页 安卓苹果ios真机调试时报错,uniapp真机测试微信支付报错如何解决,调用微信支付出现报错{"errMsg":"requestPayment:fail:[payment微信:-1]General errors"},微信支付提示错误,uniapp进行微信支付时报错怎么解决,如何解决微信支付报错,uniapp微信支付报错fail,uni-app真机测试ios上报错,uniapp真机测试安卓手机第一次支付成功第二次就报错了,uniapp微信支付调用第

✅ 精选推荐文章 4134

基于STM32L4XX、HAL库的ESP32-S3-WROOM-1-N4 WIFI模块驱动程序设计

/ 检查是否收到完整的一行(以\r\n结尾)// 例如: 解析AT命令响应或MQTT消息等。// UART2初始化已在CubeMX中配置。// USART2初始化函数(由CubeMX生成)// 缓冲区溢出,重置。// 如果需要等待响应。// 处理接收到的数据。// 处理接收到的数据。// 等待ESP32模块启动。// 主循环其他任务。// 启用UART全局中断。// 发送HTTP GET请求。// 发送AT测试命令。// 连接Wi-Fi。// UART中断回调函数。

zhusl6688的博客 290

hdu 4027 Can you answer these queries?

http://acm.hdu.edu.cn/showproblem.php?pid=4027 Can you answer these queries? Time Limit:2000MSMemory Limit:65768KB64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 4027 ...

deyou1988的专栏 72

OpenClaw实战系列01:OpenClaw接入飞书机器人全接入指南 + Ollama本地大模型

通过 OpenClaw + Ollama + 飞书的组合,我们成功搭建了一个完全私有、免费、可定制的AI助理。它不仅能在日常聊天中提供帮助,还能通过OpenClaw强大的“技能”体系,真正去操作你的电脑、管理文件、甚至执行代码。这套方案将数据留在本地,既保证了隐私安全,又锻炼了动手能力。现在,去给你的AI助理添加更多酷炫的技能吧!

PeterPan的博客 1万+

HDU4027 Can you answer these queries?

思路:这道题很坑。 区间开根号加区间求和,开根号,该怎么开,每个点开肯定超时,然后自己拿计算机算了算,long long之内的数据大概开7次根号就成为1了,那先一个一个开根号,然后再剪枝,和等于区间个数时直接return,这样10w个数据开70w次根号,,我运行了一下,只需要3ms就算出来了,所以肯定不会超时。 不过这道题很坑,它输入的l可能会比r大,另外卡cin,cin会tle,取消同步就6...

Endeavor_G的博客 237

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

hdu 4027 Can you answer these queries? 线段树

 hdu 4027 Can you answer these queries?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 b...

GYH0730的博客 620

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Can you answer these queries?( HDU - 4027,线段树 + 细节)

一.题目链接: HDU-4027 二.题目大意: n 个数. 有两种操作. 0 X Y:将区间 [X, Y] 的数开方(向下取整) 1 X Y:查询区间 [X, Y] 的和. 三.分析: 直接线段树单点更新会 TLE,由于根号 7 次后向下取整为 1. 所以只需要记录每段区间被更新过的次数. 注意:所给的 X,Y 大小关系不确定因为这里 WA 了三天啊啊啊啊啊啊 555....

The___Flash的博客 288

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Can you answer these queries? HDU 4027 线段树

Can you answer these queries? HDU 4027 线段树 题意 是说有从1到编号的船,每个船都有自己战斗值,然后我方有一个秘密武器,可以使得从一段编号内的船的战斗值变为原来值开根号下的值。有两种操作,第一种就是上面描述的那种,第二种就是询问某个区间内的船的战斗值的总和。 解题思路 使用线段树就不用多说了,关键是如果不优化的话会超时,因为每次修改都是需要递归...

weixin_30715523的博客 175

HDU-4027-Can you answer these queries?(暴力线段树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 题目大意:n个数,两种操作,0.将[l,r]内的所有数开平方(四舍五入取ll),1.求[l,r]内的sum 思路:线段树板子,首先需要更新到最下面,1开平方还是1,所以一个数最多更新64次,然后记录一下是否需要开平发即可。 ACCode: #include<stdlib.h&gt...

永远鲜红の幼月的博客 325

HDU4027-Can you answer these queries?

http://acm.hdu.edu.cn/showproblem.php?pid=4027 #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define lson l,m,rt<<1 #define rson m+1,r,r...

weixin_30851867的博客 116
上一篇: poj2228_Naptime
下一篇: poj3622_Gourmet Grazers
xjwJava
博客等级 码龄16年 19粉丝 48原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值