SPOJ 2713 线段树(sqrt)
题意:
给你n个数(n <= 100000),然后两种操作,0 x y :把x-y的数全都sqrt ,1 x y:输出 x-y的和。
思路:
直接线段树更新就行了,对于当前的这个区间,如果里面只有0或者1,那么就把他mark上,以后不用在更新了,10^18 更新不了多少次就会变成0或者1的,所以时间复杂度也没多少,每次更新能返回就返回,不能返回就一定要精确到点,然后sqrt,然后查询的话就是一个简单的段询问。
#include<stdio.h>
#include<string.h>
#include<math.h>
#define lson l ,mid ,t << 1
#define rson mid + 1 ,r ,t << 1 | 1
long long sum[440000];
long long mark[440000];
void Pushup(int t)
{
sum[t] = sum[t<<1] + sum[t<<1|1];
mark[t] = (mark[t<<1] & mark[t<<1|1]);
}
void BuidTree(int l ,int r ,int t)
{
mark[t] = sum[t] = 0;
if(l == r)
{
scanf("%lld" ,&sum[t]);
if(sum[t] == 0 || sum[t] == 1)
mark[t] = 1;
return;
}
int mid = (l + r) >> 1;
BuidTree(lson);
BuidTree(rson);
Pushup(t);
return;
}
void Update(int l ,int r ,int t ,int a ,int b)
{
if(mark[t]) return;
if(l == r)
{
sum[t] = (long long)sqrt(sum[t]*1.0);
if(sum[t] == 1 || sum[t] == 0) mark[t] = 1;
return;
}
int mid = (l + r) >> 1;
if(a <= mid) Update(lson ,a ,b);
if(b > mid) Update(rson ,a ,b);
Pushup(t);
}
long long Query(int l ,int r ,int t ,int a ,int b)
{
if(a <= l && b >= r) return sum[t];
int mid = (l + r) >> 1;
long long Ans = 0;
if(a <= mid) Ans = Query(lson ,a ,b);
if(b > mid) Ans += Query(rson ,a ,b);
return Ans;
}
int main ()
{
int i ,n ,m ,cas = 1;
int key ,a ,b;
while(~scanf("%d" ,&n))
{
BuidTree(1 ,n ,1);
scanf("%d" ,&m);
printf("Case #%d:\n" ,cas ++);
while(m--)
{
scanf("%d %d %d" ,&key ,&a ,&b);
int t;
if(a > b)
{
t = a ,a = b ,b = t;
}
if(!key)
{
Update(1 ,n ,1 ,a ,b);
}
else
{
printf("%lld\n" ,Query(1 ,n ,1 ,a ,b));
}
}
printf("\n");
}
return 0;
}
SPOJ 2713 线段树(sqrt)的更多相关文章
- SPOJ GSS3 线段树系列1
SPOJ GSS系列真是有毒啊! 立志刷完,把线段树搞完! 来自lydrainbowcat线段树上的一道例题.(所以解法参考了lyd老师) 题意翻译 n 个数, q 次操作 操作0 x y把 Ax 修 ...
- SPOJ - GSS1 —— 线段树 (结点信息合并)
题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...
- SPOJ COT3 Combat on a tree(Trie树、线段树的合并)
题目链接:http://www.spoj.com/problems/COT3/ Alice and Bob are playing a game on a tree of n nodes.Each n ...
- SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
- GSS4 2713. Can you answer these queries IV 线段树
GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...
- SPOJ 1557. Can you answer these queries II 线段树
Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...
- bzoj 2482: [Spoj GSS2] Can you answer these queries II 线段树
2482: [Spoj1557] Can you answer these queries II Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 145 ...
- spoj gss2 : Can you answer these queries II 离线&&线段树
1557. Can you answer these queries II Problem code: GSS2 Being a completist and a simplist, kid Yang ...
- SPOJ GSS1_Can you answer these queries I(线段树区间合并)
SPOJ GSS1_Can you answer these queries I(线段树区间合并) 标签(空格分隔): 线段树区间合并 题目链接 GSS1 - Can you answer these ...
随机推荐
- createNewFile() 报错 open failed: ENOENT (No such file or directory) 的解决方案
在写Android应用中使用createNewFile() 遇到open failed: ENOENT (No such file or directory) 错误,在网上查了许多方法,不过都不能解决 ...
- pytorch(04)简单的线性回归
线性回归 线性回归是分析一个变量与另外一个变量之间关系的方法 因变量:y 自变量:x 关系:线性 y = wx+b 分析:求解w,b 求解步骤: 确定模型,Model:y = wx+b 选择损失函数, ...
- 如何使用 HttpReports 监控 .NET Core 应用程序
简介 HttpReports 基于.NET Core 开发的APM监控系统,使用MIT开源协议,主要功能包括,统计, 分析, 可视化, 监控,追踪等,适合在中小项目中使用. github:https: ...
- 在Asp.Net Core 5 中使用EF Core连接MariaDB
升级到Asp.Net Core 5,使用EF Core连接MariaDB,使用的Nuget包Pomelo.EntityFrameworkCore.MySql也升级到了5.0.0-alpha.2,然后发 ...
- 去哪找Java练手项目?
经常有读者在微信上问我: 在学编程的过程中,看了不少书.视频课程,但是看完.听完之后感觉还是不会编程,想找一些项目来练手,但是不知道去哪儿找? 类似的问题,有不少读者问,估计是大部分人的困惑. 练手项 ...
- 为什么是InfluxDB | 写在《InfluxDB原理和实战》出版之际
1年前写的一篇旧文,文中的分析,以及探讨的问题和观点,至今仍有意义. 从2016年起,笔者在腾讯公司负责QQ后台的海量服务分布式组件的架构设计和研发工作,例如微服务开发框架SPP.名字路由CMLB.名 ...
- P1115_最大子段和(JAVA语言)
思路:贪心.累加求和,若和小于0则设置和为0,因为和小于0时对这段序列和无正作用,只会使整体变小,所以我们把小于0的段舍弃,从下一个数开始求序列和. 题目描述 给出一段序列,选出其中连续且非空的一段使 ...
- [倍增][换根DP]luogu P5024 保卫王国
题面 https://www.luogu.com.cn/problem/P5024 分析 可以对有限制的点对之间的链进行在倍增上的DP数组合并. 需要通过一次正向树形DP和一次换根DP得到g[0][i ...
- 如何获取占用U盘的进程
依次打开开始---所有程序---附件---系统工具---资源监视器. 打开CPU标签栏,在"关联的句柄"中的搜索框中输入U盘的盘符,如G: 按回车搜索即可出结果. 在搜索结果中右键 ...
- 一文彻底搞懂JS前端5大模块化规范及其区别
码文不易,转载请带上本文链接,感谢~ https://www.cnblogs.com/echoyya/p/14577243.html 目录 码文不易,转载请带上本文链接,感谢~ https://www ...