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 ...
随机推荐
- 【Arduino学习笔记08】使用串口监视器显示数据
代码及相关说明: 1 // 示例:读取模拟输入并显示在串口监视器中 2 3 const int ANALOG_IN = 0; 4 int val = 0; 5 6 void setup(){ 7 Se ...
- AI人脸匹对
人脸匹对 技术 调用到百度的AI接口,layui的图片上传,栅格化布局 核心代码 纯py代码运行 # encoding:utf-8 from aip import AipFace import bas ...
- 【DB宝42】MySQL高可用架构MHA+ProxySQL实现读写分离和负载均衡
目录 一.MHA+ProxySQL架构 二.快速搭建MHA环境 2.1 下载MHA镜像 2.2 编辑yml文件,创建MHA相关容器 2.3 安装docker-compose软件(若已安装,可忽略) 2 ...
- 推荐一个能让谷歌浏览器变暗色的插件(darkreader)
下载 https://codechina.csdn.net/mirrors/darkreader/darkreader?utm_source=csdn_github_accelerator 安装教程 ...
- React实用技巧
取消请求 React 中当前正在发出请求的组件从页面上卸载了,理想情况下这个请求也应该取消掉,那么如何把请求的取消和页面的卸载关联在一起呢? 这里要考虑利用 useEffect 传入函数的返回值: u ...
- sqlmap在https下的一种错误 - ssl连接失败
在昨天与师傅的交流中师傅考了我一个问题,在用sqlmap跑的时候遇到ssl爆红该怎么办,因为在实战中并没有遇到过这种情况,所以今天补一下知识. 首先查询了ssl的概念,通俗来说,如果一个网站没有安装s ...
- 采用React + Fabric + ImageMagick 实现大图片DIY定制
一,需求背景: 某个印刷公司,有一系列的设计文件模板.接到客户订单时,就在这些设计文件模板上,做一些简单的定制,就能够满足客户的印刷需求. 如在设计文件模板上添加客户的Logo,二维码,联系方式等. ...
- elf.h
1 /* This file defines standard ELF types, structures, and macros. 2 Copyright (C) 1995-2019 Free So ...
- Python爬虫系列之爬取美团美食板块商家数据(二)
今天为大家重写一个美团美食板块小爬虫,说不定哪天做旅游攻略的时候也可以用下呢.废话不多说,让我们愉快地开始吧~ 开发工具 Python版本:3.6.4 相关模块: requests模块: argpar ...
- Docker的深入浅出
定义: Docker是一个虚拟环境容器,可以将你的开发环境.代码.配置文件等一并打包到这个容器中,并发布和应用到任意平台中. 原理: docker底层使用了LXC来实现,LXC将linux进程沙盒化, ...